blob: 7455287ec0214b3757d2a24f7e975db68f724623 [file] [log] [blame]
Bram Moolenaar938ae282023-02-20 20:44:55 +00001" Vim indent file
2" Language: fish
3" Maintainer: Nicholas Boyle (github.com/nickeb96)
4" Repository: https://github.com/nickeb96/fish.vim
5" Last Change: February 4, 2023
6
7if exists("b:did_indent")
8 finish
9endif
10let b:did_indent = 1
11
12setlocal indentexpr=GetFishIndent(v:lnum)
13setlocal indentkeys+==end,=else,=case
14
15function s:PrevCmdStart(linenum)
16 let l:linenum = a:linenum
17 " look for the first line that isn't a line continuation
18 while l:linenum > 1 && getline(l:linenum - 1) =~# '\\$'
19 let l:linenum = l:linenum - 1
20 endwhile
21 return l:linenum
22endfunction
23
24function GetFishIndent(lnum)
25 let l:shiftwidth = shiftwidth()
26
27 let l:prevlnum = prevnonblank(a:lnum - 1)
28 if l:prevlnum ==# 0
29 return 0
30 endif
31
32 " if the previous line ended with a line continuation
33 if getline(a:lnum - 1) =~# '\\$'
34 if a:lnum ==# 0 || getline(a:lnum - 2) !~# '\\$'
35 " this is the first line continuation in a chain, so indent it
36 return indent(a:lnum - 1) + l:shiftwidth
37 else
38 " use the same indentation as the previous continued line
39 return indent(a:lnum - 1)
40 endif
41 endif
42
43 let l:prevlnum = s:PrevCmdStart(l:prevlnum)
44
45 let l:prevline = getline(l:prevlnum)
46 if l:prevline =~# '^\s*\(begin\|if\|else\|while\|for\|function\|case\|switch\)\>'
47 let l:indent = l:shiftwidth
48 else
49 let l:indent = 0
50 endif
51
52 let l:line = getline(a:lnum)
53 if l:line =~# '^\s*end\>'
54 " find end's matching start
55 let l:depth = 1
56 let l:currentlnum = a:lnum
57 while l:depth > 0 && l:currentlnum > 0
58 let l:currentlnum = s:PrevCmdStart(prevnonblank(l:currentlnum - 1))
59 let l:currentline = getline(l:currentlnum)
60 if l:currentline =~# '^\s*end\>'
61 let l:depth = l:depth + 1
62 elseif l:currentline =~# '^\s*\(begin\|if\|while\|for\|function\|switch\)\>'
63 let l:depth = l:depth - 1
64 endif
65 endwhile
66 if l:currentline =~# '^\s*switch\>'
67 return indent(l:currentlnum)
68 else
69 return indent(l:prevlnum) + l:indent - l:shiftwidth
70 endif
71 elseif l:line =~# '^\s*else\>'
72 return indent(l:prevlnum) + l:indent - l:shiftwidth
73 elseif l:line =~# '^\s*case\>'
74 if getline(l:prevlnum) =~# '^\s*switch\>'
75 return indent(l:prevlnum) + l:indent
76 else
77 return indent(l:prevlnum) + l:indent - l:shiftwidth
78 endif
79 else
80 return indent(l:prevlnum) + l:indent
81 endif
82endfunction