forked from kchmck/vim-coffee-script
/
coffee.vim
284 lines (232 loc) · 8.54 KB
/
coffee.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
272
273
274
275
276
277
278
279
280
281
282
283
284
" Language: CoffeeScript
" Maintainer: Mick Koch <kchmck@gmail.com>
" URL: http://github.com/kchmck/vim-coffee-script
" License: WTFPL
if exists("b:did_indent")
finish
endif
let b:did_indent = 1
setlocal autoindent
setlocal indentexpr=GetCoffeeIndent(v:lnum)
" Make sure GetCoffeeIndent is run when these are typed so they can be
" indented or outdented.
setlocal indentkeys+=0],0),0.,=else,=when,=catch,=finally
" Only define the function once.
if exists("*GetCoffeeIndent")
finish
endif
" Join a list of regexs as branches.
function! s:RegexJoin(regexes)
return join(a:regexes, '\|')
endfunction
" Create a regex group from a list of regexes.
function! s:RegexGroup(...)
return '\%(' . s:RegexJoin(a:000) . '\)'
endfunction
" Outdent certain keywords and brackets.
let s:outdent = '^'
\ . s:RegexGroup('else', 'when', 'catch', 'finally', ']', '}', ')')
" Indent after certain keywords.
let s:indent_after_keywords = '^'
\ . s:RegexGroup('if', 'unless', 'else', 'for',
\ 'while', 'until', 'loop', 'switch',
\ 'when', 'try', 'catch', 'finally',
\ 'class')
\ . '\>'
" Indent after brackets, functions, and assignments.
let s:indent_after_literals = s:RegexGroup('\[', '{', '(', '->', '=>', ':', '=')
\ . '$'
" Combine the two regexes above.
let s:indent_after = s:RegexJoin([s:indent_after_keywords,
\ s:indent_after_literals])
" Indent after operators at the end of lines.
let s:continuations = s:RegexGroup('-\@<!>', '=\@<!>', '-\@<!-', '+\@<!+',
\ '<', '\*', '/', '%', '|', '&', ',',
\ '\.\@<!\.', 'is', 'isnt', 'and', 'or')
\ . '$'
" Indent after certain keywords used as multi-line assignments.
let s:assignment_keywords = s:RegexGroup(':', '=')
\ . '\s*\<'
\ . s:RegexGroup('if', 'unless', 'for', 'while',
\ 'until', 'switch', 'try', 'class')
\ . '\>'
" Outdent after certain keywords.
let s:outdent_after = '^'
\ . s:RegexGroup('return', 'break', 'continue', 'throw')
\ . '\>'
" Don't outdent if the line contains one of these keywords (for cases like
" 'return if a is b', 'break unless a', etc.)
let s:dont_outdent_after = '\<' . s:RegexGroup('if', 'unless') . '\>'
" Max lines to look back for a match
let s:max_lookback = 50
" Check for a single-line statement (e.g., 'if a then b'), which doesn't need an
" indent afterwards.
function! s:IsSingleLineStatement(line)
" The 'then' keyword is usually a good hint.
return a:line =~ '\<then\>'
endfunction
" Check for a single-line 'else' statement (e.g., 'else return a' but
" not 'else if a'), which doesn't need an indent afterwards.
function! s:IsSingleLineElse(line)
" Check if the line actually starts with 'else', then if the line contains
" anything other than 'else', then finally if the line is actually an 'else'
" statement rather than an 'else if' or 'else unless' statement.
return a:line =~ '^else\>'
\ && a:line !~ '^else$'
\ && a:line !~ '^else if\>'
\ && a:line !~ '^else unless\>'
endfunction
" Check if a 'when' statement is the first in a switch block by searching the
" previous line for the 'switch' keyword. The first 'when' shouldn't be
" outdented.
function! s:IsFirstWhen(curline, prevline)
return a:curline =~ '^when\>' && a:prevline =~ '\<switch\>'
endfunction
" Check for a multi-line assignment like
" a = if b
" c
" else
" d
function! s:IsMultiLineAssignment(line)
return a:line =~ s:assignment_keywords
endfunction
" Get the linked syntax name of some text.
function! s:SyntaxName(line, col)
return synIDattr(synIDtrans(synID(a:line, a:col, 1)), 'name')
endfunction
" Check if some text is a comment or string.
function! s:IsCommentOrString(line, col)
return s:SyntaxName(a:line, a:col) =~ 'Comment\|Constant'
endfunction
" Crudely check if a line is a comment.
function! s:IsCommentQuick(line)
return a:line =~ '^#'
endfunction
" Check if a line is a dot-access.
function! s:IsDotAccess(line)
return a:line =~ '^\.'
endfunction
" Check if a line is a continuation.
function! s:IsContinuation(line)
return a:line =~ s:continuations
endfunction
function! s:ShouldOutdent(curline, prevline)
return !s:IsSingleLineStatement(a:prevline)
\ && !s:IsFirstWhen(a:curline, a:prevline)
\ && a:prevline !~ s:outdent_after
\ && a:curline =~ s:outdent
endfunction
function! s:ShouldIndent(curline, prevline)
return !s:IsDotAccess(a:prevline) && s:IsDotAccess(a:curline)
endfunction
function! s:ShouldIndentAfter(prevline, prevprevline)
return !s:IsSingleLineStatement(a:prevline)
\ && !s:IsSingleLineElse(a:prevline)
\ && !s:IsCommentQuick(a:prevline)
\
\ && (a:prevline =~ s:indent_after
\ || s:IsMultiLineAssignment(a:prevline)
\
\ || (s:IsContinuation(a:prevline)
\ && !s:IsContinuation(a:prevprevline)
\ && a:prevprevline !~ s:indent_after_literals))
endfunction
function! s:ShouldOutdentAfter(prevline)
return (a:prevline !~ s:dont_outdent_after
\ || s:IsSingleLineStatement(a:prevline))
\ && a:prevline =~ s:outdent_after
endfunction
function! s:ShouldSkip(startlinenum, linenum, col)
let line = s:GetTrimmedLine(a:linenum)
return s:IsCommentOrString(a:linenum, a:col)
\ || (s:IsSingleLineStatement(line)
\ && a:startlinenum - a:linenum > 1)
\ || line =~ '^return'
endfunction
" Find the farthest line to look back to, capped to line 1 (zero and negative
" numbers cause bad things).
function! s:MaxLookback(startlinenum)
return max([1, a:startlinenum - s:max_lookback])
endfunction
" Get the skip expression for searchpair().
function! s:SkipExpr(startlinenum)
return "s:ShouldSkip(" . a:startlinenum . ", line('.'), col('.'))"
endfunction
" Search for pairs of text.
function! s:SearchPair(start, end)
" The cursor must be in the first column for regexes to match.
call cursor(0, 1)
let startlinenum = line('.')
" Don't need the W flag since MaxLookback caps the search to line 1.
return searchpair(a:start, '', a:end, 'bn',
\ s:SkipExpr(startlinenum),
\ s:MaxLookback(startlinenum))
endfunction
" Try to find a previous matching line.
function! s:GetMatch(curline, prevline)
let firstchar = a:curline[0]
if firstchar == '}'
return s:SearchPair('{', '}')
elseif firstchar == ')'
return s:SearchPair('(', ')')
elseif firstchar == ']'
return s:SearchPair('\[', '\]')
elseif a:curline =~ '^else'
return s:SearchPair('\<if\|unless\|when\>', '\<else\>')
elseif a:curline =~ '^catch'
return s:SearchPair('\<try\>', '\<catch\>')
elseif a:curline =~ '^finally'
return s:SearchPair('\<try\>', '\<finally\>')
elseif a:curline =~ '^when' && !s:IsFirstWhen(a:curline, a:prevline)
return s:SearchPair('\<when\>', '\<when\>')
endif
return 0
endfunction
" Get the nearest previous non-blank line.
function! s:GetPrevLineNum(linenum)
return prevnonblank(a:linenum - 1)
endfunction
" Get the contents of a line without leading or trailing whitespace.
function! s:GetTrimmedLine(linenum)
return substitute(substitute(getline(a:linenum), '^\s\+', '', ''),
\ '\s\+$', '', '')
endfunction
function! GetCoffeeIndent(curlinenum)
let prevlinenum = s:GetPrevLineNum(a:curlinenum)
let prevprevlinenum = s:GetPrevLineNum(prevlinenum)
" No indenting is needed at the start of a file.
if prevlinenum == 0
return 0
endif
if s:IsCommentOrString(a:curlinenum, col('.'))
return -1
endif
let curindent = indent(a:curlinenum)
let previndent = indent(prevlinenum)
let curline = s:GetTrimmedLine(a:curlinenum)
let prevline = s:GetTrimmedLine(prevlinenum)
let prevprevline = s:GetTrimmedLine(prevprevlinenum)
let matchlinenum = s:GetMatch(curline, prevline)
if matchlinenum
return indent(matchlinenum)
endif
if s:ShouldIndent(curline, prevline)
return previndent + &shiftwidth
endif
if s:ShouldOutdent(curline, prevline)
" Is the line already outdented?
if curindent < previndent
return curindent
else
return curindent - &shiftwidth
endif
endif
if s:ShouldIndentAfter(prevline, prevprevline)
return previndent + &shiftwidth
endif
if s:ShouldOutdentAfter(prevline)
return previndent - &shiftwidth
endif
" No indenting or outdenting is needed
return -1
endfunction