Skip to content

Commit 016787e

Browse files
committed
readability changes in the indent script (#450)
this makes almost no difference to the indenter's effects. I found that our most time consuming indent operation is from the 'skip_expr' which looks at syntax items, removing 'regex' from it seems to help a bit but it still will be slow on long lines
1 parent f39eb1f commit 016787e

File tree

1 file changed

+41
-77
lines changed

1 file changed

+41
-77
lines changed

indent/javascript.vim

+41-77
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,17 @@ endfunction
117117

118118
" Find line above 'lnum' that isn't empty, in a comment, or in a string.
119119
function s:PrevNonBlankNonString(lnum)
120-
let in_block = 0
121120
let lnum = prevnonblank(a:lnum)
122121
while lnum > 0
123-
" Go in and out of blocks comments as necessary.
124-
" If the line isn't empty (with opt. comment) or in a string, end search.
125122
let line = getline(lnum)
126-
if s:IsInMultilineComment(lnum, matchend(line, '^\s*/\*') - 1) && line !~ s:line_pre . '$'
127-
if in_block
128-
let in_block = 0
129-
else
130-
break
131-
endif
132-
elseif !in_block && s:IsInMultilineComment(lnum, match(line, '\*/\s*$') + 1) && line !~ s:line_pre . '$'
133-
let in_block = 1
134-
elseif !in_block && line !~ s:line_pre . '\%(//\).*$' && !(s:IsInStringOrComment(lnum, 1) && s:IsInStringOrComment(lnum, strlen(line)))
123+
let com = match(line, '\*\/') + 1
124+
if s:IsInMultilineComment(lnum, com)
125+
call cursor(lnum, com)
126+
let parlnum = search('\/\*', 'nbW')
127+
if parlnum > 0
128+
let lnum = parlnum
129+
end
130+
elseif line !~ '^' . s:line_term
135131
break
136132
endif
137133
let lnum = prevnonblank(lnum - 1)
@@ -148,11 +144,9 @@ function s:GetMSL(lnum, in_one_line_scope)
148144
" If we have a continuation line, or we're in a string, use line as MSL.
149145
" Otherwise, terminate search as we have found our MSL already.
150146
let line = getline(lnum)
151-
let col = match(line, s:continuation_regex) + 1
152-
let coal = match(line, s:comma_last) + 1
153147
let line2 = getline(msl)
154148
let col2 = matchend(line2, ')')
155-
if ((col > 0 && !s:IsInStringOrComment(lnum, col) || coal > 0 && !s:IsInStringOrComment(lnum,coal)) &&
149+
if ((s:Match(lnum,s:continuation_regex) || s:Match(lnum, s:comma_last)) &&
156150
\ !s:Match(lnum, s:expr_case)) || s:IsInString(lnum, strlen(line))
157151
let msl = lnum
158152

@@ -201,8 +195,9 @@ function s:InMultiVarStatement(lnum, cont, prev)
201195
" if the line is a js keyword
202196
if a:cont
203197
call cursor(lnum,1)
204-
if searchpair('{', '', '}', 'bW', s:skip_expr) > 0
205-
let lnum = line('.')
198+
let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr)
199+
if parlnum > 0
200+
let lnum = parlnum
206201
end
207202
end
208203
if s:Match(lnum, s:js_keywords)
@@ -225,27 +220,6 @@ function s:InMultiVarStatement(lnum, cont, prev)
225220
return 0
226221
endfunction
227222

228-
" Find line above with beginning of the var statement or returns 0 if it's not"{{{2
229-
" this statement
230-
" function s:GetVarIndent(lnum)
231-
" let lvar = s:InMultiVarStatement(a:lnum, 0,0)
232-
" let prev_lnum = s:PrevNonBlankNonString(a:lnum - 1)
233-
234-
" if lvar
235-
" let line = s:RemoveTrailingComments(getline(prev_lnum))
236-
237-
" " if the previous line doesn't end in a comma, return to regular indent
238-
" if (line !~ s:comma_last)
239-
" return indent(prev_lnum) - s:sw()
240-
" else
241-
" return indent(lvar) + s:sw()
242-
" endif
243-
" endif
244-
245-
" return -1
246-
" endfunction"}}}
247-
248-
249223
" Check if line 'lnum' has more opening brackets than closing ones.
250224
function s:LineHasOpeningBrackets(lnum)
251225
let open_0 = 0
@@ -345,15 +319,14 @@ function GetJavascriptIndent()
345319
let line = getline(v:lnum)
346320
" previous nonblank line number
347321
let prevline = prevnonblank(v:lnum - 1)
348-
322+
349323
" to not change multiline string values
350-
if synIDattr(synID(v:lnum, 1, 1), 'name') =~? 'string\|template' && line !~ s:line_pre . '[''"`]'
351-
return indent(v:lnum)
324+
if line !~ '^[''"`]' && synIDattr(synID(v:lnum, 1, 1), 'name') =~? 'string\|template'
325+
return -1
352326
endif
353327

354328
" If we are in a multi-line comment, cindent does the right thing.
355-
if s:IsInMultilineComment(v:lnum, 1) && !s:IsLineComment(v:lnum, 1) &&
356-
\ s:IsInMultilineComment(v:lnum, match(line, '\s*$')) && line !~ '^\/\*'
329+
if s:IsInMultilineComment(v:lnum, 1) && line !~ '^\/\*'
357330
return cindent(v:lnum)
358331
endif
359332

@@ -375,45 +348,47 @@ function GetJavascriptIndent()
375348
call cursor(v:lnum, col)
376349

377350

378-
let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
379-
if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0
380-
let ind = s:InMultiVarStatement(line('.'), 0, 0) ? indent(line('.')) : indent(s:GetMSL(line('.'), 0))
351+
let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr)
352+
if parlnum > 0
353+
let ind = s:InMultiVarStatement(parlnum, 0, 0) ? indent(parlnum) : indent(s:GetMSL(parlnum, 0))
381354
endif
382355
return ind
383356
endif
384357

