2018-05-11 14:14:00 -04:00
|
|
|
" Author: buffalocoder - https://github.com/buffalocoder, soywod - https://github.com/soywod, hecrj - https://github.com/hecrj
|
2016-12-13 04:06:04 -05:00
|
|
|
" Description: Elm linting in Ale. Closely follows the Syntastic checker in https://github.com/ElmCast/elm-vim.
|
|
|
|
|
2018-05-11 14:07:28 -04:00
|
|
|
call ale#Set('elm_make_executable', 'elm')
|
|
|
|
call ale#Set('elm_make_use_global', get(g:, 'ale_use_global_executables', 0))
|
2017-09-10 07:58:42 -04:00
|
|
|
|
|
|
|
function! ale_linters#elm#make#GetExecutable(buffer) abort
|
2018-05-11 14:07:28 -04:00
|
|
|
return ale#node#FindExecutable(a:buffer, 'elm_make', [
|
2018-05-11 13:15:40 -04:00
|
|
|
\ 'node_modules/.bin/elm',
|
2017-09-10 07:58:42 -04:00
|
|
|
\])
|
|
|
|
endfunction
|
|
|
|
|
2017-01-22 09:54:57 -05:00
|
|
|
function! ale_linters#elm#make#Handle(buffer, lines) abort
|
2016-12-13 04:06:04 -05:00
|
|
|
let l:output = []
|
2017-06-25 12:12:40 -04:00
|
|
|
let l:unparsed_lines = []
|
2018-05-11 13:15:40 -04:00
|
|
|
|
2016-12-13 04:06:04 -05:00
|
|
|
for l:line in a:lines
|
2018-05-11 13:15:40 -04:00
|
|
|
if l:line[0] is# '{'
|
2018-05-15 11:06:52 -04:00
|
|
|
" Elm 0.19
|
|
|
|
call ale_linters#elm#make#HandleElm019Line(l:line, l:output)
|
|
|
|
elseif l:line[0] is# '['
|
|
|
|
" Elm 0.18
|
|
|
|
call ale_linters#elm#make#HandleElm018Line(l:line, l:output)
|
|
|
|
elseif l:line isnot# 'Successfully generated /dev/null'
|
2017-06-25 12:12:40 -04:00
|
|
|
call add(l:unparsed_lines, l:line)
|
2016-12-13 04:06:04 -05:00
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
|
2017-06-25 12:12:40 -04:00
|
|
|
if len(l:unparsed_lines) > 0
|
|
|
|
call add(l:output, {
|
|
|
|
\ 'lnum': 1,
|
|
|
|
\ 'type': 'E',
|
|
|
|
\ 'text': l:unparsed_lines[0],
|
|
|
|
\ 'detail': join(l:unparsed_lines, "\n")
|
|
|
|
\})
|
|
|
|
endif
|
|
|
|
|
2016-12-13 04:06:04 -05:00
|
|
|
return l:output
|
|
|
|
endfunction
|
|
|
|
|
2018-05-15 11:06:52 -04:00
|
|
|
function! ale_linters#elm#make#HandleElm019Line(line, output) abort
|
|
|
|
let l:report = json_decode(a:line)
|
|
|
|
|
|
|
|
if l:report.type is? 'error'
|
|
|
|
" General problem
|
|
|
|
let l:details = ale_linters#elm#make#ParseMessage(l:report.message)
|
|
|
|
|
|
|
|
if empty(l:report.path)
|
|
|
|
let l:report.path = 'Elm'
|
|
|
|
endif
|
|
|
|
|
|
|
|
if ale_linters#elm#make#FileIsBuffer(l:report.path)
|
|
|
|
call add(a:output, {
|
2018-05-25 16:22:47 -04:00
|
|
|
\ 'lnum': 1,
|
|
|
|
\ 'type': 'E',
|
|
|
|
\ 'text': l:details,
|
|
|
|
\})
|
2018-05-15 11:06:52 -04:00
|
|
|
else
|
|
|
|
call add(a:output, {
|
2018-05-25 16:22:47 -04:00
|
|
|
\ 'lnum': 1,
|
|
|
|
\ 'type': 'E',
|
|
|
|
\ 'text': l:report.path .' - '. l:details,
|
|
|
|
\ 'detail': l:report.path ." ----------\n\n". l:details,
|
|
|
|
\})
|
2018-05-15 11:06:52 -04:00
|
|
|
endif
|
|
|
|
else
|
|
|
|
" Compilation errors
|
|
|
|
for l:error in l:report.errors
|
|
|
|
let l:file_is_buffer = ale_linters#elm#make#FileIsBuffer(l:error.path)
|
|
|
|
|
|
|
|
for l:problem in l:error.problems
|
|
|
|
let l:details = ale_linters#elm#make#ParseMessage(l:problem.message)
|
|
|
|
|
|
|
|
if l:file_is_buffer
|
|
|
|
" Buffer module has problems
|
|
|
|
call add(a:output, {
|
2018-05-25 16:22:47 -04:00
|
|
|
\ 'lnum': l:problem.region.start.line,
|
|
|
|
\ 'col': l:problem.region.start.column,
|
|
|
|
\ 'end_lnum': l:problem.region.end.line,
|
|
|
|
\ 'end_col': l:problem.region.end.column,
|
|
|
|
\ 'type': 'E',
|
|
|
|
\ 'text': l:details,
|
|
|
|
\})
|
2018-05-15 11:06:52 -04:00
|
|
|
else
|
|
|
|
" Imported module has problems
|
|
|
|
let l:location = l:error.path .':'. l:problem.region.start.line
|
|
|
|
call add(a:output, {
|
2018-05-25 16:22:47 -04:00
|
|
|
\ 'lnum': 1,
|
|
|
|
\ 'type': 'E',
|
|
|
|
\ 'text': l:location .' - '. l:details,
|
|
|
|
\ 'detail': l:location ." ----------\n\n". l:details,
|
|
|
|
\})
|
2018-05-15 11:06:52 -04:00
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfor
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
|
|
|
function! ale_linters#elm#make#HandleElm018Line(line, output) abort
|
|
|
|
let l:errors = json_decode(a:line)
|
|
|
|
|
|
|
|
for l:error in l:errors
|
|
|
|
let l:file_is_buffer = ale_linters#elm#make#FileIsBuffer(l:error.file)
|
|
|
|
|
|
|
|
if l:file_is_buffer
|
|
|
|
" Current buffer has problems
|
|
|
|
call add(a:output, {
|
2018-05-25 16:22:47 -04:00
|
|
|
\ 'lnum': l:error.region.start.line,
|
|
|
|
\ 'col': l:error.region.start.column,
|
|
|
|
\ 'end_lnum': l:error.region.end.line,
|
|
|
|
\ 'end_col': l:error.region.end.column,
|
|
|
|
\ 'type': (l:error.type is? 'error') ? 'E' : 'W',
|
|
|
|
\ 'text': l:error.overview,
|
|
|
|
\ 'detail': l:error.overview . "\n\n" . l:error.details
|
|
|
|
\})
|
2018-05-15 11:06:52 -04:00
|
|
|
elseif l:error.type is? 'error'
|
|
|
|
" Imported module has errors
|
|
|
|
let l:location = l:error.file .':'. l:error.region.start.line
|
|
|
|
|
|
|
|
call add(a:output, {
|
2018-05-25 16:22:47 -04:00
|
|
|
\ 'lnum': 1,
|
|
|
|
\ 'type': 'E',
|
|
|
|
\ 'text': l:location .' - '. l:error.overview,
|
|
|
|
\ 'detail': l:location ." ----------\n\n". l:error.overview . "\n\n" . l:error.details
|
|
|
|
\})
|
2018-05-15 11:06:52 -04:00
|
|
|
endif
|
|
|
|
endfor
|
|
|
|
endfunction
|
|
|
|
|
2018-05-11 13:15:40 -04:00
|
|
|
function! ale_linters#elm#make#FileIsBuffer(path) abort
|
2018-07-12 08:05:59 -04:00
|
|
|
return ale#path#IsTempName(a:path)
|
2018-05-11 13:15:40 -04:00
|
|
|
endfunction
|
|
|
|
|
2018-05-11 22:16:14 -04:00
|
|
|
function! ale_linters#elm#make#ParseMessage(message) abort
|
|
|
|
return join(map(copy(a:message), 'ale_linters#elm#make#ParseMessageItem(v:val)'), '')
|
|
|
|
endfunction
|
|
|
|
|
2018-05-11 13:15:40 -04:00
|
|
|
function! ale_linters#elm#make#ParseMessageItem(item) abort
|
|
|
|
if type(a:item) == type('')
|
|
|
|
return a:item
|
|
|
|
else
|
|
|
|
return a:item.string
|
|
|
|
endif
|
|
|
|
endfunction
|
|
|
|
|
2016-12-13 04:06:04 -05:00
|
|
|
" Return the command to execute the linter in the projects directory.
|
|
|
|
" If it doesn't, then this will fail when imports are needed.
|
|
|
|
function! ale_linters#elm#make#GetCommand(buffer) abort
|
2018-05-11 13:15:40 -04:00
|
|
|
let l:elm_json = ale#path#FindNearestFile(a:buffer, 'elm.json')
|
2017-09-10 07:58:42 -04:00
|
|
|
let l:elm_exe = ale_linters#elm#make#GetExecutable(a:buffer)
|
2018-05-11 13:15:40 -04:00
|
|
|
|
2018-05-15 11:06:52 -04:00
|
|
|
if empty(l:elm_json)
|
|
|
|
" Fallback to Elm 0.18
|
|
|
|
let l:elm_json = ale#path#FindNearestFile(a:buffer, 'elm-package.json')
|
|
|
|
endif
|
|
|
|
|
2018-05-11 13:15:40 -04:00
|
|
|
if empty(l:elm_json)
|
2016-12-13 04:06:04 -05:00
|
|
|
let l:dir_set_cmd = ''
|
|
|
|
else
|
2018-05-11 13:15:40 -04:00
|
|
|
let l:root_dir = fnamemodify(l:elm_json, ':p:h')
|
2017-05-12 04:20:16 -04:00
|
|
|
let l:dir_set_cmd = 'cd ' . ale#Escape(l:root_dir) . ' && '
|
2016-12-13 04:06:04 -05:00
|
|
|
endif
|
|
|
|
|
2018-05-11 13:15:40 -04:00
|
|
|
" The elm compiler, at the time of this writing, uses '/dev/null' as
|
Elm file filter & Windows bug fixes (#223)
* Filters out unrelated errors in Elm linter
The function now filters out errors that are unrelated to the file,
those that were found in imported modules.
It does this by comparing the temp directory environment variable to the
file name in the elm output. If the file begins with the temp directory,
then it sould be included (it's from the buffer).
* Changing output to '/dev/null'
Turns out the compiler only accepts /dev/null as an ignorable name. It's
hard-coded here
https://github.com/elm-lang/elm-make/blob/master/src/Flags.hs
Changing this allows Windows linting to work. Otherwise the compiler
errors when using "nul"
* Fixes for Windows
Should now be able to successfully handle Windows.
Windows seemed to not handle the ";" properly, so I switched it to "&&",
which probably should've been done anyway to prevent false positives.
Oddly, matchend(l:error.file, l:temp_dir), and various other regex
solutions, couldn't properly match the two. Subsetting did though, hence
the new solution.
* Applying corrections
Made the file check case-insensitive for Windows, case-sensitive for
Unix/non-windows.
Added comment explaining hard coding of 'dev/null'
* Spelling correction
* Minor corrections
Actually uses the is_file_buffer variable now, added space between the
if statements, and added space between '-'
2016-12-16 05:41:21 -05:00
|
|
|
" a sort of flag to tell the compiler not to generate an output file,
|
2018-05-11 13:15:40 -04:00
|
|
|
" which is why this is hard coded here.
|
|
|
|
" Source: https://github.com/elm-lang/elm-compiler/blob/19d5a769b30ec0b2fc4475985abb4cd94cd1d6c3/builder/src/Generate/Output.hs#L253
|
2017-09-10 07:58:42 -04:00
|
|
|
let l:elm_cmd = ale#Escape(l:elm_exe)
|
2018-05-11 13:15:40 -04:00
|
|
|
\ . ' make'
|
2017-09-10 07:58:42 -04:00
|
|
|
\ . ' --report=json'
|
2017-10-06 17:26:50 -04:00
|
|
|
\ . ' --output=/dev/null'
|
2016-12-13 04:06:04 -05:00
|
|
|
|
2017-02-11 14:40:57 -05:00
|
|
|
return l:dir_set_cmd . ' ' . l:elm_cmd . ' %t'
|
2016-12-13 04:06:04 -05:00
|
|
|
endfunction
|
|
|
|
|
|
|
|
call ale#linter#Define('elm', {
|
|
|
|
\ 'name': 'make',
|
2017-09-10 07:58:42 -04:00
|
|
|
\ 'executable_callback': 'ale_linters#elm#make#GetExecutable',
|
2016-12-13 04:06:04 -05:00
|
|
|
\ 'output_stream': 'both',
|
|
|
|
\ 'command_callback': 'ale_linters#elm#make#GetCommand',
|
|
|
|
\ 'callback': 'ale_linters#elm#make#Handle'
|
|
|
|
\})
|