]> git.madduck.net Git - etc/vim.git/blob - autoload/ale/handlers/languagetool.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 / languagetool.vim
1 " Author: Vincent (wahrwolf [at] wolfpit.net)
2 " Description: languagetool for markdown files
3 "
4 call ale#Set('languagetool_executable', 'languagetool')
5 call ale#Set('languagetool_options', '--autoDetect')
6
7 function! ale#handlers#languagetool#GetExecutable(buffer) abort
8     return ale#Var(a:buffer, 'languagetool_executable')
9 endfunction
10
11 function! ale#handlers#languagetool#GetCommand(buffer) abort
12     let l:executable = ale#handlers#languagetool#GetExecutable(a:buffer)
13     let l:options = ale#Var(a:buffer, 'languagetool_options')
14
15     return ale#Escape(l:executable)
16     \ . (empty(l:options) ? '' : ' ' . l:options) . ' %s'
17 endfunction
18
19 function! ale#handlers#languagetool#HandleOutput(buffer, lines) abort
20     " Match lines like:
21     " 1.) Line 5, column 1, Rule ID:
22     let l:head_pattern = '^\v.+.\) Line (\d+), column (\d+), Rule ID. (.+)$'
23     let l:head_matches = ale#util#GetMatches(a:lines, l:head_pattern)
24
25     " Match lines like:
26     " Message: Did you forget a comma after a conjunctive/linking adverb?
27     let l:message_pattern = '^\vMessage. (.+)$'
28     let l:message_matches = ale#util#GetMatches(a:lines, l:message_pattern)
29
30     " Match lines like:
31     "   ^^^^^ "
32     let l:markers_pattern = '^\v *(\^+) *$'
33     let l:markers_matches = ale#util#GetMatches(a:lines, l:markers_pattern)
34
35     let l:output = []
36
37
38     " Okay tbh I was to lazy to figure out a smarter solution here
39     " We just check that the arrays are same sized and merge everything
40     " together
41     let l:i = 0
42
43     while l:i < len(l:head_matches)
44     \   && (
45     \       (len(l:head_matches) == len(l:markers_matches))
46     \       && (len(l:head_matches) == len(l:message_matches))
47     \   )
48         let l:item = {
49         \   'lnum'    : str2nr(l:head_matches[l:i][1]),
50         \   'col'     : str2nr(l:head_matches[l:i][2]),
51         \   'end_col' : str2nr(l:head_matches[l:i][2]) + len(l:markers_matches[l:i][1])-1,
52         \   'type'    : 'W',
53         \   'code'    : l:head_matches[l:i][3],
54         \   'text'    : l:message_matches[l:i][1]
55         \}
56         call add(l:output, l:item)
57         let l:i+=1
58     endwhile
59
60     return l:output
61 endfunction
62
63 " Define the languagetool linter for a given filetype.
64 " TODO:
65 " - Add language detection settings based on user env (for mothertongue)
66 " - Add fixer
67 " - Add config options for rules
68 function! ale#handlers#languagetool#DefineLinter(filetype) abort
69     call ale#linter#Define(a:filetype, {
70     \   'name': 'languagetool',
71     \   'executable': function('ale#handlers#languagetool#GetExecutable'),
72     \   'command': function('ale#handlers#languagetool#GetCommand'),
73     \   'output_stream': 'stdout',
74     \   'callback': 'ale#handlers#languagetool#HandleOutput',
75     \   'lint_file': 1,
76     \})
77 endfunction