Mercurial > vim
annotate runtime/indent/yaml.vim @ 35075:43739470fe40 default tip
runtime(asm): missing setlocal in indent plugin (#14658)
Commit: https://github.com/vim/vim/commit/2e9b9e9a9ebf3fd40437260ecd6b1e23b02c636b
Author: Marc Sven Schulte <167623652+msschulte@users.noreply.github.com>
Date: Sun Apr 28 21:43:03 2024 +0200
runtime(asm): missing setlocal in indent plugin (https://github.com/vim/vim/issues/14658)
Signed-off-by: Marc Sven Schulte <167623652+msschulte@users.noreply.github.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 28 Apr 2024 21:45:03 +0200 |
parents | e3d2ba1fd1a3 |
children |
rev | line source |
---|---|
3996 | 1 " Vim indent file |
20753 | 2 " Language: YAML |
3 " Maintainer: Nikolai Pavlov <zyx.vim@gmail.com> | |
30085 | 4 " Last Updates: Lukas Reineke, "lacygoill" |
29236 | 5 " Last Change: 2022 Jun 17 |
34456
e3d2ba1fd1a3
runtime(yaml): disable multiline_scalar detection by default
Christian Brabandt <cb@256bit.org>
parents:
30085
diff
changeset
|
6 " 2024 Feb 29 disable mulitline indent by default (The Vim project) |
3996 | 7 |
8 " Only load this indent file when no other was loaded. | |
9 if exists('b:did_indent') | |
10 finish | |
11 endif | |
12 | |
13 let b:did_indent = 1 | |
14 | |
15 setlocal indentexpr=GetYAMLIndent(v:lnum) | |
7228
873eae260c97
commit https://github.com/vim/vim/commit/b4ff518d95aa57c2f8c0568c915035bef849581b
Christian Brabandt <cb@256bit.org>
parents:
7147
diff
changeset
|
16 setlocal indentkeys=!^F,o,O,0#,0},0],<:>,0- |
3996 | 17 setlocal nosmartindent |
18 | |
19 let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<' | |
20 | |
21 " Only define the function once. | |
22 if exists('*GetYAMLIndent') | |
23 finish | |
24 endif | |
25 | |
23737 | 26 let s:save_cpo = &cpo |
27 set cpo&vim | |
28 | |
3996 | 29 function s:FindPrevLessIndentedLine(lnum, ...) |
30 let prevlnum = prevnonblank(a:lnum-1) | |
31 let curindent = a:0 ? a:1 : indent(a:lnum) | |
32 while prevlnum | |
30085 | 33 \ && indent(prevlnum) >= curindent |
34 \ && getline(prevlnum) !~# '^\s*#' | |
3996 | 35 let prevlnum = prevnonblank(prevlnum-1) |
36 endwhile | |
37 return prevlnum | |
38 endfunction | |
39 | |
40 function s:FindPrevLEIndentedLineMatchingRegex(lnum, regex) | |
41 let plilnum = s:FindPrevLessIndentedLine(a:lnum, indent(a:lnum)+1) | |
42 while plilnum && getline(plilnum) !~# a:regex | |
43 let plilnum = s:FindPrevLessIndentedLine(plilnum) | |
44 endwhile | |
45 return plilnum | |
46 endfunction | |
47 | |
29236 | 48 let s:mapkeyregex = '\v^\s*\#@!\S@=%(\''%([^'']|\''\'')*\''' .. |
49 \ '|\"%([^"\\]|\\.)*\"' .. | |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
50 \ '|%(%(\:\ )@!.)*)\:%(\ |$)' |
29236 | 51 let s:liststartregex = '\v^\s*%(\-%(\ |$))' |
3996 | 52 |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
53 let s:c_ns_anchor_char = '\v%([\n\r\uFEFF \t,[\]{}]@!\p)' |
29236 | 54 let s:c_ns_anchor_name = s:c_ns_anchor_char .. '+' |
55 let s:c_ns_anchor_property = '\v\&' .. s:c_ns_anchor_name | |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
56 |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
57 let s:ns_word_char = '\v[[:alnum:]_\-]' |
30085 | 58 let s:ns_tag_char = '\v%(\x\x|' .. s:ns_word_char .. '|[#/;?:@&=+$.~*''()])' |
29236 | 59 let s:c_named_tag_handle = '\v\!' .. s:ns_word_char .. '+\!' |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
60 let s:c_secondary_tag_handle = '\v\!\!' |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
61 let s:c_primary_tag_handle = '\v\!' |
29236 | 62 let s:c_tag_handle = '\v%(' .. s:c_named_tag_handle. |
63 \ '|' .. s:c_secondary_tag_handle. | |
64 \ '|' .. s:c_primary_tag_handle .. ')' | |
65 let s:c_ns_shorthand_tag = '\v' .. s:c_tag_handle .. s:ns_tag_char .. '+' | |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
66 let s:c_non_specific_tag = '\v\!' |
30085 | 67 let s:ns_uri_char = '\v%(\x\x|' .. s:ns_word_char .. '\v|[#/;?:@&=+$,.!~*''()[\]])' |
29236 | 68 let s:c_verbatim_tag = '\v\!\<' .. s:ns_uri_char.. '+\>' |
69 let s:c_ns_tag_property = '\v' .. s:c_verbatim_tag. | |
70 \ '\v|' .. s:c_ns_shorthand_tag. | |
71 \ '\v|' .. s:c_non_specific_tag | |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
72 |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
73 let s:block_scalar_header = '\v[|>]%([+-]?[1-9]|[1-9]?[+-])?' |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
74 |
3996 | 75 function GetYAMLIndent(lnum) |
76 if a:lnum == 1 || !prevnonblank(a:lnum-1) | |
77 return 0 | |
78 endif | |
79 | |
80 let prevlnum = prevnonblank(a:lnum-1) | |
81 let previndent = indent(prevlnum) | |
82 | |
83 let line = getline(a:lnum) | |
84 if line =~# '^\s*#' && getline(a:lnum-1) =~# '^\s*#' | |
85 " Comment blocks should have identical indent | |
86 return previndent | |
87 elseif line =~# '^\s*[\]}]' | |
88 " Lines containing only closing braces should have previous indent | |
89 return indent(s:FindPrevLessIndentedLine(a:lnum)) | |
90 endif | |
91 | |
92 " Ignore comment lines when calculating indent | |
93 while getline(prevlnum) =~# '^\s*#' | |
94 let prevlnum = prevnonblank(prevlnum-1) | |
95 if !prevlnum | |
96 return previndent | |
97 endif | |
98 endwhile | |
99 | |
100 let prevline = getline(prevlnum) | |
101 let previndent = indent(prevlnum) | |
102 | |
103 " Any examples below assume that shiftwidth=2 | |
104 if prevline =~# '\v[{[:]$|[:-]\ [|>][+\-]?%(\s+\#.*|\s*)$' | |
105 " Mapping key: | |
106 " nested mapping: ... | |
107 " | |
108 " - { | |
109 " key: [ | |
110 " list value | |
111 " ] | |
112 " } | |
113 " | |
114 " - |- | |
115 " Block scalar without indentation indicator | |
11518 | 116 return previndent+shiftwidth() |
3996 | 117 elseif prevline =~# '\v[:-]\ [|>]%(\d+[+\-]?|[+\-]?\d+)%(\#.*|\s*)$' |
118 " - |+2 | |
119 " block scalar with indentation indicator | |
120 "#^^ indent+2, not indent+shiftwidth | |
121 return previndent + str2nr(matchstr(prevline, | |
122 \'\v([:-]\ [|>])@<=[+\-]?\d+%([+\-]?%(\s+\#.*|\s*)$)@=')) | |
123 elseif prevline =~# '\v\"%([^"\\]|\\.)*\\$' | |
124 " "Multiline string \ | |
125 " with escaped end" | |
126 let qidx = match(prevline, '\v\"%([^"\\]|\\.)*\\') | |
127 return virtcol([prevlnum, qidx+1]) | |
128 elseif line =~# s:liststartregex | |
129 " List line should have indent equal to previous list line unless it was | |
130 " caught by one of the previous rules | |
131 return indent(s:FindPrevLEIndentedLineMatchingRegex(a:lnum, | |
132 \ s:liststartregex)) | |
133 elseif line =~# s:mapkeyregex | |
134 " Same for line containing mapping key | |
7147
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
135 let prevmapline = s:FindPrevLEIndentedLineMatchingRegex(a:lnum, |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
136 \ s:mapkeyregex) |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
137 if getline(prevmapline) =~# '^\s*- ' |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
138 return indent(prevmapline) + 2 |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
139 else |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
140 return indent(prevmapline) |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
141 endif |
34456
e3d2ba1fd1a3
runtime(yaml): disable multiline_scalar detection by default
Christian Brabandt <cb@256bit.org>
parents:
30085
diff
changeset
|
142 elseif get(g:, 'yaml_indent_multiline_scalar', 0) && |
e3d2ba1fd1a3
runtime(yaml): disable multiline_scalar detection by default
Christian Brabandt <cb@256bit.org>
parents:
30085
diff
changeset
|
143 \ prevline =~# '^\s*- ' |
3996 | 144 " - List with |
145 " multiline scalar | |
146 return previndent+2 | |
34456
e3d2ba1fd1a3
runtime(yaml): disable multiline_scalar detection by default
Christian Brabandt <cb@256bit.org>
parents:
30085
diff
changeset
|
147 elseif get(g:, 'yaml_indent_multiline_scalar', 0) && |
e3d2ba1fd1a3
runtime(yaml): disable multiline_scalar detection by default
Christian Brabandt <cb@256bit.org>
parents:
30085
diff
changeset
|
148 \ prevline =~# s:mapkeyregex .. '\v\s*%(%(' .. s:c_ns_tag_property .. |
29236 | 149 \ '\v|' .. s:c_ns_anchor_property .. |
150 \ '\v|' .. s:block_scalar_header .. | |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
151 \ '\v)%(\s+|\s*%(\#.*)?$))*' |
3996 | 152 " Mapping with: value |
153 " that is multiline scalar | |
11518 | 154 return previndent+shiftwidth() |
3996 | 155 endif |
156 return previndent | |
157 endfunction | |
158 | |
159 let &cpo = s:save_cpo |