diff --git a/runtime/indent/testdir/yaml.in b/runtime/indent/testdir/yaml.in index bf99668da2..32b56a73f8 100644 --- a/runtime/indent/testdir/yaml.in +++ b/runtime/indent/testdir/yaml.in @@ -18,3 +18,11 @@ map: | line1 line2 # END_INDENT + +# START_INDENT +list: + - element1: + foo: bar + - element2: + foo: bar +# END_INDENT diff --git a/runtime/indent/testdir/yaml.ok b/runtime/indent/testdir/yaml.ok index 8b38633e71..0bfabfc166 100644 --- a/runtime/indent/testdir/yaml.ok +++ b/runtime/indent/testdir/yaml.ok @@ -18,3 +18,11 @@ map: | line1 line2 # END_INDENT + +# START_INDENT +list: + - element1: + foo: bar + - element2: + foo: bar +# END_INDENT diff --git a/runtime/indent/yaml.vim b/runtime/indent/yaml.vim index c38712745d..fb0f055dce 100644 --- a/runtime/indent/yaml.vim +++ b/runtime/indent/yaml.vim @@ -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