/
jslint.vim
executable file
·272 lines (233 loc) · 6.95 KB
/
jslint.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
" Global Options
"
" Enable/Disable highlighting of errors in source.
" Default is Enable
" To disable the highlighting put the line
" let g:JSLintHighlightErrorLine = 0
" in your .vimrc
"
if exists("b:did_jslint_plugin")
finish
else
let b:did_jslint_plugin = 1
endif
let s:install_dir = expand('<sfile>:p:h')
au BufLeave <buffer> call s:JSLintClear()
au BufEnter <buffer> call s:JSLint()
au InsertLeave <buffer> call s:JSLint()
"au InsertEnter <buffer> call s:JSLint()
au BufWritePost <buffer> call s:JSLint()
" due to http://tech.groups.yahoo.com/group/vimdev/message/52115
if(!has("win32") || v:version>702)
au CursorHold <buffer> call s:JSLint()
au CursorHoldI <buffer> call s:JSLint()
au CursorHold <buffer> call s:GetJSLintMessage()
endif
au CursorMoved <buffer> call s:GetJSLintMessage()
if !exists("g:JSLintHighlightErrorLine")
let g:JSLintHighlightErrorLine = 1
endif
if !exists("*s:JSLintUpdate")
function s:JSLintUpdate()
silent call s:JSLint()
call s:GetJSLintMessage()
endfunction
endif
if !exists(":JSLintUpdate")
command JSLintUpdate :call s:JSLintUpdate()
endif
if !exists(":JSLintToggle")
command JSLintToggle :let b:jslint_disabled = exists('b:jslint_disabled') ? b:jslint_disabled ? 0 : 1 : 1
endif
noremap <buffer><silent> dd dd:JSLintUpdate<CR>
noremap <buffer><silent> dw dw:JSLintUpdate<CR>
noremap <buffer><silent> u u:JSLintUpdate<CR>
noremap <buffer><silent> <C-R> <C-R>:JSLintUpdate<CR>
" Set up command and parameters
if has("win32")
let s:cmd = 'cscript /NoLogo '
let s:runjslint_ext = 'wsf'
else
let s:runjslint_ext = 'js'
if exists("$JS_CMD")
let s:cmd = "$JS_CMD"
elseif executable('/System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc')
let s:cmd = '/System/Library/Frameworks/JavaScriptCore.framework/Resources/jsc'
elseif executable('node')
let s:cmd = 'node'
elseif executable('js')
let s:cmd = 'js'
else
echoerr('No JS interpreter found. Checked for jsc, js (spidermonkey), and node')
endif
endif
let s:plugin_path = s:install_dir . "/jslint/"
if has('win32')
let s:plugin_path = substitute(s:plugin_path, '/', '\', 'g')
endif
let s:cmd = "cd " . s:plugin_path . " && " . s:cmd . " " . s:plugin_path . "runjslint." . s:runjslint_ext
let s:jslintrc_file = expand('~/.jslintrc')
if filereadable(s:jslintrc_file)
let s:jslintrc = readfile(s:jslintrc_file)
else
let s:jslintrc = []
end
" WideMsg() prints [long] message up to (&columns-1) length
" guaranteed without "Press Enter" prompt.
if !exists("*s:WideMsg")
function s:WideMsg(msg)
let x=&ruler | let y=&showcmd
set noruler noshowcmd
redraw
echo a:msg
let &ruler=x | let &showcmd=y
endfun
endif
function! s:JSLintClear()
" Delete previous matches
let s:matches = getmatches()
for s:matchId in s:matches
if s:matchId['group'] == 'JSLintError'
call matchdelete(s:matchId['id'])
endif
endfor
let b:matched = []
let b:matchedlines = {}
let b:cleared = 1
endfunction
function! s:JSLint()
if exists("b:jslint_disabled") && b:jslint_disabled == 1
return
endif
highlight link JSLintError SpellBad
if exists("b:cleared")
if b:cleared == 0
call s:JSLintClear()
endif
let b:cleared = 1
endif
let b:matched = []
let b:matchedlines = {}
" Detect range
if a:firstline == a:lastline
" Skip a possible shebang line, e.g. for node.js script.
if getline(1)[0:1] == "#!"
let b:firstline = 2
else
let b:firstline = 1
endif
let b:lastline = '$'
else
let b:firstline = a:firstline
let b:lastline = a:lastline
endif
let b:qf_list = []
let b:qf_window_count = -1
let lines = join(s:jslintrc + getline(b:firstline, b:lastline), "\n")
if len(lines) == 0
return
endif
let b:jslint_output = system(s:cmd, lines . "\n")
if v:shell_error
echoerr 'could not invoke JSLint!'
let b:jslint_disabled = 1
end
for error in split(b:jslint_output, "\n")
" Match {line}:{char}:{message}
let b:parts = matchlist(error, '\v(\d+):(\d+):([A-Z]+):(.*)')
if !empty(b:parts)
let l:line = b:parts[1] + (b:firstline - 1 - len(s:jslintrc)) " Get line relative to selection
let l:errorMessage = b:parts[4]
if l:line < 1
echoerr 'error in jslintrc, line ' . b:parts[1] . ', character ' . b:parts[2] . ': ' . l:errorMessage
else
" Store the error for an error under the cursor
let s:matchDict = {}
let s:matchDict['lineNum'] = l:line
let s:matchDict['message'] = l:errorMessage
let b:matchedlines[l:line] = s:matchDict
if b:parts[3] == 'ERROR'
let l:errorType = 'E'
else
let l:errorType = 'W'
endif
if g:JSLintHighlightErrorLine == 1
let s:mID = matchadd('JSLintError', '\v%' . l:line . 'l\S.*(\S|$)')
endif
" Add line to match list
call add(b:matched, s:matchDict)
" Store the error for the quickfix window
let l:qf_item = {}
let l:qf_item.bufnr = bufnr('%')
let l:qf_item.filename = expand('%')
let l:qf_item.lnum = l:line
let l:qf_item.text = l:errorMessage
let l:qf_item.type = l:errorType
" Add line to quickfix list
call add(b:qf_list, l:qf_item)
endif
endif
endfor
if exists("s:jslint_qf")
" if jslint quickfix window is already created, reuse it
call s:ActivateJSLintQuickFixWindow()
call setqflist(b:qf_list, 'r')
else
" one jslint quickfix window for all buffers
call setqflist(b:qf_list, '')
let s:jslint_qf = s:GetQuickFixStackCount()
endif
let b:cleared = 0
endfunction
let b:showing_message = 0
if !exists("*s:GetJSLintMessage")
function s:GetJSLintMessage()
let s:cursorPos = getpos(".")
" Bail if RunJSLint hasn't been called yet
if !exists('b:matchedlines')
return
endif
if has_key(b:matchedlines, s:cursorPos[1])
let s:jslintMatch = get(b:matchedlines, s:cursorPos[1])
call s:WideMsg(s:jslintMatch['message'])
let b:showing_message = 1
return
endif
if b:showing_message == 1
echo
let b:showing_message = 0
endif
endfunction
endif
if !exists("*s:GetQuickFixStackCount")
function s:GetQuickFixStackCount()
let l:stack_count = 0
try
silent colder 9
catch /E380:/
endtry
try
for i in range(9)
silent cnewer
let l:stack_count = l:stack_count + 1
endfor
catch /E381:/
return l:stack_count
endtry
endfunction
endif
if !exists("*s:ActivateJSLintQuickFixWindow")
function s:ActivateJSLintQuickFixWindow()
try
silent colder 9 " go to the bottom of quickfix stack
catch /E380:/
endtry
if s:jslint_qf > 0
try
exe "silent cnewer " . s:jslint_qf
catch /E381:/
echoerr "Could not activate JSLint Quickfix Window."
endtry
endif
endfunction
endif