summaryrefslogtreecommitdiff
path: root/ale_linters/rust/rustc.vim
diff options
context:
space:
mode:
authorEinfachToll <istjanichtzufassen@googlemail.com>2017-01-12 10:33:55 +0100
committerw0rp <w0rp@users.noreply.github.com>2017-01-12 09:33:55 +0000
commit9c5f092b4f9464d035d6a334b3053f2f3eb95c22 (patch)
tree581ae70db5f0ebcf23a80bdae49585d122a9f763 /ale_linters/rust/rustc.vim
parent75485d53f67ab665fb7a4e13803d87c71b7368ae (diff)
downloadale-9c5f092b4f9464d035d6a334b3053f2f3eb95c22.zip
Add support for Rust using rustc and cargo (#230)
* Add rustc checker for rust files * Add documentation for rustc * Use a nice helper function * Add cargo as linter * Complete the doc for rust linters * Put l: in front of every local variable * Apply the requested stylistic changes
Diffstat (limited to 'ale_linters/rust/rustc.vim')
-rw-r--r--ale_linters/rust/rustc.vim99
1 files changed, 99 insertions, 0 deletions
diff --git a/ale_linters/rust/rustc.vim b/ale_linters/rust/rustc.vim
new file mode 100644
index 00000000..0c1f3028
--- /dev/null
+++ b/ale_linters/rust/rustc.vim
@@ -0,0 +1,99 @@
+" Author: Daniel Schemala <istjanichtzufassen@gmail.com>
+" 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#handle_rustc_errors(buffer_number, errorlines)
+ 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 !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
+ if l:span.is_primary &&
+ \ (l:span.file_name ==# l:file_name || l:span.file_name ==# '<anon>')
+ 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:find_error_in_expansion(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:find_error_in_expansion(span, file_name)
+ 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:find_error_in_expansion(a:span.expansion.span, a:file_name)
+ endif
+
+ return []
+endfunction
+
+
+function! ale_linters#rust#rustc#rustc_command(buffer_number)
+ " Try to guess the library search path. If the project is managed by cargo,
+ " it's usually <project root>/target/debug/deps/ or
+ " <project root>/target/release/deps/
+ let l:cargo_file = ale#util#FindNearestFile(a:buffer_number, 'Cargo.toml')
+
+ if l:cargo_file !=# ''
+ let l:project_root = fnamemodify(l:cargo_file, ':h')
+ let l:dependencies = '-L ' . l:project_root . '/target/debug/deps -L ' .
+ \ l:project_root . '/target/release/deps'
+ else
+ let l:dependencies = ''
+ endif
+
+ 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#rustc_command',
+\ 'callback': 'ale_linters#rust#rustc#handle_rustc_errors',
+\ 'output_stream': 'stderr',
+\})