Mercurial > vim
comparison runtime/indent/sh.vim @ 16267:b471858040bc
Update runtime files.
commit https://github.com/vim/vim/commit/62e1bb4a111e7ce570c30965f40a68a07a9da5b0
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Apr 8 16:25:07 2019 +0200
Update runtime files.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 08 Apr 2019 16:30:06 +0200 |
parents | a23c883685cb |
children | 1eaf34420bb3 |
comparison
equal
deleted
inserted
replaced
16266:a904f7ffa001 | 16267:b471858040bc |
---|---|
1 " Vim indent file | 1 " Vim indent file |
2 " Language: Shell Script | 2 " Language: Shell Script |
3 " Maintainer: Christian Brabandt <cb@256bit.org> | 3 " Maintainer: Christian Brabandt <cb@256bit.org> |
4 " Original Author: Nikolai Weibull <now@bitwi.se> | 4 " Original Author: Nikolai Weibull <now@bitwi.se> |
5 " Previous Maintainer: Peter Aronoff <telemachus@arpinum.org> | 5 " Previous Maintainer: Peter Aronoff <telemachus@arpinum.org> |
6 " Latest Revision: 2018-03-26 | 6 " Latest Revision: 2019-03-25 |
7 " License: Vim (see :h license) | 7 " License: Vim (see :h license) |
8 " Repository: https://github.com/chrisbra/vim-sh-indent | 8 " Repository: https://github.com/chrisbra/vim-sh-indent |
9 " Changelog: | 9 " Changelog: |
10 " 20190325 - Indent fi; correctly | |
11 " https://github.com/chrisbra/vim-sh-indent/issues/14 | |
12 " 20190319 - Indent arrays (only zsh and bash) | |
13 " https://github.com/chrisbra/vim-sh-indent/issues/13 | |
14 " 20190316 - Make use of searchpairpos for nested if sections | |
15 " fixes https://github.com/chrisbra/vim-sh-indent/issues/11 | |
16 " 20190201 - Better check for closing if sections | |
10 " 20180724 - make check for zsh syntax more rigid (needs word-boundaries) | 17 " 20180724 - make check for zsh syntax more rigid (needs word-boundaries) |
11 " 20180326 - better support for line continuation | 18 " 20180326 - better support for line continuation |
12 " 20180325 - better detection of function definitions | 19 " 20180325 - better detection of function definitions |
13 " 20180127 - better support for zsh complex commands | 20 " 20180127 - better support for zsh complex commands |
14 " 20170808: - better indent of line continuation | 21 " 20170808: - better indent of line continuation |
57 endif | 64 endif |
58 return Value | 65 return Value |
59 endfunction | 66 endfunction |
60 | 67 |
61 function! GetShIndent() | 68 function! GetShIndent() |
69 let curline = getline(v:lnum) | |
62 let lnum = prevnonblank(v:lnum - 1) | 70 let lnum = prevnonblank(v:lnum - 1) |
63 if lnum == 0 | 71 if lnum == 0 |
64 return 0 | 72 return 0 |
65 endif | 73 endif |
66 let line = getline(lnum) | 74 let line = getline(lnum) |
70 let ind = indent(lnum) | 78 let ind = indent(lnum) |
71 | 79 |
72 " Check contents of previous lines | 80 " Check contents of previous lines |
73 if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' || | 81 if line =~ '^\s*\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>' || |
74 \ (&ft is# 'zsh' && line =~ '\<\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>') | 82 \ (&ft is# 'zsh' && line =~ '\<\%(if\|then\|do\|else\|elif\|case\|while\|until\|for\|select\|foreach\)\>') |
75 if line !~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$' | 83 if !s:is_end_expression(line) |
76 let ind += s:indent_value('default') | 84 let ind += s:indent_value('default') |
77 endif | 85 endif |
78 elseif s:is_case_label(line, pnum) | 86 elseif s:is_case_label(line, pnum) |
79 if !s:is_case_ended(line) | 87 if !s:is_case_ended(line) |
80 let ind += s:indent_value('case-statements') | 88 let ind += s:indent_value('case-statements') |
82 " function definition | 90 " function definition |
83 elseif s:is_function_definition(line) | 91 elseif s:is_function_definition(line) |
84 if line !~ '}\s*\%(#.*\)\=$' | 92 if line !~ '}\s*\%(#.*\)\=$' |
85 let ind += s:indent_value('default') | 93 let ind += s:indent_value('default') |
86 endif | 94 endif |
95 " array (only works for zsh or bash) | |
96 elseif s:is_array(line) && line !~ ')\s*$' && (&ft is# 'zsh' || s:is_bash()) | |
97 let ind += s:indent_value('continuation-line') | |
98 " end of array | |
99 elseif curline =~ '^\s*)$' | |
100 let ind -= s:indent_value('continuation-line') | |
87 elseif s:is_continuation_line(line) | 101 elseif s:is_continuation_line(line) |
88 if pnum == 0 || !s:is_continuation_line(pline) | 102 if pnum == 0 || !s:is_continuation_line(pline) |
89 let ind += s:indent_value('continuation-line') | 103 let ind += s:indent_value('continuation-line') |
90 endif | 104 endif |
91 elseif s:end_block(line) && !s:start_block(line) | 105 elseif s:end_block(line) && !s:start_block(line) |
92 let ind -= s:indent_value('default') | 106 let ind -= s:indent_value('default') |
93 elseif pnum != 0 && s:is_continuation_line(pline) && !s:end_block(getline(v:lnum)) | 107 elseif pnum != 0 && |
108 \ s:is_continuation_line(pline) && | |
109 \ !s:end_block(curline) && | |
110 \ !s:is_end_expression(curline) | |
94 " only add indent, if line and pline is in the same block | 111 " only add indent, if line and pline is in the same block |
95 let i = v:lnum | 112 let i = v:lnum |
96 let ind2 = indent(s:find_continued_lnum(pnum)) | 113 let ind2 = indent(s:find_continued_lnum(pnum)) |
97 while !s:is_empty(getline(i)) && i > pnum | 114 while !s:is_empty(getline(i)) && i > pnum |
98 let i -= 1 | 115 let i -= 1 |
104 endif | 121 endif |
105 endif | 122 endif |
106 | 123 |
107 let pine = line | 124 let pine = line |
108 " Check content of current line | 125 " Check content of current line |
109 let line = getline(v:lnum) | 126 let line = curline |
110 if line =~ '^\s*\%(then\|do\|else\|elif\|fi\|done\|end\)\>' || s:end_block(line) | 127 " Current line is a endif line, so get indent from start of "if condition" line |
128 " TODO: should we do the same for other "end" lines? | |
129 if curline =~ '^\s*\%(fi\);\?\s*\%(#.*\)\=$' | |
130 let previous_line = searchpair('\<if\>', '', '\<fi\>', 'bnW') | |
131 if previous_line > 0 | |
132 let ind = indent(previous_line) | |
133 endif | |
134 elseif line =~ '^\s*\%(then\|do\|else\|elif\|done\|end\)\>' || s:end_block(line) | |
111 let ind -= s:indent_value('default') | 135 let ind -= s:indent_value('default') |
112 elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1)) | 136 elseif line =~ '^\s*esac\>' && s:is_case_empty(getline(v:lnum - 1)) |
113 let ind -= s:indent_value('default') | 137 let ind -= s:indent_value('default') |
114 elseif line =~ '^\s*esac\>' | 138 elseif line =~ '^\s*esac\>' |
115 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? | 139 let ind -= (s:is_case_label(pine, lnum) && s:is_case_ended(pine) ? |
163 | 187 |
164 function! s:is_function_definition(line) | 188 function! s:is_function_definition(line) |
165 return a:line =~ '^\s*\<\k\+\>\s*()\s*{' || | 189 return a:line =~ '^\s*\<\k\+\>\s*()\s*{' || |
166 \ a:line =~ '^\s*{' || | 190 \ a:line =~ '^\s*{' || |
167 \ a:line =~ '^\s*function\s*\w\S\+\s*\%(()\)\?\s*{' | 191 \ a:line =~ '^\s*function\s*\w\S\+\s*\%(()\)\?\s*{' |
192 endfunction | |
193 | |
194 function! s:is_array(line) | |
195 return a:line =~ '^\s*\<\k\+\>=(' | |
168 endfunction | 196 endfunction |
169 | 197 |
170 function! s:is_case_label(line, pnum) | 198 function! s:is_case_label(line, pnum) |
171 if a:line !~ '^\s*(\=.*)' | 199 if a:line !~ '^\s*(\=.*)' |
172 return 0 | 200 return 0 |
208 return a:line =~ '^\s*;[;&]' | 236 return a:line =~ '^\s*;[;&]' |
209 endfunction | 237 endfunction |
210 | 238 |
211 function! s:is_here_doc(line) | 239 function! s:is_here_doc(line) |
212 if a:line =~ '^\w\+$' | 240 if a:line =~ '^\w\+$' |
213 let here_pat = '<<-\?'. s:escape(a:line). '\$' | 241 let here_pat = '<<-\?'. s:escape(a:line). '\$' |
214 return search(here_pat, 'bnW') > 0 | 242 return search(here_pat, 'bnW') > 0 |
215 endif | 243 endif |
216 return 0 | 244 return 0 |
217 endfunction | 245 endfunction |
218 | 246 |
219 function! s:is_case_ended(line) | 247 function! s:is_case_ended(line) |
254 | 282 |
255 function! s:is_comment(line) | 283 function! s:is_comment(line) |
256 return a:line =~ '^\s*#' | 284 return a:line =~ '^\s*#' |
257 endfunction | 285 endfunction |
258 | 286 |
287 function! s:is_end_expression(line) | |
288 return a:line =~ '\<\%(fi\|esac\|done\|end\)\>\s*\%(#.*\)\=$' | |
289 endfunction | |
290 | |
291 function! s:is_bash() | |
292 return get(g:, 'is_bash', 0) || get(b:, 'is_bash', 0) | |
293 endfunction | |
294 | |
259 let &cpo = s:cpo_save | 295 let &cpo = s:cpo_save |
260 unlet s:cpo_save | 296 unlet s:cpo_save |