All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
1 " Author: buffalocoder - https://github.com/buffalocoder, soywod - https://github.com/soywod, hecrj - https://github.com/hecrj
2 " Description: Elm linting in Ale. Closely follows the Syntastic checker in https://github.com/ElmCast/elm-vim.
4 call ale#Set('elm_make_executable', 'elm')
5 call ale#Set('elm_make_use_global', get(g:, 'ale_use_global_executables', 0))
7 function! ale_linters#elm#make#Handle(buffer, lines) abort
9 let l:unparsed_lines = []
14 call ale_linters#elm#make#HandleElm019Line(l:line, l:output)
15 elseif l:line[0] is# '['
17 call ale_linters#elm#make#HandleElm018Line(l:line, l:output)
18 elseif l:line isnot# 'Successfully generated /dev/null'
19 call add(l:unparsed_lines, l:line)
23 if len(l:unparsed_lines) > 0
27 \ 'text': l:unparsed_lines[0],
28 \ 'detail': join(l:unparsed_lines, "\n")
35 function! ale_linters#elm#make#HandleElm019Line(line, output) abort
36 let l:report = json_decode(a:line)
38 if l:report.type is? 'error'
40 let l:details = ale_linters#elm#make#ParseMessage(l:report.message)
42 if empty(l:report.path)
43 let l:report.path = 'Elm'
46 if ale_linters#elm#make#FileIsBuffer(l:report.path)
56 \ 'text': l:report.path .' - '. l:details,
57 \ 'detail': l:report.path ." ----------\n\n". l:details,
62 for l:error in l:report.errors
63 let l:file_is_buffer = ale_linters#elm#make#FileIsBuffer(l:error.path)
65 for l:problem in l:error.problems
66 let l:details = ale_linters#elm#make#ParseMessage(l:problem.message)
69 " Buffer module has problems
71 \ 'lnum': l:problem.region.start.line,
72 \ 'col': l:problem.region.start.column,
73 \ 'end_lnum': l:problem.region.end.line,
74 \ 'end_col': l:problem.region.end.column,
79 " Imported module has problems
80 let l:location = l:error.path .':'. l:problem.region.start.line
84 \ 'text': l:location .' - '. l:details,
85 \ 'detail': l:location ." ----------\n\n". l:details,
93 function! ale_linters#elm#make#HandleElm018Line(line, output) abort
94 let l:errors = json_decode(a:line)
96 for l:error in l:errors
97 let l:file_is_buffer = ale_linters#elm#make#FileIsBuffer(l:error.file)
100 " Current buffer has problems
102 \ 'lnum': l:error.region.start.line,
103 \ 'col': l:error.region.start.column,
104 \ 'end_lnum': l:error.region.end.line,
105 \ 'end_col': l:error.region.end.column,
106 \ 'type': (l:error.type is? 'error') ? 'E' : 'W',
107 \ 'text': l:error.overview,
108 \ 'detail': l:error.overview . "\n\n" . l:error.details
110 elseif l:error.type is? 'error'
111 " Imported module has errors
112 let l:location = l:error.file .':'. l:error.region.start.line
117 \ 'text': l:location .' - '. l:error.overview,
118 \ 'detail': l:location ." ----------\n\n". l:error.overview . "\n\n" . l:error.details
124 function! ale_linters#elm#make#FileIsBuffer(path) abort
125 return ale#path#IsTempName(a:path)
128 function! ale_linters#elm#make#ParseMessage(message) abort
129 return join(map(copy(a:message), 'ale_linters#elm#make#ParseMessageItem(v:val)'), '')
132 function! ale_linters#elm#make#ParseMessageItem(item) abort
133 if type(a:item) is v:t_string
140 function! ale_linters#elm#make#GetPackageFile(buffer) abort
141 let l:elm_json = ale#path#FindNearestFile(a:buffer, 'elm.json')
144 " Fallback to Elm 0.18
145 let l:elm_json = ale#path#FindNearestFile(a:buffer, 'elm-package.json')
151 function! ale_linters#elm#make#IsVersionGte19(buffer) abort
152 let l:elm_json = ale_linters#elm#make#GetPackageFile(a:buffer)
154 if l:elm_json =~# '-package'
161 function! ale_linters#elm#make#GetRootDir(buffer) abort
162 let l:elm_json = ale_linters#elm#make#GetPackageFile(a:buffer)
167 return fnamemodify(l:elm_json, ':p:h')
171 function! ale_linters#elm#make#IsTest(buffer) abort
172 let l:root_dir = ale_linters#elm#make#GetRootDir(a:buffer)
178 let l:tests_dir = join([l:root_dir, 'tests', ''], has('win32') ? '\' : '/')
180 let l:buffer_path = fnamemodify(bufname(a:buffer), ':p')
182 if stridx(l:buffer_path, l:tests_dir) == 0
189 function! ale_linters#elm#make#GetCwd(buffer) abort
190 let l:root_dir = ale_linters#elm#make#GetRootDir(a:buffer)
192 return !empty(l:root_dir) ? l:root_dir : ''
195 " Return the command to execute the linter in the projects directory.
196 " If it doesn't, then this will fail when imports are needed.
197 function! ale_linters#elm#make#GetCommand(buffer) abort
198 let l:executable = ale_linters#elm#make#GetExecutable(a:buffer)
199 let l:is_v19 = ale_linters#elm#make#IsVersionGte19(a:buffer)
200 let l:is_using_elm_test = l:executable =~# 'elm-test$'
202 " elm-test needs to know the path of elm-make if elm isn't installed globally.
203 " https://github.com/rtfeldman/node-test-runner/blob/57728f10668f2d2ab3179e7e3208bcfa9a1f19aa/README.md#--compiler
204 if l:is_v19 && l:is_using_elm_test
205 let l:elm_make_executable = ale#path#FindExecutable(a:buffer, 'elm_make', ['node_modules/.bin/elm'])
206 let l:elm_test_compiler_flag = ' --compiler ' . l:elm_make_executable . ' '
208 let l:elm_test_compiler_flag = ' '
211 " The elm compiler, at the time of this writing, uses '/dev/null' as
212 " a sort of flag to tell the compiler not to generate an output file,
213 " which is why this is hard coded here.
214 " Source: https://github.com/elm-lang/elm-compiler/blob/19d5a769b30ec0b2fc4475985abb4cd94cd1d6c3/builder/src/Generate/Output.hs#L253
215 return '%e make --report=json --output=/dev/null'
216 \ . l:elm_test_compiler_flag
220 function! ale_linters#elm#make#GetExecutable(buffer) abort
221 let l:is_test = ale_linters#elm#make#IsTest(a:buffer)
222 let l:is_v19 = ale_linters#elm#make#IsVersionGte19(a:buffer)
224 if l:is_test && l:is_v19
225 return ale#path#FindExecutable(
228 \ ['node_modules/.bin/elm-test', 'node_modules/.bin/elm']
231 return ale#path#FindExecutable(a:buffer, 'elm_make', ['node_modules/.bin/elm'])
235 call ale#linter#Define('elm', {
237 \ 'executable': function('ale_linters#elm#make#GetExecutable'),
238 \ 'output_stream': 'both',
239 \ 'cwd': function('ale_linters#elm#make#GetCwd'),
240 \ 'command': function('ale_linters#elm#make#GetCommand'),
241 \ 'callback': 'ale_linters#elm#make#Handle'