robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqvim-patch:cb0b4cf: Fix a few more typos (#39596)37596fcd29

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