summaryrefslogtreecommitdiff
path: root/ale_linters/rust/rustc.vim
blob: c7a06cc7c7e51c2946342d6a17e3cc5dc2e96c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
" Author: Daniel Schemala <istjanichtzufassen@gmail.com>
" Description: rustc for rust files

call ale#Set('rust_rustc_options', '--emit=mir -o /dev/null')
" FIXME Both cargo and rustc uses ale#handlers#rust#HandleRustErrors which
"       might or might not require this, depending on the fix for [#1570]
"       (https://github.com/dense-analysis/ale/issues/1570).
" call ale#Set('rust_only_current_buffer', '0')

function! ale_linters#rust#rustc#RustcCommand(buffer) abort
    " 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#path#FindNearestFile(a:buffer, 'Cargo.toml')

    if l:cargo_file isnot# ''
        let l:root = fnamemodify(l:cargo_file, ':h')
        let l:dependencies = ' -L ' . ale#Escape(ale#path#GetAbsPath(l:root, 'target/debug/deps'))
        \   . ' -L ' . ale#Escape(ale#path#GetAbsPath(l:root, 'target/release/deps'))
    else
        let l:dependencies = ''
    endif

    let l:options = ale#Var(a:buffer, 'rust_rustc_options')

    return 'rustc --error-format=json'
    \   . (!empty(l:options) ? ' ' . l:options : '')
    \   . l:dependencies . ' -'
endfunction

call ale#linter#Define('rust', {
\   'name': 'rustc',
\   'executable': 'rustc',
\   'command': function('ale_linters#rust#rustc#RustcCommand'),
\   'callback': 'ale#handlers#rust#HandleRustErrors',
\   'output_stream': 'stderr',
\})