robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
43339dee40
master
1" Vim indent file 2" Language: YAML 3" Maintainer: Nikolai Pavlov <zyx.vim@gmail.com> 4" Last Updates: Lukas Reineke, "lacygoill" 5" Last Change: 2022 Jun 17 6" 2024 Feb 29 by Vim project: disable mulitline indent by default 7" 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 9" 2026 Jan 15 by Vim project: fix double shiftwidth from previous change 10 11" Only load this indent file when no other was loaded. 12if exists ( 'b:did_indent' ) 13finish 14endif 15 16let b: did_indent = 1 17 18setlocal indentexpr = GetYAMLIndent (v:lnum) 19setlocal indentkeys = !^F,o,O,0},0],<:>,0- 20setlocal nosmartindent 21 22let b: undo_indent = 'setlocal indentexpr< indentkeys< smartindent<' 23 24" Only define the function once. 25if exists ( '*GetYAMLIndent' ) 26finish 27endif 28 29let s: save_cpo = & cpo 30set cpo & vim 31 32function s: FindPrevLessIndentedLine ( lnum , ...) 33let prevlnum = prevnonblank ( a: lnum - 1 ) 34let curindent = a: 0 ? a: 1 : indent ( a: lnum ) 35while prevlnum 36 \&& indent ( prevlnum ) >= curindent 37 \&& getline ( prevlnum ) !~# '^\s*#' 38let prevlnum = prevnonblank ( prevlnum - 1 ) 39endwhile 40return prevlnum 41endfunction 42 43function s: FindPrevLEIndentedLineMatchingRegex ( lnum , regex ) 44let plilnum = s: FindPrevLessIndentedLine ( a: lnum , indent ( a: lnum ) + 1 ) 45while plilnum && getline ( plilnum ) !~# a: regex 46let plilnum = s: FindPrevLessIndentedLine ( plilnum ) 47endwhile 48return plilnum 49endfunction 50 51let s: mapkeyregex = '\v^\s*\#@!\S@=%(\''%([^'']|\''\'')*\''' .. 52 \'|\"%([^"\\]|\\.)*\"' .. 53 \'|%(%(\:\ )@!.)*)\:%(\ |$)' 54let s: liststartregex = '\v^\s*%(\-%(\ |$))' 55 56let s: c_ns_anchor_char = '\v%([\n\r\uFEFF \t,[\]{}]@!\p)' 57let s: c_ns_anchor_name = s: c_ns_anchor_char .. '+' 58let s: c_ns_anchor_property = '\v\&' .. s: c_ns_anchor_name 59 60let s: ns_word_char = '\v[[:alnum:]_\-]' 61let s: ns_tag_char = '\v%(\x\x|' .. s: ns_word_char .. '|[#/;?:@&=+$.~*''()])' 62let s: c_named_tag_handle = '\v\!' .. s: ns_word_char .. '+\!' 63let s: c_secondary_tag_handle = '\v\!\!' 64let s: c_primary_tag_handle = '\v\!' 65let s: c_tag_handle = '\v%(' .. s: c_named_tag_handle . 66 \'|' .. s: c_secondary_tag_handle . 67 \'|' .. s: c_primary_tag_handle .. ')' 68let s: c_ns_shorthand_tag = '\v' .. s: c_tag_handle .. s: ns_tag_char .. '+' 69let s: c_non_specific_tag = '\v\!' 70let s: ns_uri_char = '\v%(\x\x|' .. s: ns_word_char .. '\v|[#/;?:@&=+$,.!~*''()[\]])' 71let s: c_verbatim_tag = '\v\!\<' .. s: ns_uri_char .. '+\>' 72let s: c_ns_tag_property = '\v' .. s: c_verbatim_tag . 73 \'\v|' .. s: c_ns_shorthand_tag . 74 \'\v|' .. s: c_non_specific_tag 75 76let s: block_scalar_header = '\v[|>]%([+-]?[1-9]|[1-9]?[+-])?' 77 78function GetYAMLIndent ( lnum ) 79if a: lnum == 1 || ! prevnonblank ( a: lnum - 1 ) 80return 0 81endif 82 83let prevlnum = prevnonblank ( a: lnum - 1 ) 84let previndent = indent ( prevlnum ) 85 86let line = getline ( a: lnum ) 87if line =~# '^\s*#' && getline ( a: lnum - 1 ) =~# '^\s*#' 88" Comment blocks should have identical indent 89return previndent 90elseif line =~# '^\s*[\]}]' 91" Lines containing only closing braces should have previous indent 92return indent ( s: FindPrevLessIndentedLine ( a: lnum )) 93endif 94 95" Ignore comment lines when calculating indent 96while getline ( prevlnum ) =~# '^\s*#' 97let prevlnum = prevnonblank ( prevlnum - 1 ) 98if ! prevlnum 99return previndent 100endif 101endwhile 102 103let prevline = getline ( prevlnum ) 104let previndent = indent ( prevlnum ) 105 106" Any examples below assume that shiftwidth=2 107if prevline =~# '\v[{[:]$|[:-]\ [|>][+\-]?%(\s+\#.*|\s*)$' 108" Mapping key: 109" nested mapping: ... 110" 111" - { 112" key: [ 113" list value 114" ] 115" } 116" 117" - |- 118" Block scalar without indentation indicator 119if prevline =~# '^\s*-\s.*:$' 120" Special case: list item with mapping key (- key:) 121" Need to account for the "- " prefix 122return previndent + 2 + shiftwidth () 123else 124return previndent + shiftwidth () 125endif 126elseif prevline =~# '\v[:-]\ [|>]%(\d+[+\-]?|[+\-]?\d+)%(\#.*|\s*)$' 127" - |+2 128" block scalar with indentation indicator 129"#^^ indent+2, not indent+shiftwidth 130return previndent + str2nr ( matchstr ( prevline , 131 \'\v([:-]\ [|>])@<=[+\-]?\d+%([+\-]?%(\s+\#.*|\s*)$)@=' )) 132elseif prevline =~# '\v\"%([^"\\]|\\.)*\\$' 133" "Multiline string \ 134" with escaped end" 135let qidx = match ( prevline , '\v\"%([^"\\]|\\.)*\\' ) 136return virtcol ([ prevlnum , qidx + 1 ]) 137elseif line =~# s: liststartregex 138" List line should have indent equal to previous list line unless it was 139" caught by one of the previous rules 140return indent ( s: FindPrevLEIndentedLineMatchingRegex ( a: lnum , 141 \s: liststartregex )) 142elseif line =~# s: mapkeyregex 143" Same for line containing mapping key 144let prevmapline = s: FindPrevLEIndentedLineMatchingRegex ( a: lnum , 145 \s: mapkeyregex ) 146if getline ( prevmapline ) =~# '^\s*- ' 147" Previous mapping key is in a list item (- key:) 148" The key effectively starts at indent + 2 (after "- ") 149" Content under it should be indented relative to the key position 150return indent ( prevmapline ) + 2 151else 152return indent ( prevmapline ) 153endif 154elseif get ( g:, 'yaml_indent_multiline_scalar' , 0 ) && 155 \prevline =~# '^\s*- ' 156" - List with 157" multiline scalar 158return previndent + 2 159elseif get ( g:, 'yaml_indent_multiline_scalar' , 0 ) && 160 \prevline =~# s: mapkeyregex .. '\v\s*%(%(' .. s: c_ns_tag_property .. 161 \'\v|' .. s: c_ns_anchor_property .. 162 \'\v|' .. s: block_scalar_header .. 163 \'\v)%(\s+|\s*%(\#.*)?$))*' 164" Mapping with: value 165" that is multiline scalar 166return previndent + shiftwidth () 167endif 168return previndent 169endfunction 170 171let & cpo = s: save_cpo