summaryrefslogtreecommitdiff
path: root/ale_linters/d
diff options
context:
space:
mode:
authorw0rp <devw0rp@gmail.com>2017-02-04 18:30:30 +0000
committerw0rp <devw0rp@gmail.com>2017-02-04 18:30:30 +0000
commit829f87bc6a63cd442c7e2bf55870faa8804668a4 (patch)
tree5886ed67def110fb58b1515f03545e8f1c9e15bd /ale_linters/d
parent97131262abf31de883bcb65835ed6842f66bbd3b (diff)
downloadale-829f87bc6a63cd442c7e2bf55870faa8804668a4.zip
Fix #124 Finish implementing command chaining, and make it work for DMD
Diffstat (limited to 'ale_linters/d')
-rw-r--r--ale_linters/d/dmd.vim60
1 files changed, 46 insertions, 14 deletions
diff --git a/ale_linters/d/dmd.vim b/ale_linters/d/dmd.vim
index 0a6e9bf3..0c665d71 100644
--- a/ale_linters/d/dmd.vim
+++ b/ale_linters/d/dmd.vim
@@ -1,24 +1,54 @@
" Author: w0rp <devw0rp@gmail.com>
" Description: "dmd for D files"
-" A function for finding the dmd-wrapper script in the Vim runtime paths
-function! s:FindWrapperScript() abort
- for l:parent in split(&runtimepath, ',')
- " Expand the path to deal with ~ issues.
- let l:path = expand(l:parent . '/' . 'dmd-wrapper')
-
- if filereadable(l:path)
- return l:path
+function! s:FindDUBConfig(buffer) abort
+ " Find a DUB configuration file in ancestor paths.
+ " The most DUB-specific names will be tried first.
+ for l:possible_filename in ['dub.sdl', 'dub.json', 'package.json']
+ let l:dub_file = ale#util#FindNearestFile(a:buffer, l:possible_filename)
+
+ if !empty(l:dub_file)
+ return l:dub_file
endif
endfor
+
+ return ''
endfunction
-function! ale_linters#d#dmd#GetCommand(buffer) abort
- let l:wrapper_script = s:FindWrapperScript()
+function! ale_linters#d#dmd#DUBCommand(buffer) abort
+ " If we can't run dub, then skip this command.
+ if !executable('dub')
+ " Returning an empty string skips to the DMD command.
+ return ''
+ endif
+
+ let l:dub_file = s:FindDUBConfig(a:buffer)
- let l:command = l:wrapper_script . ' -o- -vcolumns -c'
+ if empty(l:dub_file)
+ return ''
+ endif
+
+ " To support older dub versions, we just change the directory to
+ " the directory where we found the dub config, and then run `dub describe`
+ " from that directory.
+ return 'cd ' . fnameescape(fnamemodify(l:dub_file, ':h'))
+ \ . ' && dub describe --import-paths'
+endfunction
+
+function! ale_linters#d#dmd#DMDCommand(buffer, dub_output) abort
+ let l:import_list = []
+
+ " Build a list of import paths generated from DUB, if available.
+ for l:line in a:dub_output
+ if !empty(l:line)
+ " The arguments must be '-Ifilename', not '-I filename'
+ call add(l:import_list, '-I' . fnameescape(l:line))
+ endif
+ endfor
- return l:command
+ return g:ale#util#stdin_wrapper . ' .d dmd '
+ \ . join(l:import_list)
+ \ . ' -o- -vcolumns -c'
endfunction
function! ale_linters#d#dmd#Handle(buffer, lines) abort
@@ -57,8 +87,10 @@ endfunction
call ale#linter#Define('d', {
\ 'name': 'dmd',
-\ 'output_stream': 'stderr',
\ 'executable': 'dmd',
-\ 'command_callback': 'ale_linters#d#dmd#GetCommand',
+\ 'command_chain': [
+\ {'callback': 'ale_linters#d#dmd#DUBCommand', 'output_stream': 'stdout'},
+\ {'callback': 'ale_linters#d#dmd#DMDCommand', 'output_stream': 'stderr'},
+\ ],
\ 'callback': 'ale_linters#d#dmd#Handle',
\})