summaryrefslogtreecommitdiff
path: root/autoload
diff options
context:
space:
mode:
authorw0rp <devw0rp@gmail.com>2017-02-07 21:17:03 +0000
committerw0rp <devw0rp@gmail.com>2017-02-07 21:17:10 +0000
commitc3ebe7bd9eb70952cdbdf8a0a8127533c493fe34 (patch)
tree91e706ee6dbf29dc9040ff01875aa2ef6d8d1344 /autoload
parent472631573ed46f8750fd07906068ccf14d341c9b (diff)
downloadale-c3ebe7bd9eb70952cdbdf8a0a8127533c493fe34.zip
Cover the Rust handler with some tests
Diffstat (limited to 'autoload')
-rw-r--r--autoload/ale/handlers/rust.vim90
1 files changed, 90 insertions, 0 deletions
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 <istjanichtzufassen@gmail.com>,
+" w0rp <devw0rp@gmail.com>
+"
+" 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 ==# '<anon>')
+ \)
+ 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