Mercurial > vim
annotate runtime/indent/sh.vim @ 21141:d745abd682f9
Added tag v8.2.1121 for changeset f86140ad01646e71f2a5854fb70bb7bb2048e621
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Fri, 03 Jul 2020 18:30:03 +0200 |
parents | 6d11fc4aa683 |
children | f0d7cb510ce3 |
rev | line source |
---|---|
7 | 1 " Vim indent file |
7013 | 2 " Language: Shell Script |
3 " Maintainer: Christian Brabandt <cb@256bit.org> | |
13937 | 4 " Original Author: Nikolai Weibull <now@bitwi.se> |
6918 | 5 " Previous Maintainer: Peter Aronoff <telemachus@arpinum.org> |
18456 | 6 " Latest Revision: 2019-10-24 |
7013 | 7 " License: Vim (see :h license) |
8 " Repository: https://github.com/chrisbra/vim-sh-indent | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
9 " Changelog: |
17571 | 10 " 20190726 - Correctly skip if keywords in syntax comments |
11 " (issue #17) | |
12 " 20190603 - Do not indent in zsh filetypes with an `if` in comments | |
16610 | 13 " 20190428 - De-indent fi correctly when typing with |
14 " https://github.com/chrisbra/vim-sh-indent/issues/15 | |
16267 | 15 " 20190325 - Indent fi; correctly |
16 " https://github.com/chrisbra/vim-sh-indent/issues/14 | |
17 " 20190319 - Indent arrays (only zsh and bash) | |
18 " https://github.com/chrisbra/vim-sh-indent/issues/13 | |
19 " 20190316 - Make use of searchpairpos for nested if sections | |
20 " fixes https://github.com/chrisbra/vim-sh-indent/issues/11 | |
21 " 20190201 - Better check for closing if sections | |
14421 | 22 " 20180724 - make check for zsh syntax more rigid (needs word-boundaries) |
13937 | 23 " 20180326 - better support for line continuation |
24 " 20180325 - better detection of function definitions | |
25 " 20180127 - better support for zsh complex commands | |
12045 | 26 " 20170808: - better indent of line continuation |
11442 | 27 " 20170502: - get rid of buffer-shiftwidth function |
28 " 20160912: - preserve indentation of here-doc blocks | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
29 " 20160627: - detect heredocs correctly |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
30 " 20160213: - detect function definition correctly |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
31 " 20160202: - use shiftwidth() function |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
32 " 20151215: - set b:undo_indent variable |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
33 " 20150728: - add foreach detection for zsh |
7 | 34 |
35 if exists("b:did_indent") | |
36 finish | |
37 endif | |
38 let b:did_indent = 1 | |
39 | |
40 setlocal indentexpr=GetShIndent() | |
7013 | 41 setlocal indentkeys+=0=then,0=do,0=else,0=elif,0=fi,0=esac,0=done,0=end,),0=;;,0=;& |
2034 | 42 setlocal indentkeys+=0=fin,0=fil,0=fip,0=fir,0=fix |
7 | 43 setlocal indentkeys-=:,0# |
2034 | 44 setlocal nosmartindent |
7 | 45 |
8246
f16bfe02cef1
commit https://github.com/vim/vim/commit/f391327adbbffb11180cf6038a92af1ed144e907
Christian Brabandt <cb@256bit.org>
parents:
7924
diff
changeset
|
46 let b:undo_indent = 'setlocal indentexpr< indentkeys< smartindent<' |
f16bfe02cef1
commit https://github.com/vim/vim/commit/f391327adbbffb11180cf6038a92af1ed144e907
Christian Brabandt <cb@256bit.org>
parents:
7924
diff
changeset
|
47 |
7 | 48 if exists("*GetShIndent") |
49 finish | |
50 endif | |
51 | |
375 | 52 let s:cpo_save = &cpo |
53 set cpo&vim | |
7 | 54 |
2034 | 55 let s:sh_indent_defaults = { |
11442 | 56 \ 'default': function('shiftwidth'), |
57 \ 'continuation-line': function('shiftwidth'), | |
58 \ 'case-labels': function('shiftwidth'), | |
59 \ 'case-statements': function('shiftwidth'), | |
2034 | 60 \ 'case-breaks': 0 } |
61 | |
62 function! s:indent_value(option) | |
63 let Value = exists('b:sh_indent_options') | |
64 \ && has_key(b:sh_indent_options, a:option) ? | |
65 \ b:sh_indent_options[a:option] : | |
66 \ s:sh_indent_defaults[a:option] | |
67 if type(Value) == type(function('type')) | |
68 return Value() | |
69 endif | |
70 return Value | |
71 endfunction | |
72 | |
73 function! GetShIndent() | |
16267 | 74 let curline = getline(v:lnum) |
7 | 75 let lnum = prevnonblank(v:lnum - 1) |
76 if lnum == 0 | |
77 return 0 | |
78 endif | |
13912 | 79 let line = getline(lnum) |
7 | 80 |
2034 | 81 let pnum = prevnonblank(lnum - 1) |
13912 | 82 let pline = getline(pnum) |
7 | 83 let ind = indent(lnum) |
13912 | 84 |
85 " Check contents of previous lines | |
17571 | 86 " should not apply to e.g. commented lines |
13912 | 87 if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' || |
17571 | 88 \ (&ft is# 'zsh' && line =~ '^\s*\<\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>') |
16267 | 89 if !s:is_end_expression(line) |
2034 | 90 let ind += s:indent_value('default') |
91 endif | |
92 elseif s:is_case_label(line, pnum) | |
93 if !s:is_case_ended(line) | |
94 let ind += s:indent_value('case-statements') | |
7 | 95 endif |
13912 | 96 " function definition |
97 elseif s:is_function_definition(line) | |
2034 | 98 if line !~ '}\s*\%(#.*\)\=$' |
99 let ind += s:indent_value('default') | |
100 endif | |
16267 | 101 " array (only works for zsh or bash) |
102 elseif s:is_array(line) && line !~ ')\s*$' && (&ft is# 'zsh' || s:is_bash()) | |
103 let ind += s:indent_value('continuation-line') | |
104 " end of array | |
105 elseif curline =~ '^\s*)$' | |
106 let ind -= s:indent_value('continuation-line') | |
2034 | 107 elseif s:is_continuation_line(line) |
13912 | 108 if pnum == 0 || !s:is_continuation_line(pline) |
2034 | 109 let ind += s:indent_value('continuation-line') |
110 endif | |
13912 | 111 elseif s:end_block(line) && !s:start_block(line) |
112 let ind -= s:indent_value('default') | |
16267 | 113 elseif pnum != 0 && |
114 \ s:is_continuation_line(pline) && | |
115 \ !s:end_block(curline) && | |
116 \ !s:is_end_expression(curline) | |
13912 | 117 " only add indent, if line and pline is in the same block |
118 let i = v:lnum | |
119 let ind2 = indent(s:find_continued_lnum(pnum)) | |
120 while !s:is_empty(getline(i)) && i > pnum | |
121 let i -= 1 | |
122 endw | |
123 if i == pnum | |
124 let ind += ind2 | |
125 else | |
126 let ind = ind2 | |
127 endif | |
7 | 128 endif |
129 | |
2034 | 130 let pine = line |
13912 | 131 " Check content of current line |
16267 | 132 let line = curline |
133 " Current line is a endif line, so get indent from start of "if condition" line | |
134 " TODO: should we do the same for other "end" lines? | |
135 if curline =~ '^\s*\%(fi\);\?\s*\%(#.*\)\=$' | |
17571 | 136 let ind = indent(v:lnum) |
18456 | 137 let previous_line = searchpair('\<if\>', '', '\<fi\>\zs', 'bnW', 'synIDattr(synID(line("."),col("."), 1),"name") =~? "comment\\|quote"') |
16267 | 138 if previous_line > 0 |
139 let ind = indent(previous_line) | |
140 endif | |
141 elseif line =~ '^\s*\%(then\|do\|else\|elif\|done\|end\)\>' || s:end_block(line) | |
2034 | 142 let ind -= s:indent_value('default') |
5555 | 143 elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1)) |
144 let ind -= s:indent_value('default') | |
2034 | 145 elseif line =~ '^\s*esac\>' |
146 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? | |
147 \ 0 : s:indent_value('case-statements')) + | |
148 \ s:indent_value('case-labels') | |
149 if s:is_case_break(pine) | |
150 let ind += s:indent_value('case-breaks') | |
151 endif | |
152 elseif s:is_case_label(line, lnum) | |
153 if s:is_case(pine) | |
154 let ind = indent(lnum) + s:indent_value('case-labels') | |
155 else | |
6153 | 156 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? |
157 \ 0 : s:indent_value('case-statements')) - | |
158 \ s:indent_value('case-breaks') | |
2034 | 159 endif |
160 elseif s:is_case_break(line) | |
161 let ind -= s:indent_value('case-breaks') | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
162 elseif s:is_here_doc(line) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
163 let ind = 0 |
11442 | 164 " statements, executed within a here document. Keep the current indent |
165 elseif match(map(synstack(v:lnum, 1), 'synIDattr(v:val, "name")'), '\c\mheredoc') > -1 | |
166 return indent(v:lnum) | |
13912 | 167 elseif s:is_comment(line) && s:is_empty(getline(v:lnum-1)) |
168 return indent(v:lnum) | |
7 | 169 endif |
170 | |
13912 | 171 return ind > 0 ? ind : 0 |
7 | 172 endfunction |
173 | |
2034 | 174 function! s:is_continuation_line(line) |
13912 | 175 " Comment, cannot be a line continuation |
176 if a:line =~ '^\s*#' | |
177 return 0 | |
178 else | |
179 " start-of-line | |
180 " \\ or && or || or | | |
181 " followed optionally by { or # | |
182 return a:line =~ '\%(\%(^\|[^\\]\)\\\|&&\|||\||\)' . | |
12045 | 183 \ '\s*\({\s*\)\=\(#.*\)\=$' |
13912 | 184 endif |
2034 | 185 endfunction |
186 | |
187 function! s:find_continued_lnum(lnum) | |
188 let i = a:lnum | |
189 while i > 1 && s:is_continuation_line(getline(i - 1)) | |
190 let i -= 1 | |
191 endwhile | |
192 return i | |
193 endfunction | |
194 | |
13912 | 195 function! s:is_function_definition(line) |
196 return a:line =~ '^\s*\<\k\+\>\s*()\s*{' || | |
197 \ a:line =~ '^\s*{' || | |
18456 | 198 \ a:line =~ '^\s*function\s*\k\+\s*\%(()\)\?\s*{' |
13912 | 199 endfunction |
200 | |
16267 | 201 function! s:is_array(line) |
202 return a:line =~ '^\s*\<\k\+\>=(' | |
203 endfunction | |
204 | |
2034 | 205 function! s:is_case_label(line, pnum) |
206 if a:line !~ '^\s*(\=.*)' | |
207 return 0 | |
208 endif | |
209 | |
210 if a:pnum > 0 | |
211 let pine = getline(a:pnum) | |
212 if !(s:is_case(pine) || s:is_case_ended(pine)) | |
213 return 0 | |
214 endif | |
215 endif | |
216 | |
217 let suffix = substitute(a:line, '^\s*(\=', "", "") | |
218 let nesting = 0 | |
219 let i = 0 | |
220 let n = strlen(suffix) | |
221 while i < n | |
222 let c = suffix[i] | |
223 let i += 1 | |
224 if c == '\\' | |
225 let i += 1 | |
226 elseif c == '(' | |
227 let nesting += 1 | |
228 elseif c == ')' | |
229 if nesting == 0 | |
230 return 1 | |
231 endif | |
232 let nesting -= 1 | |
233 endif | |
234 endwhile | |
235 return 0 | |
236 endfunction | |
237 | |
238 function! s:is_case(line) | |
239 return a:line =~ '^\s*case\>' | |
240 endfunction | |
241 | |
242 function! s:is_case_break(line) | |
243 return a:line =~ '^\s*;[;&]' | |
244 endfunction | |
245 | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
246 function! s:is_here_doc(line) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
247 if a:line =~ '^\w\+$' |
16267 | 248 let here_pat = '<<-\?'. s:escape(a:line). '\$' |
249 return search(here_pat, 'bnW') > 0 | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
250 endif |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
251 return 0 |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
252 endfunction |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
253 |
2034 | 254 function! s:is_case_ended(line) |
255 return s:is_case_break(a:line) || a:line =~ ';[;&]\s*\%(#.*\)\=$' | |
256 endfunction | |
257 | |
5555 | 258 function! s:is_case_empty(line) |
259 if a:line =~ '^\s*$' || a:line =~ '^\s*#' | |
260 return s:is_case_empty(getline(v:lnum - 1)) | |
261 else | |
262 return a:line =~ '^\s*case\>' | |
263 endif | |
264 endfunction | |
265 | |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
266 function! s:escape(pattern) |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
267 return '\V'. escape(a:pattern, '\\') |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
268 endfunction |
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8246
diff
changeset
|
269 |
13912 | 270 function! s:is_empty(line) |
271 return a:line =~ '^\s*$' | |
272 endfunction | |
273 | |
274 function! s:end_block(line) | |
275 return a:line =~ '^\s*}' | |
276 endfunction | |
277 | |
278 function! s:start_block(line) | |
279 return a:line =~ '{\s*\(#.*\)\?$' | |
280 endfunction | |
281 | |
282 function! s:find_start_block(lnum) | |
283 let i = a:lnum | |
284 while i > 1 && !s:start_block(getline(i)) | |
285 let i -= 1 | |
286 endwhile | |
287 return i | |
288 endfunction | |
289 | |
290 function! s:is_comment(line) | |
291 return a:line =~ '^\s*#' | |
292 endfunction | |
293 | |
16267 | 294 function! s:is_end_expression(line) |
295 return a:line =~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$' | |
296 endfunction | |
297 | |
298 function! s:is_bash() | |
299 return get(g:, 'is_bash', 0) || get(b:, 'is_bash', 0) | |
300 endfunction | |
301 | |
375 | 302 let &cpo = s:cpo_save |
303 unlet s:cpo_save |