358+
let lnum = s:PrevNonBlankNonString(v:lnum - 1)
359+
385360
" If line starts with an operator...
386361
if (line =~ s:operator_first)
387-
if (s:Match(prevline, s:operator_first))
362+
if (s:Match(lnum, s:operator_first))
388363
" and so does previous line, don't indent
389-
return indent(prevline)
364+
return indent(lnum)
390365
end
391-
let counts = s:LineHasOpeningBrackets(prevline)
366+
let counts = s:LineHasOpeningBrackets(lnum)
392367
if counts[0] == '2' || counts[1] == '2' || counts[2] == '2'
393-
call cursor(prevline, 1)
368+
call cursor(lnum, 1)
394369
" Search for the opening tag
395-
let bs = strpart('(){}[]', stridx(')}]', line[col - 1]) * 2, 2)
396-
if searchpair(escape(bs[0], '\['), '', bs[1], 'bW', s:skip_expr) > 0 && s:Match(line('.'), s:operator_first)
397-
return indent(line('.'))
370+
let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr)
371+
if parlnum > 0 && s:Match(parlnum, s:operator_first)
372+
return indent(parlnum)
398373
end
399374
elseif counts[0] != '1' && counts[1] != '1' && counts[2] != '1'
400375
" otherwise, indent 1 level
401-
return indent(prevline) + s:sw()
376+
return indent(lnum) + s:sw()
402377
end
403378

404379
" If previous line starts with an operator...
405-
elseif (s:Match(prevline, s:operator_first) && !s:Match(prevline,s:continuation_regex))||getline(prevline) =~ ');\=' . s:line_term
406-
let counts = s:LineHasOpeningBrackets(prevline)
407-
if counts[0] == '2' && !s:Match(prevline, s:operator_first)
408-
call cursor(prevline, 1)
380+
elseif (s:Match(lnum, s:operator_first) && !s:Match(lnum,s:continuation_regex))||getline(lnum) =~ ');\=' . s:line_term
381+
let counts = s:LineHasOpeningBrackets(lnum)
382+
if counts[0] == '2' && !s:Match(lnum, s:operator_first)
383+
call cursor(lnum, 1)
409384
" Search for the opening tag
410-
let mnum = searchpair('(', '', ')', 'bW', s:skip_expr)
385+
let mnum = searchpair('(', '', ')', 'nbW', s:skip_expr)
411386
if mnum > 0 && s:Match(mnum, s:operator_first)
412387
return indent(mnum) - s:sw()
413388
end
414-
elseif s:Match(prevline, s:operator_first)
389+
elseif s:Match(lnum, s:operator_first)
415390
if counts[0] != '1' && counts[1] != '1' && counts[2] != '1'
416-
return indent(prevline) - s:sw()
391+
return indent(lnum) - s:sw()
417392
end
418393
end
419394
end
@@ -429,7 +404,6 @@ function GetJavascriptIndent()
429404
endif
430405

431406
" Find a non-blank, non-multi-line string line above the current line.
432-
let lnum = s:PrevNonBlankNonString(v:lnum - 1)
433407

434408
" If the line is empty and inside a string, use the previous line.
435409
if line =~ '^\s*$' && lnum != prevline
@@ -454,23 +428,13 @@ function GetJavascriptIndent()
454428
" add indent depending on the bracket type.
455429
if s:Match(lnum, '[[({})\]]')
456430
let counts = s:LineHasOpeningBrackets(lnum)
457-
if counts[0] == '2'
458-
call cursor(lnum, 1)
459-
" Search for the opening tag
460-
if searchpair('(', '', ')', 'bW', s:skip_expr) > 0
461-
return indent(s:GetMSL(line('.'), 0))
462-
end
463-
elseif counts[1] == '2' && !s:Match(lnum, s:line_pre . '}')
464-
call cursor(lnum, 1)
465-
" Search for the opening tag
466-
if searchpair('{', '', '}', 'bW', s:skip_expr) > 0
467-
return indent(s:GetMSL(line('.'), 0))
468-
end
469-
elseif counts[2] == '2' && !s:Match(lnum, s:line_pre . ']')
431+
if counts[0] == '2' || (counts[1] == '2' && !s:Match(lnum, s:line_pre . '}')) ||
432+
\ (counts[2] == '2' && !s:Match(lnum, s:line_pre . ']'))
470433
call cursor(lnum, 1)
471434
" Search for the opening tag
472-
if searchpair('\[', '', '\]', 'bW', s:skip_expr) > 0
473-
return indent(s:GetMSL(line('.'), 0))
435+
let parlnum = searchpair('(\|{\|\[', '', ')\|}\|\]', 'nbW', s:skip_expr)
436+
if parlnum > 0
437+
return indent(s:GetMSL(parlnum, 0))
474438
end
475439
elseif counts[1] == '1' || counts[2] == '1' || counts[0] == '1' || s:Onescope(lnum)
476440
return ind + s:sw()

0 commit comments

Comments
 (0)