Skip to content

Commit 8274830

Browse files
lacygoillchrisbra
authored andcommitted
runtime(vim): fix various indentation issues
fixes: #15123 closes: #17849 Signed-off-by: lacygoill <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 1a2c493 commit 8274830

3 files changed

Lines changed: 75 additions & 36 deletions

File tree

runtime/autoload/dist/vimindent.vim

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ vim9script
22

33
# Language: Vim script
44
# Maintainer: github user lacygoill
5-
# Last Change: 2025 Apr 13
5+
# Last Change: 2025 Jul 25
66
#
77
# Includes changes from The Vim Project:
8-
# - 2024 Feb 09: Fix indent after literal Dict (A. Radev via #13966)
9-
# - 2024 Nov 08: Fix indent after :silent! function (D. Kearns via #16009)
10-
# - 2024 Dec 26: Fix indent for enums (Jim Zhou via #16293)
118

129
# NOTE: Whenever you change the code, make sure the tests are still passing:
1310
#
@@ -23,9 +20,8 @@ def IndentMoreInBracketBlock(): number # {{{2
2320
if get(g:, 'vim_indent', {})
2421
->get('more_in_bracket_block', false)
2522
return shiftwidth()
26-
else
27-
return 0
2823
endif
24+
return 0
2925
enddef
3026

3127
def IndentMoreLineContinuation(): number # {{{2
@@ -35,9 +31,8 @@ def IndentMoreLineContinuation(): number # {{{2
3531

3632
if n->typename() == 'string'
3733
return n->eval()
38-
else
39-
return n
4034
endif
35+
return n
4136
enddef
4237
# }}}2
4338

@@ -145,7 +140,7 @@ const HEREDOC_OPERATOR: string = '\s=<<\s\@=\%(\s\+\%(trim\|eval\)\)\{,2}'
145140

