Skip to content

Commit 9179ddc

Browse files
Cezar Dimoiuchrisbra
authored andcommitted
runtime(yaml): update YAML indentation for mapping keys inside list items
When a list item contains a mapping key (e.g., '- element1:'), the content under that key was incorrectly indented. The indent function was not accounting for the '- ' prefix when calculating indentation for nested content. Example that now works correctly: list: - element1: foo: bar # Now correctly at indent 6, not 4 The fix adds special handling in two places: 1. When previous line ends with ':' and starts with '- ' 2. When looking up previous mapping key that is a list item Fixes indentation to account for the 2-character '- ' prefix. fixes: #18943 closes: #19133 Signed-off-by: Cezar Dimoiu <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent 4895ae8 commit 9179ddc

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

runtime/indent/testdir/yaml.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ map: |
1818
line1
1919
line2
2020
# END_INDENT
21+
22+
# START_INDENT
23+
list:
24+
- element1:
25+
foo: bar
26+
- element2:
27+
foo: bar
28+
# END_INDENT

runtime/indent/testdir/yaml.ok

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ map: |
1818
line1
1919
line2
2020
# END_INDENT
21+
22+
# START_INDENT
23+
list:
24+
- element1:
25+
foo: bar
26+
- element2:
27+
foo: bar
28+
# END_INDENT

runtime/indent/yaml.vim

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
" Last Change: 2022 Jun 17
66
" 2024 Feb 29 by Vim project: disable mulitline indent by default
77
" 2024 Aug 14 by Vim project: fix re-indenting when commenting out lines
8+
" 2026 Jan 08 by Vim project: fix object indentation in array
89

910
" Only load this indent file when no other was loaded.
1011
if exists('b:did_indent')
@@ -114,7 +115,13 @@ function GetYAMLIndent(lnum)
114115
"
115116
" - |-
116117
" Block scalar without indentation indicator
117-
return previndent+shiftwidth()
118+
if prevline =~# '^\s*-\s.*:$'
119+
" Special case: list item with mapping key (- key:)
120+
" Need to account for the "- " prefix
121+
return previndent + 2 + shiftwidth()
122+
else
123+
return previndent+shiftwidth()
124+
endif
118125
elseif prevline =~# '\v[:-]\ [|>]%(\d+[+\-]?|[+\-]?\d+)%(\#.*|\s*)$'
119126
" - |+2
120127
" block scalar with indentation indicator
@@ -136,7 +143,10 @@ function GetYAMLIndent(lnum)
136143
let prevmapline = s:FindPrevLEIndentedLineMatchingRegex(a:lnum,
137144
\ s:mapkeyregex)
138145
if getline(prevmapline) =~# '^\s*- '
139-
return indent(prevmapline) + 2
146+
" Previous mapping key is in a list item (- key:)
147+
" The key effectively starts at indent + 2 (after "- ")
148+
" Content under it should be indented relative to the key position
149+
return indent(prevmapline) + 2 + shiftwidth()
140150
else
141151
return indent(prevmapline)
142152
endif

0 commit comments

Comments
 (0)