Mercurial > vim
annotate runtime/indent/yaml.vim @ 10049:efc8a434f38f v7.4.2296
commit https://github.com/vim/vim/commit/c628fdcd46e93c308f742efdf54248695960e290
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Aug 31 20:33:27 2016 +0200
patch 7.4.2296
Problem: No tests for :undolist and "U" command.
Solution: Add tests. (Dominique Pelle)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Wed, 31 Aug 2016 20:45:05 +0200 |
parents | b4da19b7539f |
children | 63b0b7b79b25 |
rev | line source |
---|---|
3996 | 1 " Vim indent file |
2 " Language: YAML | |
3 " Maintainer: Nikolai Pavlov <zyx.vim@gmail.com> | |
7228
873eae260c97
commit https://github.com/vim/vim/commit/b4ff518d95aa57c2f8c0568c915035bef849581b
Christian Brabandt <cb@256bit.org>
parents:
7147
diff
changeset
|
4 " Last Change: 2015 Nov 01 |
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 if exists('*shiftwidth') | |
28 let s:shiftwidth = function('shiftwidth') | |
29 else | |
30 function s:shiftwidth() | |
31 return &shiftwidth | |
32 endfunction | |
33 endif | |
34 | |
35 function s:FindPrevLessIndentedLine(lnum, ...) | |
36 let prevlnum = prevnonblank(a:lnum-1) | |
37 let curindent = a:0 ? a:1 : indent(a:lnum) | |
38 while prevlnum | |
39 \&& indent(prevlnum) >= curindent | |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
40 \&& getline(prevlnum) =~# '^\s*#' |
3996 | 41 let prevlnum = prevnonblank(prevlnum-1) |
42 endwhile | |
43 return prevlnum | |
44 endfunction | |
45 | |
46 function s:FindPrevLEIndentedLineMatchingRegex(lnum, regex) | |
47 let plilnum = s:FindPrevLessIndentedLine(a:lnum, indent(a:lnum)+1) | |
48 while plilnum && getline(plilnum) !~# a:regex | |
49 let plilnum = s:FindPrevLessIndentedLine(plilnum) | |
50 endwhile | |
51 return plilnum | |
52 endfunction | |
53 | |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
54 let s:mapkeyregex='\v^\s*\#@!\S@=%(\''%([^'']|\''\'')*\'''. |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
55 \ '|\"%([^"\\]|\\.)*\"'. |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
56 \ '|%(%(\:\ )@!.)*)\:%(\ |$)' |
3996 | 57 let s:liststartregex='\v^\s*%(\-%(\ |$))' |
58 | |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
59 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
|
60 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
|
61 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
|
62 |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
63 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
|
64 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
|
65 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
|
66 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
|
67 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
|
68 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
|
69 \ '|'.s:c_secondary_tag_handle. |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
70 \ '|'.s:c_primary_tag_handle.')' |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
71 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
|
72 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
|
73 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
|
74 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
|
75 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
|
76 \ '\v|'.s:c_ns_shorthand_tag. |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
77 \ '\v|'.s:c_non_specific_tag |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
78 |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
79 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
|
80 |
3996 | 81 function GetYAMLIndent(lnum) |
82 if a:lnum == 1 || !prevnonblank(a:lnum-1) | |
83 return 0 | |
84 endif | |
85 | |
86 let prevlnum = prevnonblank(a:lnum-1) | |
87 let previndent = indent(prevlnum) | |
88 | |
89 let line = getline(a:lnum) | |
90 if line =~# '^\s*#' && getline(a:lnum-1) =~# '^\s*#' | |
91 " Comment blocks should have identical indent | |
92 return previndent | |
93 elseif line =~# '^\s*[\]}]' | |
94 " Lines containing only closing braces should have previous indent | |
95 return indent(s:FindPrevLessIndentedLine(a:lnum)) | |
96 endif | |
97 | |
98 " Ignore comment lines when calculating indent | |
99 while getline(prevlnum) =~# '^\s*#' | |
100 let prevlnum = prevnonblank(prevlnum-1) | |
101 if !prevlnum | |
102 return previndent | |
103 endif | |
104 endwhile | |
105 | |
106 let prevline = getline(prevlnum) | |
107 let previndent = indent(prevlnum) | |
108 | |
109 " Any examples below assume that shiftwidth=2 | |
110 if prevline =~# '\v[{[:]$|[:-]\ [|>][+\-]?%(\s+\#.*|\s*)$' | |
111 " Mapping key: | |
112 " nested mapping: ... | |
113 " | |
114 " - { | |
115 " key: [ | |
116 " list value | |
117 " ] | |
118 " } | |
119 " | |
120 " - |- | |
121 " Block scalar without indentation indicator | |
122 return previndent+s:shiftwidth() | |
123 elseif prevline =~# '\v[:-]\ [|>]%(\d+[+\-]?|[+\-]?\d+)%(\#.*|\s*)$' | |
124 " - |+2 | |
125 " block scalar with indentation indicator | |
126 "#^^ indent+2, not indent+shiftwidth | |
127 return previndent + str2nr(matchstr(prevline, | |
128 \'\v([:-]\ [|>])@<=[+\-]?\d+%([+\-]?%(\s+\#.*|\s*)$)@=')) | |
129 elseif prevline =~# '\v\"%([^"\\]|\\.)*\\$' | |
130 " "Multiline string \ | |
131 " with escaped end" | |
132 let qidx = match(prevline, '\v\"%([^"\\]|\\.)*\\') | |
133 return virtcol([prevlnum, qidx+1]) | |
134 elseif line =~# s:liststartregex | |
135 " List line should have indent equal to previous list line unless it was | |
136 " caught by one of the previous rules | |
137 return indent(s:FindPrevLEIndentedLineMatchingRegex(a:lnum, | |
138 \ s:liststartregex)) | |
139 elseif line =~# s:mapkeyregex | |
140 " 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
|
141 let prevmapline = s:FindPrevLEIndentedLineMatchingRegex(a:lnum, |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
142 \ s:mapkeyregex) |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
143 if getline(prevmapline) =~# '^\s*- ' |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
144 return indent(prevmapline) + 2 |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
145 else |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
146 return indent(prevmapline) |
c590de398af9
commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents:
3996
diff
changeset
|
147 endif |
3996 | 148 elseif prevline =~# '^\s*- ' |
149 " - List with | |
150 " multiline scalar | |
151 return previndent+2 | |
9887
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
152 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
|
153 \ '\v|'.s:c_ns_anchor_property. |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
154 \ '\v|'.s:block_scalar_header. |
b4da19b7539f
commit https://github.com/vim/vim/commit/dc1f1645cb495fa6bfbe216d7359f23539a0e25d
Christian Brabandt <cb@256bit.org>
parents:
7228
diff
changeset
|
155 \ '\v)%(\s+|\s*%(\#.*)?$))*' |
3996 | 156 " Mapping with: value |
157 " that is multiline scalar | |
158 return previndent+s:shiftwidth() | |
159 endif | |
160 return previndent | |
161 endfunction | |
162 | |
163 let &cpo = s:save_cpo |