]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
runtime(yaml): update YAML indentation for mapping keys inside list items
authorCezar Dimoiu <cezar.dimoiu@keysight.com>
Thu, 8 Jan 2026 20:05:07 +0000 (20:05 +0000)
committerChristian Brabandt <cb@256bit.org>
Thu, 8 Jan 2026 20:07:18 +0000 (20:07 +0000)
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 <cezar.dimoiu@keysight.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/indent/testdir/yaml.in
runtime/indent/testdir/yaml.ok
runtime/indent/yaml.vim

index bf99668da294a6cc0578c05ecb0fd5718f8a4d50..32b56a73f8cb7e3294defd8bc7d3025403a6236b 100644 (file)
@@ -18,3 +18,11 @@ map: |
 line1
 line2
 # END_INDENT
+
+# START_INDENT
+list:
+  - element1:
+    foo: bar
+  - element2:
+      foo: bar
+# END_INDENT
index 8b38633e714884e251cdba653229911b3dd3dbf2..0bfabfc1665e892b809afe264cb569397c2d12c9 100644 (file)
@@ -18,3 +18,11 @@ map: |
   line1
   line2
 # END_INDENT
+
+# START_INDENT
+list:
+  - element1:
+      foo: bar
+  - element2:
+      foo: bar
+# END_INDENT
index c38712745d4821266024e3d1b623cd183ed4f6b8..fb0f055dce055b1e04710d8c4a4917a23770cda3 100644 (file)
@@ -5,6 +5,7 @@
 " Last Change: 2022 Jun 17
 " 2024 Feb 29 by Vim project: disable mulitline indent by default
 " 2024 Aug 14 by Vim project: fix re-indenting when commenting out lines
+" 2026 Jan 08 by Vim project: fix object indentation in array
 
 " Only load this indent file when no other was loaded.
 if exists('b:did_indent')
@@ -114,7 +115,13 @@ function GetYAMLIndent(lnum)
         "
         " - |-
         "     Block scalar without indentation indicator
-        return previndent+shiftwidth()
+        if prevline =~# '^\s*-\s.*:$'
+            " Special case: list item with mapping key (- key:)
+            " Need to account for the "- " prefix
+            return previndent + 2 + shiftwidth()
+        else
+            return previndent+shiftwidth()
+        endif
     elseif prevline =~# '\v[:-]\ [|>]%(\d+[+\-]?|[+\-]?\d+)%(\#.*|\s*)$'
         " - |+2
         "   block scalar with indentation indicator
@@ -136,7 +143,10 @@ function GetYAMLIndent(lnum)
         let prevmapline = s:FindPrevLEIndentedLineMatchingRegex(a:lnum,
                     \                                           s:mapkeyregex)
         if getline(prevmapline) =~# '^\s*- '
-            return indent(prevmapline) + 2
+            " Previous mapping key is in a list item (- key:)
+            " The key effectively starts at indent + 2 (after "- ")
+            " Content under it should be indented relative to the key position
+            return indent(prevmapline) + 2 + shiftwidth()
         else
             return indent(prevmapline)
         endif