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