annotate runtime/indent/yaml.vim @ 9786:3089f1d99b9e v7.4.2168

commit https://github.com/vim/vim/commit/5b4a3767f6d1760ba1ce103ef3cffb696ece0244 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Aug 6 20:36:34 2016 +0200 patch 7.4.2168 Problem: Not running the startup test on MS-Windows. Solution: Write vimcmd.
author Christian Brabandt <cb@256bit.org>
date Sat, 06 Aug 2016 20:45:06 +0200
parents 873eae260c97
children b4da19b7539f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3996
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
1 " Vim indent file
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
2 " Language: YAML
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
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
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
5
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
6 " Only load this indent file when no other was loaded.
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
7 if exists('b:did_indent')
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
8 finish
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
9 endif
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
10
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
11 let s:save_cpo = &cpo
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
12 set cpo&vim
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
13
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
14 let b:did_indent = 1
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
15
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
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
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
18 setlocal nosmartindent
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
19
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
20 let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<'
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
21
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
22 " Only define the function once.
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
23 if exists('*GetYAMLIndent')
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
24 finish
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
25 endif
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
26
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
27 if exists('*shiftwidth')
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
28 let s:shiftwidth = function('shiftwidth')
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
29 else
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
30 function s:shiftwidth()
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
31 return &shiftwidth
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
32 endfunction
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
33 endif
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
34
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
35 function s:FindPrevLessIndentedLine(lnum, ...)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
36 let prevlnum = prevnonblank(a:lnum-1)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
37 let curindent = a:0 ? a:1 : indent(a:lnum)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
38 while prevlnum
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
39 \&& indent(prevlnum) >= curindent
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
40 \&& getline(prevlnum) !~# '^\s*#'
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
41 let prevlnum = prevnonblank(prevlnum-1)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
42 endwhile
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
43 return prevlnum
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
44 endfunction
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
45
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
46 function s:FindPrevLEIndentedLineMatchingRegex(lnum, regex)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
47 let plilnum = s:FindPrevLessIndentedLine(a:lnum, indent(a:lnum)+1)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
48 while plilnum && getline(plilnum) !~# a:regex
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
49 let plilnum = s:FindPrevLessIndentedLine(plilnum)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
50 endwhile
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
51 return plilnum
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
52 endfunction
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
53
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
54 let s:mapkeyregex='\v^\s*%(\''%([^'']|'''')*\'''.
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
55 \ '|\"%([^"\\]|\\.)*\"'.
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
56 \ '|%(%(\:\ )@!.)*)\:%(\ |$)'
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
57 let s:liststartregex='\v^\s*%(\-%(\ |$))'
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
58
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
59 function GetYAMLIndent(lnum)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
60 if a:lnum == 1 || !prevnonblank(a:lnum-1)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
61 return 0
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
62 endif
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
63
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
64 let prevlnum = prevnonblank(a:lnum-1)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
65 let previndent = indent(prevlnum)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
66
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
67 let line = getline(a:lnum)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
68 if line =~# '^\s*#' && getline(a:lnum-1) =~# '^\s*#'
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
69 " Comment blocks should have identical indent
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
70 return previndent
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
71 elseif line =~# '^\s*[\]}]'
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
72 " Lines containing only closing braces should have previous indent
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
73 return indent(s:FindPrevLessIndentedLine(a:lnum))
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
74 endif
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
75
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
76 " Ignore comment lines when calculating indent
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
77 while getline(prevlnum) =~# '^\s*#'
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
78 let prevlnum = prevnonblank(prevlnum-1)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
79 if !prevlnum
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
80 return previndent
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
81 endif
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
82 endwhile
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
83
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
84 let prevline = getline(prevlnum)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
85 let previndent = indent(prevlnum)
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
86
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
87 " Any examples below assume that shiftwidth=2
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
88 if prevline =~# '\v[{[:]$|[:-]\ [|>][+\-]?%(\s+\#.*|\s*)$'
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
89 " Mapping key:
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
90 " nested mapping: ...
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
91 "
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
92 " - {
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
93 " key: [
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
94 " list value
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
95 " ]
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
96 " }
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
97 "
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
98 " - |-
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
99 " Block scalar without indentation indicator
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
100 return previndent+s:shiftwidth()
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
101 elseif prevline =~# '\v[:-]\ [|>]%(\d+[+\-]?|[+\-]?\d+)%(\#.*|\s*)$'
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
102 " - |+2
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
103 " block scalar with indentation indicator
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
104 "#^^ indent+2, not indent+shiftwidth
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
105 return previndent + str2nr(matchstr(prevline,
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
106 \'\v([:-]\ [|>])@<=[+\-]?\d+%([+\-]?%(\s+\#.*|\s*)$)@='))
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
107 elseif prevline =~# '\v\"%([^"\\]|\\.)*\\$'
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
108 " "Multiline string \
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
109 " with escaped end"
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
110 let qidx = match(prevline, '\v\"%([^"\\]|\\.)*\\')
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
111 return virtcol([prevlnum, qidx+1])
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
112 elseif line =~# s:liststartregex
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
113 " List line should have indent equal to previous list line unless it was
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
114 " caught by one of the previous rules
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
115 return indent(s:FindPrevLEIndentedLineMatchingRegex(a:lnum,
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
116 \ s:liststartregex))
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
117 elseif line =~# s:mapkeyregex
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
118 " 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
119 let prevmapline = s:FindPrevLEIndentedLineMatchingRegex(a:lnum,
c590de398af9 commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents: 3996
diff changeset
120 \ s:mapkeyregex)
c590de398af9 commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents: 3996
diff changeset
121 if getline(prevmapline) =~# '^\s*- '
c590de398af9 commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents: 3996
diff changeset
122 return indent(prevmapline) + 2
c590de398af9 commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents: 3996
diff changeset
123 else
c590de398af9 commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents: 3996
diff changeset
124 return indent(prevmapline)
c590de398af9 commit https://github.com/vim/vim/commit/ca63501fbcd1cf9c8aa9ff12c093c95b62a89ed7
Christian Brabandt <cb@256bit.org>
parents: 3996
diff changeset
125 endif
3996
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
126 elseif prevline =~# '^\s*- '
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
127 " - List with
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
128 " multiline scalar
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
129 return previndent+2
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
130 elseif prevline =~# s:mapkeyregex
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
131 " Mapping with: value
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
132 " that is multiline scalar
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
133 return previndent+s:shiftwidth()
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
134 endif
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
135 return previndent
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
136 endfunction
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
137
b3f3237a3d72 Update runtime files.
Bram Moolenaar <bram@vim.org>
parents:
diff changeset
138 let &cpo = s:save_cpo