diff --git a/ale_linters/rust/cargo.vim b/ale_linters/rust/cargo.vim index 8c782656..7f821d2c 100644 --- a/ale_linters/rust/cargo.vim +++ b/ale_linters/rust/cargo.vim @@ -1,7 +1,6 @@ " Author: Daniel Schemala " Description: rustc invoked by cargo for rust files - function! ale_linters#rust#cargo#GetCargoExecutable(bufnr) abort if ale#util#FindNearestFile(a:bufnr, 'Cargo.toml') !=# '' return 'cargo' @@ -16,6 +15,6 @@ call ale#linter#Define('rust', { \ 'name': 'cargo', \ 'executable_callback': 'ale_linters#rust#cargo#GetCargoExecutable', \ 'command': 'cargo build --message-format=json -q', -\ 'callback': 'ale_linters#rust#rustc#HandleRustcErrors', +\ 'callback': 'ale#handlers#rust#HandleRustErrors', \ 'output_stream': 'stdout', \}) diff --git a/ale_linters/rust/rustc.vim b/ale_linters/rust/rustc.vim index 97d5d0bb..1d080b98 100644 --- a/ale_linters/rust/rustc.vim +++ b/ale_linters/rust/rustc.vim @@ -1,86 +1,6 @@ " Author: Daniel Schemala " Description: rustc for rust files -if !exists('g:ale_rust_ignore_error_codes') - let g:ale_rust_ignore_error_codes = [] -endif - - -function! ale_linters#rust#rustc#HandleRustcErrors(buffer_number, errorlines) abort - let l:file_name = fnamemodify(bufname(a:buffer_number), ':t') - let l:output = [] - - for l:errorline in a:errorlines - " ignore everything that is not Json - if l:errorline !~# '^{' - continue - endif - - let l:error = json_decode(l:errorline) - - if has_key(l:error, 'message') && type(l:error.message) == type({}) - let l:error = l:error.message - endif - - if !has_key(l:error, 'code') - continue - endif - - if !empty(l:error.code) && index(g:ale_rust_ignore_error_codes, l:error.code.code) > -1 - continue - endif - - for l:span in l:error.spans - let l:span.file_name = fnamemodify(l:span.file_name, ':t') - if l:span.is_primary && - \ (l:span.file_name ==# l:file_name || l:span.file_name ==# '') - call add(l:output, { - \ 'bufnr': a:buffer_number, - \ 'lnum': l:span.line_start, - \ 'vcol': 0, - \ 'col': l:span.byte_start, - \ 'nr': -1, - \ 'text': l:error.message, - \ 'type': toupper(l:error.level[0]), - \}) - else - " when the error is caused in the expansion of a macro, we have - " to bury deeper - let l:root_cause = s:FindErrorInExpansion(l:span, l:file_name) - - if !empty(l:root_cause) - call add(l:output, { - \ 'bufnr': a:buffer_number, - \ 'lnum': l:root_cause[0], - \ 'vcol': 0, - \ 'col': l:root_cause[1], - \ 'nr': -1, - \ 'text': l:error.message, - \ 'type': toupper(l:error.level[0]), - \}) - endif - endif - endfor - endfor - - return l:output -endfunction - - -" returns: a list [lnum, col] with the location of the error or [] -function! s:FindErrorInExpansion(span, file_name) abort - if a:span.file_name ==# a:file_name - return [a:span.line_start, a:span.byte_start] - endif - - if !empty(a:span.expansion) - return s:FindErrorInExpansion(a:span.expansion.span, a:file_name) - endif - - return [] -endfunction - - function! ale_linters#rust#rustc#RustcCommand(buffer_number) abort " Try to guess the library search path. If the project is managed by cargo, " it's usually /target/debug/deps/ or @@ -98,11 +18,10 @@ function! ale_linters#rust#rustc#RustcCommand(buffer_number) abort return 'rustc --error-format=json -Z no-trans ' . l:dependencies . ' -' endfunction - call ale#linter#Define('rust', { \ 'name': 'rustc', \ 'executable': 'rustc', \ 'command_callback': 'ale_linters#rust#rustc#RustcCommand', -\ 'callback': 'ale_linters#rust#rustc#HandleRustcErrors', +\ 'callback': 'ale#handlers#rust#HandleRustErrors', \ 'output_stream': 'stderr', \}) diff --git a/autoload/ale/handlers/rust.vim b/autoload/ale/handlers/rust.vim new file mode 100644 index 00000000..c00c2276 --- /dev/null +++ b/autoload/ale/handlers/rust.vim @@ -0,0 +1,90 @@ +" Author: Daniel Schemala , +" w0rp +" +" Description: This file implements handlers specific to Rust. + +if !exists('g:ale_rust_ignore_error_codes') + let g:ale_rust_ignore_error_codes = [] +endif + +" returns: a list [lnum, col] with the location of the error or [] +function! s:FindErrorInExpansion(span, file_name) abort + if a:span.file_name ==# a:file_name + return [a:span.line_start, a:span.byte_start] + endif + + if !empty(a:span.expansion) + return s:FindErrorInExpansion(a:span.expansion.span, a:file_name) + endif + + return [] +endfunction + +" A handler function which accepts a file name, to make unit testing easier. +function! ale#handlers#rust#HandleRustErrorsForFile(buffer, full_filename, lines) abort + let l:filename = fnamemodify(a:full_filename, ':t') + let l:output = [] + + for l:errorline in a:lines + " ignore everything that is not Json + if l:errorline !~# '^{' + continue + endif + + let l:error = json_decode(l:errorline) + + if has_key(l:error, 'message') && type(l:error.message) == type({}) + let l:error = l:error.message + endif + + if !has_key(l:error, 'code') + continue + endif + + if !empty(l:error.code) && index(g:ale_rust_ignore_error_codes, l:error.code.code) > -1 + continue + endif + + for l:span in l:error.spans + let l:span_filename = fnamemodify(l:span.file_name, ':t') + + if ( + \ l:span.is_primary + \ && (l:span_filename ==# l:filename || l:span_filename ==# '') + \) + call add(l:output, { + \ 'bufnr': a:buffer, + \ 'lnum': l:span.line_start, + \ 'vcol': 0, + \ 'col': l:span.byte_start, + \ 'nr': -1, + \ 'text': l:error.message, + \ 'type': toupper(l:error.level[0]), + \}) + else + " when the error is caused in the expansion of a macro, we have + " to bury deeper + let l:root_cause = s:FindErrorInExpansion(l:span, l:filename) + + if !empty(l:root_cause) + call add(l:output, { + \ 'bufnr': a:buffer, + \ 'lnum': l:root_cause[0], + \ 'vcol': 0, + \ 'col': l:root_cause[1], + \ 'nr': -1, + \ 'text': l:error.message, + \ 'type': toupper(l:error.level[0]), + \}) + endif + endif + endfor + endfor + + return l:output +endfunction + +" A handler for output for Rust linters. +function! ale#handlers#rust#HandleRustErrors(buffer, lines) abort + return ale#handlers#rust#HandleRustErrorsForFile(a:buffer, bufname(a:buffer), a:lines) +endfunction diff --git a/test/test_rust_handler.vader b/test/test_rust_handler.vader new file mode 100644 index 00000000..d4d54d37 --- /dev/null +++ b/test/test_rust_handler.vader @@ -0,0 +1,28 @@ +Execute(The Rust handler should handle rustc output): + AssertEqual + \ [ + \ {'lnum': 15, 'bufnr': 347, 'vcol': 0, 'nr': -1, 'type': 'E', 'col': 418, 'text': 'expected one of `.`, `;`, `?`, `}`, or an operator, found `for`'}, + \ {'lnum': 13, 'bufnr': 347, 'vcol': 0, 'nr': -1, 'type': 'E', 'col': 407, 'text': 'no method named `wat` found for type `std::string::String` in the current scope'}, + \ ], + \ ale#handlers#rust#HandleRustErrorsForFile(347, 'src/playpen.rs', [ + \ '', + \ 'ignore this', + \ '{"message":"expected one of `.`, `;`, `?`, `}`, or an operator, found `for`","code":null,"level":"error","spans":[{"file_name":"","byte_start":418,"byte_end":421,"line_start":15,"line_end":15,"column_start":5,"column_end":8,"is_primary":true,"text":[{"text":" for chr in source.trim().chars() {","highlight_start":5,"highlight_end":8}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[],"rendered":null}', + \ '{"message":"main function not found","code":null,"level":"error","spans":[],"children":[],"rendered":null}', + \ '{"message":"no method named `wat` found for type `std::string::String` in the current scope","code":null,"level":"error","spans":[{"file_name":"","byte_start":407,"byte_end":410,"line_start":13,"line_end":13,"column_start":7,"column_end":10,"is_primary":true,"text":[{"text":" s.wat()","highlight_start":7,"highlight_end":10}],"label":null,"suggested_replacement":null,"expansion":null}],"children":[],"rendered":null}', + \ '{"message":"aborting due to previous error","code":null,"level":"error","spans":[],"children":[],"rendered":null}', + \ ]) + +Execute(The Rust handler should handle cargo output): + AssertEqual + \ [ + \ {'lnum': 15, 'bufnr': 347, 'vcol': 0, 'nr': -1, 'type': 'E', 'col': 11505, 'text': 'expected one of `.`, `;`, `?`, `}`, or an operator, found `for`'}, + \ {'lnum': 13, 'bufnr': 347, 'vcol': 0, 'nr': -1, 'type': 'E', 'col': 11494, 'text': 'no method named `wat` found for type `std::string::String` in the current scope'}, + \ ], + \ ale#handlers#rust#HandleRustErrorsForFile(347, 'src/playpen.rs', [ + \ '', + \ 'ignore this', + \ '{"message":{"children":[],"code":null,"level":"error","message":"expected one of `.`, `;`, `?`, `}`, or an operator, found `for`","rendered":null,"spans":[{"byte_end":11508,"byte_start":11505,"column_end":8,"column_start":5,"expansion":null,"file_name":"src/playpen.rs","is_primary":true,"label":null,"line_end":15,"line_start":15,"suggested_replacement":null,"text":[{"highlight_end":8,"highlight_start":5,"text":" for chr in source.trim().chars() {"}]}]},"package_id":"update 0.0.1 (path+file:///home/w0rp/Downloads/rust-by-example)","reason":"compiler-message","target":{"kind":["bin"],"name":"update","src_path":"/home/w0rp/Downloads/rust-by-example/src/main.rs"}}', + \ '{"message":{"children":[],"code":null,"level":"error","message":"no method named `wat` found for type `std::string::String` in the current scope","rendered":null,"spans":[{"byte_end":11497,"byte_start":11494,"column_end":10,"column_start":7,"expansion":null,"file_name":"src/playpen.rs","is_primary":true,"label":null,"line_end":13,"line_start":13,"suggested_replacement":null,"text":[{"highlight_end":10,"highlight_start":7,"text":" s.wat()"}]}]},"package_id":"update 0.0.1 (path+file:///home/w0rp/Downloads/rust-by-example)","reason":"compiler-message","target":{"kind":["bin"],"name":"update","src_path":"/home/w0rp/Downloads/rust-by-example/src/main.rs"}}', + \ '{"message":{"children":[],"code":null,"level":"error","message":"aborting due to previous error","rendered":null,"spans":[]},"package_id":"update 0.0.1 (path+file:///home/w0rp/Downloads/rust-by-example)","reason":"compiler-message","target":{"kind":["bin"],"name":"update","src_path":"/home/w0rp/Downloads/rust-by-example/src/main.rs"}}', + \ ])