summaryrefslogtreecommitdiff
path: root/autoload/ale/handlers/rust.vim
blob: 676e6c13930643039ec79c5306f9b34038ad4cf4 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
" 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

if !exists('g:ale_rust_ignore_secondary_spans')
    let g:ale_rust_ignore_secondary_spans = 0
endif

function! s:ProcessSpan(buffer, error, span) abort
    if ale#Var(a:buffer, 'rust_ignore_secondary_spans') && !get(a:span, 'is_primary', 1)
        return {}
    endif

    if !empty(a:span)
        " :help ale-loclist-format
        let l:output_line = {
        \   'lnum': a:span.line_start,
        \   'end_lnum': a:span.line_end,
        \   'col': a:span.column_start,
        \   'end_col': a:span.column_end-1,
        \   'text': empty(a:span.label) ? a:error.message : printf('%s: %s', a:error.message, a:span.label),
        \   'type': toupper(a:error.level[0]),
        \}
        " When operating on stdin rustc (and possibly others) gives '<anon>'
        " as the file_name. Leaving the filename property of the loclist
        " unpopulated makes ale connect such errors with the linted buffer.
        if a:span.file_name !=# '<anon>'
            let l:output_line['filename'] = a:span.file_name
        endif

        if has_key(a:error, 'rendered') && !empty(a:error.rendered)
            let l:output_line.detail = a:error.rendered
        endif

        return l:output_line
    endif
    return {}
endfunction

function! ale#handlers#rust#HandleRustErrors(buffer, lines) abort
    let l:output = []

    " Format of JSON is documented at
    " rustc: https://doc.rust-lang.org/rustc/json.html
    " cargo: https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages
    for l:diagnosticsline in a:lines
        " ignore everything that is not JSON
        if l:diagnosticsline !~# '^{'
            continue
        endif

        let l:diagnostics = json_decode(l:diagnosticsline)

        if has_key(l:diagnostics, 'message') && type(l:diagnostics.message) is v:t_dict
            let l:diagnostics = l:diagnostics.message
        endif

        if !has_key(l:diagnostics, 'code')
            continue
        endif

        if !empty(l:diagnostics.code) && index(g:ale_rust_ignore_error_codes, l:diagnostics.code.code) > -1
            continue
        endif

        let l:spans = []

        " FIXME One might also wish to loop over l:diagnostics.children and
        "       their spans.
        for l:span in l:diagnostics.spans
            call add(l:spans, l:span)

            let l:expansion = l:span.expansion
            if !empty(l:expansion)
                let l:expansion_span = l:expansion.span
                call add(l:spans, l:expansion_span)
            endif
        endfor

        for l:span in l:spans
            let l:output_line = s:ProcessSpan(a:buffer, l:diagnostics, l:span)
            if !empty(l:output_line)
                call add(l:output, l:output_line)
            endif
        endfor
    endfor

    return l:output
endfunction