146141
# A better regex would be:
147142
#
148-
# [^-+*/%.:# \t[:alnum:]\"|]\@=.\|->\@!\%(=\s\)\@!\|[+*/%]\%(=\s\)\@!
143+
# [^-+*/%.:#[:blank:][:alnum:]\"|]\|->\@!\%(=\s\)\@!\|[+*/%]\%(=\s\)\@!
149144
#
150145
# But sometimes, it can be too costly and cause `E363` to be given.
151146
const PATTERN_DELIMITER: string = '[-+*/%]\%(=\s\)\@!'
@@ -193,10 +188,9 @@ const MODIFIERS: dict<string> = {
193188
patterns =<< trim eval END
194189
argdo\>!\=
195190
bufdo\>!\=
196-
cdo\>!\=
191+
[cl]f\=do\>!\=
197192
folddoc\%[losed]\>
198193
foldd\%[oopen]\>
199-
ldo\=\>!\=
200194
tabdo\=\>
201195
windo\>
202196
au\%[tocmd]\>!\=.*
@@ -290,9 +284,9 @@ patterns = []
290284
for kwds: list<string> in BLOCKS
291285
for kwd: string in kwds[0 : -2]
292286
if MODIFIERS->has_key(kwd->Unshorten())
293-
patterns += [$'\%({MODIFIERS[kwd]}\)\={kwd}']
287+
patterns->add($'\%({MODIFIERS[kwd]}\)\={kwd}')
294288
else
295-
patterns += [kwd]
289+
patterns->add(kwd)
296290
endif
297291
endfor
298292
endfor
@@ -348,7 +342,8 @@ patterns =<< trim eval END
348342
{'\'}<argd\%[elete]\s\+\*\s*$
349343
\<[lt]\=cd!\=\s\+-\s*$
350344
\<norm\%[al]!\=\s*\S\+$
351-
\%(\<sil\%[ent]!\=\s\+\)\=\<[nvxsoilct]\=\%(nore\|un\)map!\=\s
345+
\%(\<sil\%[ent]!\=\s\+\)\=\<[nvxsoilct]\=\%(nore\|un\)\=map!\=\s
346+
\<set\%(\%[global]\|\%[local]\)\>.*,$
352347
{PLUS_MINUS_COMMAND}
353348
END
354349

@@ -430,6 +425,9 @@ export def Expr(lnum = v:lnum): number # {{{2
430425
elseif line_A.lnum->IsRightBelow('HereDoc')
431426
var ind: number = b:vimindent.startindent
432427
unlet! b:vimindent
428+
if line_A.text =~ ENDS_BLOCK_OR_CLAUSE
429+
return ind - shiftwidth()
430+
endif
433431
return ind
434432
endif
435433

@@ -444,9 +442,8 @@ export def Expr(lnum = v:lnum): number # {{{2
444442
if line_A.text =~ BACKSLASH_AT_SOL
445443
if line_B.text =~ BACKSLASH_AT_SOL
446444
return Indent(line_B.lnum)
447-
else
448-
return Indent(line_B.lnum) + IndentMoreLineContinuation()
449445
endif
446+
return Indent(line_B.lnum) + IndentMoreLineContinuation()
450447
endif
451448

452449
if line_A->AtStartOf('FuncHeader')
@@ -459,9 +456,8 @@ export def Expr(lnum = v:lnum): number # {{{2
459456
unlet! b:vimindent
460457
if line_A.text =~ ENDS_FUNCTION
461458
return startindent
462-
else
463-
return startindent + shiftwidth()
464459
endif
460+
return startindent + shiftwidth()
465461
endif
466462

467463
var past_bracket_block: dict<any>
@@ -542,8 +538,9 @@ export def Expr(lnum = v:lnum): number # {{{2
542538

543539
if line_B.text =~ STARTS_CURLY_BLOCK
544540
return Indent(line_B.lnum) + shiftwidth() + IndentMoreInBracketBlock()
541+
endif
545542

546-
elseif line_A.text =~ CLOSING_BRACKET_AT_SOL
543+
if line_A.text =~ CLOSING_BRACKET_AT_SOL
547544
var start: number = MatchingOpenBracket(line_A)
548545
if start <= 0
549546
return -1
@@ -565,9 +562,8 @@ export def Expr(lnum = v:lnum): number # {{{2
565562
var block_start: number = SearchPairStart(start, middle, end)
566563
if block_start > 0
567564
return Indent(block_start)
568-
else
569-
return -1
570565
endif
566+
return -1
571567
endif
572568

573569
var base_ind: number
@@ -591,8 +587,7 @@ export def Expr(lnum = v:lnum): number # {{{2
591587
endif
592588
endif
593589

594-
var ind: number = base_ind + Offset(line_A, line_B)
595-
return [ind, 0]->max()
590+
return base_ind + Offset(line_A, line_B)
596591
enddef
597592

598593
def g:GetVimIndent(): number # {{{2
@@ -611,29 +606,31 @@ def Offset( # {{{2
611606
if line_B->AtStartOf('FuncHeader')
612607
&& IsInInterface()
613608
return 0
609+
endif
614610

615611
# increase indentation inside a block
616-
elseif line_B.text =~ STARTS_NAMED_BLOCK
612+
if line_B.text =~ STARTS_NAMED_BLOCK
617613
|| line_B->EndsWithCurlyBlock()
618614
# But don't indent if the line starting the block also closes it.
619615
if line_B->AlsoClosesBlock()
620616
return 0
617+
endif
621618
# Indent twice for a line continuation in the block header itself, so that
622619
# we can easily distinguish the end of the block header from the start of
623620
# the block body.
624-
elseif (line_B->EndsWithLineContinuation()
621+
if (line_B->EndsWithLineContinuation()
625622
&& !line_A.isfirst)
626623
|| (line_A.text =~ LINE_CONTINUATION_AT_SOL
627624
&& line_A.text !~ PLUS_MINUS_COMMAND)
628625
|| line_A.text->Is_IN_KeywordForLoop(line_B.text)
629626
return 2 * shiftwidth()
630-
else
631-
return shiftwidth()
632627
endif
628+
return shiftwidth()
629+
endif
633630

634631
# increase indentation of a line if it's the continuation of a command which
635632
# started on a previous line
636-
elseif !line_A.isfirst
633+
if !line_A.isfirst
637634
&& (line_B->EndsWithLineContinuation()
638635
|| line_A.text =~ LINE_CONTINUATION_AT_SOL)
639636
&& !(line_B->EndsWithComma() && line_A.lnum->IsInside('EnumBlock'))
@@ -653,12 +650,11 @@ def HereDocIndent(line_A: string): number # {{{2
653650
# will need to be indented relative to the start of the heredoc. It
654651
# must know where it starts; it needs the cache.
655652
return 0
656-
else
657-
var ind: number = b:vimindent.startindent
658-
# invalidate the cache so that it's not used for the next heredoc
659-
unlet! b:vimindent
660-
return ind
661653
endif
654+
var ind: number = b:vimindent.startindent
655+
# invalidate the cache so that it's not used for the next heredoc
656+
unlet! b:vimindent
657+
return ind
662658
endif
663659

664660
# In a non-trimmed heredoc, all of leading whitespace is semantic.
@@ -700,7 +696,7 @@ def HereDocIndent(line_A: string): number # {{{2
700696
b:vimindent.startindent = new_startindent
701697
endif
702698

703-
return [0, Indent(v:lnum) + b:vimindent.offset]->max()
699+
return Indent(v:lnum) + b:vimindent.offset
704700
enddef
705701

706702
def CommentIndent(): number # {{{2
@@ -727,9 +723,8 @@ def CommentIndent(): number # {{{2
727723
endif
728724
if getline(next) =~ ENDS_BLOCK
729725
return ind + shiftwidth()
730-
else
731-
return ind
732726
endif
727+
return ind
733728
enddef
734729

735730
def BracketBlockIndent(line_A: dict<any>, block: dict<any>): number # {{{2

runtime/indent/testdir/vim.in

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,25 @@ silent! function Bar()
196196
return 42
197197
endfunction
198198
" END_INDENT
199+
200+
" START_INDENT
201+
if true
202+
nmap xxx,
203+
else
204+
endif
205+
" END_INDENT
206+
207+
" START_INDENT
208+
if true
209+
var heredoc =<< END
210+
foo
211+
bar
212+
baz
213+
END
214+
endif
215+
" END_INDENT
216+
217+
" START_INDENT
218+
set path=.,,
219+
set clipboard=unnamed,unnamedplus
220+
" END_INDENT

runtime/indent/testdir/vim.ok

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,25 @@ silent! function Bar()
196196
return 42
197197
endfunction
198198
" END_INDENT
199+
200+
" START_INDENT
201+
if true
202+
nmap xxx,
203+
else
204+
endif
205+
" END_INDENT
206+
207+
" START_INDENT
208+
if true
209+
var heredoc =<< END
210+
foo
211+
bar
212+
baz
213+
END
214+
endif
215+
" END_INDENT
216+
217+
" START_INDENT
218+
set path=.,,
219+
set clipboard=unnamed,unnamedplus
220+
" END_INDENT

0 commit comments

Comments
 (0)