summaryrefslogtreecommitdiff
path: root/autoload/ale/handlers/sml.vim
blob: f5365dd6fb64d0fd33406f48cf9d09407e6ebbbd (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
" Author: Jake Zimmerman <jake@zimmerman.io>
" Description: Shared functions for SML linters

" The glob to use for finding the .cm file.
"
" See :help ale-sml-smlnj for more information.
call ale#Set('sml_smlnj_cm_file', '*.cm')

function! ale#handlers#sml#GetCmFile(buffer) abort
    let l:pattern = ale#Var(a:buffer, 'sml_smlnj_cm_file')
    let l:as_list = 1

    let l:cmfile = ''

    for l:path in ale#path#Upwards(expand('#' . a:buffer . ':p:h'))
        let l:results = glob(l:path . '/' . l:pattern, 0, l:as_list)

        if len(l:results) > 0
            " If there is more than one CM file, we take the first one
            " See :help ale-sml-smlnj for how to configure this.
            let l:cmfile = l:results[0]
        endif
    endfor

    return l:cmfile
endfunction

" Only one of smlnj or smlnj-cm can be enabled at a time.
function! s:GetExecutable(buffer, source) abort
    if ale#handlers#sml#GetCmFile(a:buffer) is# ''
        " No CM file found; only allow single-file mode to be enabled
        if a:source is# 'smlnj-file'
            return 'sml'
        elseif a:source is# 'smlnj-cm'
            return ''
        endif
    else
        " Found a CM file; only allow cm-file mode to be enabled
        if a:source is# 'smlnj-file'
            return ''
        elseif a:source is# 'smlnj-cm'
            return 'sml'
        endif
    endif
endfunction

function! ale#handlers#sml#GetExecutableSmlnjCm(buffer) abort
    return s:GetExecutable(a:buffer, 'smlnj-cm')
endfunction

function! ale#handlers#sml#GetExecutableSmlnjFile(buffer) abort
    return s:GetExecutable(a:buffer, 'smlnj-file')
endfunction

function! ale#handlers#sml#Handle(buffer, lines) abort
    " Try to match basic sml errors
    " TODO(jez) We can get better errorfmt strings from Syntastic
    let l:out = []
    let l:pattern = '^\(.*\)\:\([0-9\.]\+\)\ \(\w\+\)\:\ \(.*\)'
    let l:pattern2 = '^\(.*\)\:\([0-9]\+\)\.\?\([0-9]\+\).* \(\(Warning\|Error\): .*\)'

    for l:line in a:lines
        let l:match2 = matchlist(l:line, l:pattern2)

        if len(l:match2) != 0
            call add(l:out, {
            \   'filename': l:match2[1],
            \   'lnum': l:match2[2] + 0,
            \   'col' : l:match2[3] - 1,
            \   'text': l:match2[4],
            \   'type': l:match2[4] =~# '^Warning' ? 'W' : 'E',
            \})

            continue
        endif

        let l:match = matchlist(l:line, l:pattern)

        if len(l:match) != 0
            call add(l:out, {
            \   'filename': l:match[1],
            \   'lnum': l:match[2] + 0,
            \   'text': l:match[3] . ': ' . l:match[4],
            \   'type': l:match[3] is# 'error' ? 'E' : 'W',
            \})
            continue
        endif
    endfor

    return l:out
endfunction

" vim:ts=4:sts=4:sw=4