]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/handlers/cspell.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 / cspell.vim
1 scriptencoding utf-8
2 " Author: David Houston <houstdav000>
3 " Description: Define a handler function for cspell's output
4
5 function! ale#handlers#cspell#GetExecutable(buffer) abort
6     return ale#path#FindExecutable(a:buffer,
7     \    'cspell', [
8     \        'node_modules/.bin/cspell',
9     \        'node_modules/cspell/bin.js',
10     \    ]
11     \)
12 endfunction
13
14 function! ale#handlers#cspell#GetLanguageId(buffer) abort
15     let l:filetype = getbufvar(a:buffer, '&filetype')
16
17     if l:filetype is# 'tex'
18         " Vim's tex corresponds to latex language-id in cspell
19         return 'latex'
20     elseif l:filetype is# 'plaintex'
21         " Vim's plaintex corresponds to tex language-id in cspell
22         return 'tex'
23     else
24         " Fallback to filetype for everything else.
25         return l:filetype
26     endif
27 endfunction
28
29 function! ale#handlers#cspell#GetCommand(buffer) abort
30     let l:executable = ale#handlers#cspell#GetExecutable(a:buffer)
31     let l:options = ale#Var(a:buffer, 'cspell_options')
32     let l:language_id = ale#handlers#cspell#GetLanguageId(a:buffer)
33
34     let l:language_id_option = empty(l:language_id) ? '' : '--language-id="' . l:language_id . '"'
35
36     return ale#node#Executable(a:buffer, l:executable)
37     \   . ' lint --no-color --no-progress --no-summary'
38     \   . ale#Pad(l:language_id_option)
39     \   . ale#Pad(l:options)
40     \   . ' -- stdin'
41 endfunction
42
43 function! ale#handlers#cspell#Handle(buffer, lines) abort
44     " Look for lines like the following:
45     "
46     " /home/user/repos/ale/README.md:3:128 - Unknown word (Neovim)
47     " match1: 3
48     " match2: 128
49     " match3: Unknown word (Neovim)
50     " match4: Neovim
51     let l:pattern = '\v^.*:(\d+):(\d+) - ([^\(]+\(([^\)]+)\).*)$'
52     let l:output = []
53
54     for l:match in ale#util#GetMatches(a:lines, l:pattern)
55         call add(l:output, {
56         \   'lnum': l:match[1] + 0,
57         \   'col': l:match[2] + 0,
58         \   'end_col': l:match[2] + len(l:match[4]) - 1,
59         \   'text': l:match[3],
60         \   'type': 'W',
61         \})
62     endfor
63
64     return l:output
65 endfunction
66
67 function! ale#handlers#cspell#DefineLinter(filetype) abort
68     call ale#Set('cspell_executable', 'cspell')
69     call ale#Set('cspell_options', '')
70     call ale#Set('cspell_use_global', get(g:, 'ale_use_global_executables', 0))
71
72     call ale#linter#Define(a:filetype, {
73     \   'name': 'cspell',
74     \   'executable': function('ale#handlers#cspell#GetExecutable'),
75     \   'command': function('ale#handlers#cspell#GetCommand'),
76     \   'callback': 'ale#handlers#cspell#Handle',
77     \})
78 endfunction