summaryrefslogtreecommitdiff
path: root/autoload/ale/handlers/rust.vim
blob: 12a826e084ea8a719a7c55f0d1dd5cb95748903f (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
" 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! ale#handlers#rust#HandleRustErrors(buffer, lines) abort
    let l:output = []

    " Format of JSON
    " 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

        for l:root_span in l:diagnostics.spans
            let l:span = l:root_span

            if ale#Var(a:buffer, 'rust_ignore_secondary_spans') && !get(l:span, 'is_primary', 1)
                continue
            endif

            if !empty(l:span)
                " :help ale-loclist-format
                let l:output_line = {
                \   'filename': l:span.file_name,
                \   'lnum': l:span.line_start,
                \   'end_lnum': l:span.line_end,
                \   'col': l:span.column_start,
                \   'end_col': l:span.column_end-1,
                \   'text': empty(l:span.label) ? l:diagnostics.message : printf('%s: %s', l:diagnostics.message, l:span.label),
                \   'type': toupper(l:diagnostics.level[0]),
                \}

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

                call add(l:output, l:output_line)
            endif
        endfor
    endfor

    return l:output
endfunction