]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/handlers/writegood.vim

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

Squashed '.vim/bundle/ale/' content from commit 22185c4c
[etc/vim.git] / autoload / ale / handlers / writegood.vim
1 " Author: Sumner Evans <sumner.evans98@gmail.com>
2 " Description: Error handling for errors in the write-good format.
3
4 function! ale#handlers#writegood#ResetOptions() abort
5     call ale#Set('writegood_options', '')
6     call ale#Set('writegood_executable', 'write-good')
7     call ale#Set('writegood_use_global', get(g:, 'ale_use_global_executables', 0))
8 endfunction
9
10 " Reset the options so the tests can test how they are set.
11 call ale#handlers#writegood#ResetOptions()
12
13 function! ale#handlers#writegood#GetExecutable(buffer) abort
14     return ale#path#FindExecutable(a:buffer, 'writegood', [
15     \   'node_modules/.bin/write-good',
16     \   'node_modules/write-good/bin/write-good.js',
17     \])
18 endfunction
19
20 function! ale#handlers#writegood#GetCommand(buffer) abort
21     let l:executable = ale#handlers#writegood#GetExecutable(a:buffer)
22     let l:options = ale#Var(a:buffer, 'writegood_options')
23
24     return ale#node#Executable(a:buffer, l:executable)
25     \   . (!empty(l:options) ? ' ' . l:options : '')
26     \   . ' %t'
27 endfunction
28
29 function! ale#handlers#writegood#Handle(buffer, lines) abort
30     " Look for lines like the following.
31     "
32     " "it is" is wordy or unneeded on line 20 at column 53
33     " "easily" can weaken meaning on line 154 at column 29
34     let l:marks_pattern = '\v^ *(\^+) *$'
35     let l:pattern = '\v^(".*"\s.*)\son\sline\s(\d+)\sat\scolumn\s(\d+)$'
36     let l:output = []
37     let l:last_len = 0
38
39     for l:match in ale#util#GetMatches(a:lines, [l:marks_pattern, l:pattern])
40         if empty(l:match[2])
41             let l:last_len = len(l:match[1])
42         else
43             let l:col = l:match[3] + 1
44
45             " Add the linter error. Note that we need to add 1 to the col because
46             " write-good reports the column corresponding to the space before the
47             " offending word or phrase.
48             call add(l:output, {
49             \   'text': l:match[1],
50             \   'lnum': l:match[2] + 0,
51             \   'col': l:col,
52             \   'end_col': l:last_len ? (l:col + l:last_len - 1) : l:col,
53             \   'type': 'W',
54             \})
55
56             let l:last_len = 0
57         endif
58     endfor
59
60     return l:output
61 endfunction
62
63 " Define the writegood linter for a given filetype.
64 function! ale#handlers#writegood#DefineLinter(filetype) abort
65     call ale#linter#Define(a:filetype, {
66     \   'name': 'writegood',
67     \   'aliases': ['write-good'],
68     \   'executable': function('ale#handlers#writegood#GetExecutable'),
69     \   'command': function('ale#handlers#writegood#GetCommand'),
70     \   'callback': 'ale#handlers#writegood#Handle',
71     \})
72 endfunction