From 18f4d5a6da532b5dd9a2cdf7e6f6aea05dced116 Mon Sep 17 00:00:00 2001 From: w0rp Date: Sun, 10 Sep 2017 00:05:04 +0100 Subject: [PATCH] Simplify some Rust handler code, and get the Rust handler tests passing on Windows --- autoload/ale/handlers/rust.vim | 49 ++---- test/handler/test_rust_handler.vader | 240 +++++++++++++++++++++++---- 2 files changed, 222 insertions(+), 67 deletions(-) diff --git a/autoload/ale/handlers/rust.vim b/autoload/ale/handlers/rust.vim index 12a5a16..395b915 100644 --- a/autoload/ale/handlers/rust.vim +++ b/autoload/ale/handlers/rust.vim @@ -7,25 +7,25 @@ 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, buffer) abort - if ale#path#IsBufferPath(a:buffer, a:span.file_name) - return [a:span.line_start, a:span.line_end, a:span.byte_start, a:span.byte_end] +function! s:FindSpan(buffer, span) abort + if ale#path#IsBufferPath(a:buffer, a:span.file_name) || a:span.file_name is# '' + return a:span endif - if !empty(a:span.expansion) - return s:FindErrorInExpansion(a:span.expansion.span, a:buffer) + " Search inside the expansion of an error, as the problem for this buffer + " could lie inside a nested object. + if !empty(get(a:span, 'expansion', v:null)) + return s:FindSpan(a:buffer, a:span.expansion.span) endif - return [] + 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 +function! ale#handlers#rust#HandleRustErrors(buffer, lines) abort let l:output = [] for l:errorline in a:lines - " ignore everything that is not Json + " ignore everything that is not JSON if l:errorline !~# '^{' continue endif @@ -44,11 +44,10 @@ function! ale#handlers#rust#HandleRustErrorsForFile(buffer, full_filename, lines continue endif - for l:span in l:error.spans - if ( - \ l:span.is_primary - \ && (ale#path#IsBufferPath(a:buffer, l:span.file_name) || l:span.file_name is# '') - \) + for l:root_span in l:error.spans + let l:span = s:FindSpan(a:buffer, l:root_span) + + if !empty(l:span) call add(l:output, { \ 'lnum': l:span.line_start, \ 'end_lnum': l:span.line_end, @@ -57,29 +56,9 @@ function! ale#handlers#rust#HandleRustErrorsForFile(buffer, full_filename, lines \ 'text': empty(l:span.label) ? l:error.message : printf('%s: %s', l:error.message, l:span.label), \ '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, a:buffer) - - if !empty(l:root_cause) - call add(l:output, { - \ 'lnum': l:root_cause[0], - \ 'end_lnum': l:root_cause[1], - \ 'col': l:root_cause[2], - \ 'end_col': l:root_cause[3], - \ '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/handler/test_rust_handler.vader b/test/handler/test_rust_handler.vader index 510ae69..a148103 100644 --- a/test/handler/test_rust_handler.vader +++ b/test/handler/test_rust_handler.vader @@ -20,13 +20,58 @@ Execute(The Rust handler should handle rustc output): \ 'text': 'no method named `wat` found for type `std::string::String` in the current scope', \ }, \ ], - \ ale#handlers#rust#HandleRustErrorsForFile(bufnr(''), 'src/playpen.rs', [ + \ ale#handlers#rust#HandleRustErrors(bufnr(''), [ \ '', \ '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}', + \ json_encode({ + \ 'message': 'expected one of `.`, `;`, `?`, `}`, or an operator, found `for`', + \ 'code': v: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': v:true, + \ 'label': v:null, + \ }, + \ ], + \ }), + \ json_encode({ + \ 'message': 'main function not found', + \ 'code': v:null, + \ 'level': 'error', + \ 'spans': [], + \ }), + \ json_encode({ + \ 'code': v:null, + \ 'level': 'error', + \ 'message': 'no method named `wat` found for type `std::string::String` in the current scope', + \ 'spans': [ + \ { + \ 'byte_end': 410, + \ 'byte_start': 407, + \ 'column_end': 10, + \ 'column_start': 7, + \ 'file_name': '', + \ 'is_primary': v:true, + \ 'label': v:null, + \ 'line_end': 13, + \ 'line_start': 13, + \ } + \ ] + \ }), + \ json_encode({ + \ 'code': v:null, + \ 'level': 'error', + \ 'message': 'aborting due to previous error', + \ 'spans': [ + \ ] + \ }), \ ]) Execute(The Rust handler should handle cargo output): @@ -51,31 +96,107 @@ Execute(The Rust handler should handle cargo output): \ 'text': 'no method named `wat` found for type `std::string::String` in the current scope', \ }, \ ], - \ ale#handlers#rust#HandleRustErrorsForFile(bufnr(''), 'src/playpen.rs', [ + \ ale#handlers#rust#HandleRustErrors(bufnr(''), [ \ '', \ '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"}}', + \ json_encode({ + \ 'message': { + \ 'code': v:null, + \ 'level': 'error', + \ 'message': 'expected one of `.`, `;`, `?`, `}`, or an operator, found `for`', + \ 'spans': [ + \ { + \ 'byte_end': 11508, + \ 'byte_start': 11505, + \ 'column_end': 8, + \ 'column_start': 5, + \ 'file_name': ale#path#Winify('src/playpen.rs'), + \ 'is_primary': v:true, + \ 'label': v:null, + \ 'line_end': 15, + \ 'line_start': 15, + \ } + \ ] + \ }, + \ }), + \ json_encode({ + \ 'message': { + \ 'code': v:null, + \ 'level': 'error', + \ 'message': 'no method named `wat` found for type `std::string::String` in the current scope', + \ 'spans': [ + \ { + \ 'byte_end': 11497, + \ 'byte_start': 11494, + \ 'column_end': 10, + \ 'column_start': 7, + \ 'file_name': ale#path#Winify('src/playpen.rs'), + \ 'is_primary': v:true, + \ 'label': v:null, + \ 'line_end': 13, + \ 'line_start': 13, + \ } + \ ] + \ }, + \ }), + \ json_encode({ + \ 'message': { + \ 'code': v:null, + \ 'level': 'error', + \ 'message': 'aborting due to previous error', + \ 'spans': [ + \ ] + \ }, + \ }), \ ]) -" Execute(The Rust handler should handle cargo output on Windows): -" call ale#test#SetFilename('src\nvim.rs') -" -" AssertEqual -" \ [ -" \ { -" \ 'lnum': 467, -" \ 'end_lnum': 467, -" \ 'type': 'E', -" \ 'col': 43198, -" \ 'end_col': 43199, -" \ 'text': 'expected one of `!` or `::`, found `#`: unexpected token', -" \ }, -" \ ], -" \ ale#handlers#rust#HandleRustErrorsForFile(bufnr(''), 'src\nvim.rs', [ -" \ '{"message":{"children":[],"code":null,"level":"error","message":"expected one of `!` or `::`, found `#`","rendered":null,"spans":[{"byte_end":43199,"byte_start":43198,"column_end":2,"column_start":1,"expansion":null,"file_name":"src\\nvim.rs","is_primary":true,"label":"unexpected token","line_end":467,"line_start":467,"suggested_replacement":null,"text":[{"highlight_end":2,"highlight_start":1,"text":"#[cfg(test)]\r"}]}]},"package_id":"nvim-gtk 0.1.2 (path+file:///E:/daa/local/neovim-gtk)","reason":"compiler-message","target":{"crate_types":["bin"],"kind":["bin"],"name":"nvim-gtk","src_path":"E:\\daa\\local\\neovim-gtk\\src\\main.rs"}}', -" \ ]) +Execute(The Rust handler should should errors from expansion spans): + AssertEqual + \ [ + \ { + \ 'lnum': 4, + \ 'end_lnum': 4, + \ 'type': 'E', + \ 'col': 52, + \ 'end_col': 54, + \ 'text': 'mismatched types: expected bool, found integral variable', + \ }, + \ ], + \ ale#handlers#rust#HandleRustErrors(bufnr(''), [ + \ json_encode({ + \ 'message': { + \ 'code': v:null, + \ 'level': 'error', + \ 'message': 'mismatched types', + \ 'spans': [ + \ { + \ 'byte_end': 1, + \ 'byte_start': 1, + \ 'column_end': 1, + \ 'column_start': 1, + \ 'file_name': ale#path#Winify('src/other.rs'), + \ 'is_primary': v:true, + \ 'label': 'some other error', + \ 'line_end': 4, + \ 'line_start': 4, + \ 'expansion': { + \ 'span': { + \ 'byte_end': 54, + \ 'byte_start': 52, + \ 'column_end': 23, + \ 'column_start': 21, + \ 'file_name': ale#path#Winify('src/playpen.rs'), + \ 'is_primary': v:true, + \ 'label': 'expected bool, found integral variable', + \ 'line_end': 4, + \ 'line_start': 4, + \ } + \ } + \ } + \ ] + \ }, + \ }), + \ ]) Execute(The Rust handler should show detailed errors): call ale#test#SetFilename('src/playpen.rs') @@ -91,11 +212,39 @@ Execute(The Rust handler should show detailed errors): \ 'text': 'mismatched types: expected bool, found integral variable', \ }, \ ], - \ ale#handlers#rust#HandleRustErrorsForFile(bufnr(''), 'src/playpen.rs', [ + \ ale#handlers#rust#HandleRustErrors(bufnr(''), [ \ '', \ 'ignore this', - \ '{"message":{"children":[],"code":null,"level":"error","message":"mismatched types","rendered":null,"spans":[{"byte_end":54,"byte_start":52,"column_end":23,"column_start":21,"expansion":null,"file_name":"src/playpen.rs","is_primary":true,"label":"expected bool, found integral variable","line_end":4,"line_start":4,"suggested_replacement":null,"text":[{"highlight_end":23,"highlight_start":21,"text":" let foo: bool = 42;"}]}]},"package_id":"ale-rust-details 0.1.1 (path+file:///home/jon/tmp/ale-rust-details)","reason":"compiler-message","target":{"crate_types":["bin"],"kind":["bin"],"name":"ale-rust-details","src_path":"/home/jon/tmp/ale-rust-details/src/main.rs"}}', - \ '{"message":{"children":[],"code":null,"level":"error","message":"aborting due to previous error(s)","rendered":null,"spans":[]},"package_id":"ale-rust-details 0.1.1 (path+file:///home/jon/tmp/ale-rust-details)","reason":"compiler-message","target":{"crate_types":["bin"],"kind":["bin"],"name":"ale-rust-details","src_path":"/home/jon/tmp/ale-rust-details/src/main.rs"}}', + \ json_encode({ + \ 'message': { + \ 'code': v:null, + \ 'level': 'error', + \ 'message': 'mismatched types', + \ 'spans': [ + \ { + \ 'byte_end': 54, + \ 'byte_start': 52, + \ 'column_end': 23, + \ 'column_start': 21, + \ 'expansion': v:null, + \ 'file_name': ale#path#Winify('src/playpen.rs'), + \ 'is_primary': v:true, + \ 'label': 'expected bool, found integral variable', + \ 'line_end': 4, + \ 'line_start': 4, + \ } + \ ] + \ }, + \ }), + \ json_encode({ + \ 'message': { + \ 'code': v:null, + \ 'level': 'error', + \ 'message': 'aborting due to previous error(s)', + \ 'spans': [ + \ ] + \ }, + \ }), \ ]) Execute(The Rust handler should find correct files): @@ -103,9 +252,36 @@ Execute(The Rust handler should find correct files): AssertEqual \ [], - \ ale#handlers#rust#HandleRustErrorsForFile(bufnr(''), 'src/noerrors/mod.rs', [ + \ ale#handlers#rust#HandleRustErrors(bufnr(''), [ \ '', \ 'ignore this', - \ '{"message":{"children":[],"code":null,"level":"error","message":"unresolved import `Undefined`","rendered":null,"spans":[{"byte_end":103,"byte_start":94,"column_end":14,"column_start":5,"expansion":null,"file_name":"src/haserrors/mod.rs","is_primary":true,"label":"no `Undefined` in the root","line_end":1,"line_start":1,"suggested_replacement":null,"text":[{"highlight_end":14,"highlight_start":5,"text":"use Undefined;"}]}]},"package_id":"sample 0.1.0 (path+file:///private/tmp/sample)","reason":"compiler-message","target":{"crate_types":["lib"],"kind":["lib"],"name":"sample","src_path":"/private/tmp/sample/src/lib.rs"}}', - \ '{"message":{"children":[],"code":null,"level":"error","message":"aborting due to previous error","rendered":null,"spans":[]},"package_id":"sample 0.1.0 (path+file:///private/tmp/sample)","reason":"compiler-message","target":{"crate_types":["lib"],"kind":["lib"],"name":"sample","src_path":"/private/tmp/sample/src/lib.rs"}}', + \ json_encode({ + \ 'message': { + \ 'code': v:null, + \ 'level': 'error', + \ 'message': 'unresolved import `Undefined`', + \ 'spans': [ + \ { + \ 'byte_end': 103, + \ 'byte_start': 94, + \ 'column_end': 14, + \ 'column_start': 5, + \ 'file_name': 'src/haserrors/mod.rs', + \ 'is_primary': v:true, + \ 'label': 'no `Undefined` in the root', + \ 'line_end': 1, + \ 'line_start': 1, + \ } + \ ] + \ }, + \ }), + \ json_encode({ + \ 'message': { + \ 'code': v:null, + \ 'level': 'error', + \ 'message': 'aborting due to previous error', + \ 'spans': [ + \ ] + \ }, + \ }), \ ])