summaryrefslogtreecommitdiff
path: root/autoload/ale/handlers/writegood.vim
blob: 8ae61a38bfb43ea9fa129e1ed0605f2012be2022 (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
" Author: Sumner Evans <sumner.evans98@gmail.com>
" Description: Error handling for errors in the write-good format.

function! ale#handlers#writegood#ResetOptions() abort
    call ale#Set('writegood_options', '')
    call ale#Set('writegood_executable', 'write-good')
    call ale#Set('writegood_use_global', get(g:, 'ale_use_global_executables', 0))
endfunction

" Reset the options so the tests can test how they are set.
call ale#handlers#writegood#ResetOptions()

function! ale#handlers#writegood#GetExecutable(buffer) abort
    return ale#node#FindExecutable(a:buffer, 'writegood', [
    \   'node_modules/.bin/write-good',
    \   'node_modules/write-good/bin/write-good.js',
    \])
endfunction

function! ale#handlers#writegood#GetCommand(buffer) abort
    let l:executable = ale#handlers#writegood#GetExecutable(a:buffer)
    let l:options = ale#Var(a:buffer, 'writegood_options')

    return ale#node#Executable(a:buffer, l:executable)
    \   . (!empty(l:options) ? ' ' . l:options : '')
    \   . ' %t'
endfunction

function! ale#handlers#writegood#Handle(buffer, lines) abort
    " Look for lines like the following.
    "
    " "it is" is wordy or unneeded on line 20 at column 53
    " "easily" can weaken meaning on line 154 at column 29
    let l:marks_pattern = '\v^ *(\^+) *$'
    let l:pattern = '\v^(".*"\s.*)\son\sline\s(\d+)\sat\scolumn\s(\d+)$'
    let l:output = []
    let l:last_len = 0

    for l:match in ale#util#GetMatches(a:lines, [l:marks_pattern, l:pattern])
        if empty(l:match[2])
            let l:last_len = len(l:match[1])
        else
            let l:col = l:match[3] + 1

            " Add the linter error. Note that we need to add 1 to the col because
            " write-good reports the column corresponding to the space before the
            " offending word or phrase.
            call add(l:output, {
            \   'text': l:match[1],
            \   'lnum': l:match[2] + 0,
            \   'col': l:col,
            \   'end_col': l:last_len ? (l:col + l:last_len - 1) : l:col,
            \   'type': 'W',
            \})

            let l:last_len = 0
        endif
    endfor

    return l:output
endfunction

" Define the writegood linter for a given filetype.
function! ale#handlers#writegood#DefineLinter(filetype) abort
    call ale#linter#Define(a:filetype, {
    \   'name': 'writegood',
    \   'aliases': ['write-good'],
    \   'executable': function('ale#handlers#writegood#GetExecutable'),
    \   'command': function('ale#handlers#writegood#GetCommand'),
    \   'callback': 'ale#handlers#writegood#Handle',
    \})
endfunction