Skip to content

Commit

Permalink
Fix windows path check on rust linter (#736)
Browse files Browse the repository at this point in the history
* Fix rust linter on windows

* Add windows path test

* Use ale#path#IsBufferPath to compare paths

* Fix errors
  • Loading branch information
daa84 authored and w0rp committed Jul 7, 2017
1 parent d438f8f commit 74f2afb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
11 changes: 5 additions & 6 deletions autoload/ale/handlers/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ if !exists('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
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]
endif

if !empty(a:span.expansion)
return s:FindErrorInExpansion(a:span.expansion.span, a:file_name)
return s:FindErrorInExpansion(a:span.expansion.span, a:buffer)
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
Expand All @@ -48,7 +47,7 @@ function! ale#handlers#rust#HandleRustErrorsForFile(buffer, full_filename, lines
for l:span in l:error.spans
if (
\ l:span.is_primary
\ && (a:full_filename =~ (l:span.file_name . '$') || l:span.file_name ==# '<anon>')
\ && (ale#path#IsBufferPath(a:buffer, l:span.file_name) || l:span.file_name ==# '<anon>')
\)
call add(l:output, {
\ 'lnum': l:span.line_start,
Expand All @@ -61,7 +60,7 @@ function! ale#handlers#rust#HandleRustErrorsForFile(buffer, full_filename, lines
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)
let l:root_cause = s:FindErrorInExpansion(l:span, a:buffer)

if !empty(l:root_cause)
call add(l:output, {
Expand Down
34 changes: 30 additions & 4 deletions test/handler/test_rust_handler.vader
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Execute(The Rust handler should handle rustc output):
call ale#test#SetFilename('src/playpen.rs')

AssertEqual
\ [
\ {
Expand All @@ -18,7 +20,7 @@ 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(347, 'src/playpen.rs', [
\ ale#handlers#rust#HandleRustErrorsForFile(bufnr(''), 'src/playpen.rs', [
\ '',
\ 'ignore this',
\ '{"message":"expected one of `.`, `;`, `?`, `}`, or an operator, found `for`","code":null,"level":"error","spans":[{"file_name":"<anon>","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}',
Expand All @@ -28,6 +30,8 @@ Execute(The Rust handler should handle rustc output):
\ ])

Execute(The Rust handler should handle cargo output):
call ale#test#SetFilename('src/playpen.rs')

AssertEqual
\ [
\ {
Expand All @@ -47,15 +51,35 @@ 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(347, 'src/playpen.rs', [
\ ale#handlers#rust#HandleRustErrorsForFile(bufnr(''), '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"}}',
\ ])

" 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 show detailed errors):
call ale#test#SetFilename('src/playpen.rs')

AssertEqual
\ [
\ {
Expand All @@ -67,17 +91,19 @@ Execute(The Rust handler should show detailed errors):
\ 'text': 'mismatched types: expected bool, found integral variable',
\ },
\ ],
\ ale#handlers#rust#HandleRustErrorsForFile(347, 'src/playpen.rs', [
\ ale#handlers#rust#HandleRustErrorsForFile(bufnr(''), 'src/playpen.rs', [
\ '',
\ '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"}}',
\ ])

Execute(The Rust handler should find correct files):
call ale#test#SetFilename('src/noerrors/mod.rs')

AssertEqual
\ [],
\ ale#handlers#rust#HandleRustErrorsForFile(347, 'src/noerrors/mod.rs', [
\ ale#handlers#rust#HandleRustErrorsForFile(bufnr(''), 'src/noerrors/mod.rs', [
\ '',
\ '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"}}',
Expand Down

0 comments on commit 74f2afb

Please sign in to comment.