forked from torik42/YaLafi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlty.vim
145 lines (138 loc) · 5.21 KB
/
lty.vim
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"
" Author: Matthias Baumann
" Description: ALE linter, LanguageTool with YaLafi as LaTeX filter
"
" possible improvements:
" - pipe buffer content to stdin (currently: temp file)
" --> yalafi.shell: add reading from stdin
"
" home directory of LT software
call ale#Set('tex_lty_ltdirectory', '~/lib/LanguageTool')
" alternative LT command
call ale#Set('tex_lty_ltcommand', '')
" use an LT server?
call ale#Set('tex_lty_server', 'my')
" language code passed to LT
call ale#Set('tex_lty_language', 'en-GB')
" LT option --disable
call ale#Set('tex_lty_disable', 'WHITESPACE_RULE')
" LT option --enable
call ale#Set('tex_lty_enable', '')
" LT option --disablecategories
call ale#Set('tex_lty_disablecategories', '')
" LT option --enablecategories
call ale#Set('tex_lty_enablecategories', '')
" further options passed to yalafi.shell
call ale#Set('tex_lty_shelloptions', '')
" displayed in status line
call ale#Set('tex_lty_code', 'LT')
call ale#Set('tex_lty_executable', 'python')
call ale#Set('tex_lty_options',
\ ' -m yalafi.shell'
\ . ' --output json'
\ . ' --lt-command "' . g:ale_tex_lty_ltcommand . '"'
\ . (g:ale_tex_lty_ltcommand != '' ?
\ '' : ' --lt-directory ' . g:ale_tex_lty_ltdirectory)
\ . (g:ale_tex_lty_server == '' ?
\ '' : ' --server ' . g:ale_tex_lty_server)
\ . ' --language ' . g:ale_tex_lty_language
\ . ' --disable "' . g:ale_tex_lty_disable . '"'
\ . ' --enable "' . g:ale_tex_lty_enable . '"'
\ . ' --disablecategories "' . g:ale_tex_lty_disablecategories . '"'
\ . ' --enablecategories "' . g:ale_tex_lty_enablecategories . '"'
\ . ' ' . g:ale_tex_lty_shelloptions
\)
function! ale_linters#tex#lty#GetExecutable(buffer) abort
return ale#Var(a:buffer, 'tex_lty_executable')
endfunction
function! ale_linters#tex#lty#GetCommand(buffer) abort
let l:executable = ale_linters#tex#lty#GetExecutable(a:buffer)
let l:options = ale#Var(a:buffer, 'tex_lty_options')
return ale#Escape(l:executable)
\ . (empty(l:options) ? '' : ' ' . l:options) . ' %t'
endfunction
function! s:check_installation(buffer) abort
let l:pref = 'In order to use the tex/lty linter, please '
if !executable('python')
return l:pref . 'install Python.'
endif
call system('python -c "import yalafi"')
if v:shell_error != 0
return l:pref . 'install the Python module YaLafi.'
endif
if ale#Var(a:buffer, 'tex_lty_server') != 'lt'
if !executable('java')
return l:pref . 'install Java.'
endif
if ale#Var(a:buffer, 'tex_lty_ltcommand') != ''
if !executable(ale#Var(a:buffer, 'tex_lty_ltcommand'))
return l:pref . 'set g:ale_tex_lty_ltcommand (or similar) correctly.'
endif
else
if !filereadable(fnamemodify(ale#Var(a:buffer, 'tex_lty_ltdirectory')
\ . '/languagetool-commandline.jar', ':p'))
return l:pref . 'set g:ale_tex_lty_ltdirectory (or similar)'
\ . ' to the path of LanguageTool.'
endif
endif
endif
return ''
endfunction
function! ale_linters#tex#lty#Handle(buffer, lines) abort
try
let l:report = json_decode(a:lines[0])
catch
let l:err = s:check_installation(a:buffer)
if l:err == ''
let l:err = 'Unknown error with linter tex/lty.'
endif
return [{
\ 'lnum': 1,
\ 'col': 1,
\ 'code': 'ERROR',
\ 'text': l:err,
\ 'detail': l:err
\}]
endtry
let l:output = []
for l:match in l:report['matches']
let l:lin = l:match['priv']['fromy'] + 1
let l:col = l:match['priv']['fromx'] + 1
let l:rule = l:match['rule']['id']
\ . (has_key(l:match['rule'], 'subId') ?
\ '[' . l:match['rule']['subId'] . ']': '')
let l:replacements = []
for l:repl in l:match['replacements']
call add(l:replacements, l:repl['value'])
endfor
let l:context = l:match['context']
let l:mark = repeat(' ', l:context['offset'])
\ . repeat('^', l:context['length'])
let l:detail = 'Line ' . l:lin . ', column ' . l:col
\ . ', rule ID ' . l:rule . "\n"
\ . 'Message: ' . l:match['message'] . "\n"
\ . 'Suggestion: ' . join(l:replacements, '; ') . "\n"
\ . l:context['text'] . "\n"
\ . l:mark . "\n"
call add(l:output, {
\ 'lnum' : l:lin,
\ 'col' : l:col,
\ 'end_lnum': l:match['priv']['toy'] + 1,
\ 'end_col' : l:match['priv']['tox'],
\ 'vcol' : 1,
\ 'code' : ale#Var(a:buffer, 'tex_lty_code'),
\ 'text' : l:match['message'],
\ 'detail' : l:detail,
\})
endfor
return l:output
endfunction
call ale#linter#Define('tex', {
\ 'name': 'lty',
\ 'executable': function('ale_linters#tex#lty#GetExecutable'),
\ 'command': function('ale_linters#tex#lty#GetCommand'),
\ 'output_stream': 'stdout',
\ 'callback': 'ale_linters#tex#lty#Handle',
\ 'lint_file': 0,
\ 'read_buffer': 0,
\})