blob: d5d0d94b52b205c8683c63bbbcfdd6099f23e6a1 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001" Vim indent file
2" Language: Vim script
3" Maintainer: Bram Moolenaar <Bram@vim.org>
Bram Moolenaar113cb512021-11-07 20:27:04 +00004" Last Change: 2021 Nov 03
Bram Moolenaar071d4272004-06-13 20:20:40 +00005
6" Only load this indent file when no other was loaded.
7if exists("b:did_indent")
8 finish
9endif
10let b:did_indent = 1
11
12setlocal indentexpr=GetVimIndent()
Bram Moolenaard58a3bf2020-09-28 21:48:16 +020013setlocal indentkeys+==end,=},=else,=cat,=finall,=END,0\\,0=\"\\\
Bram Moolenaar2547aa92020-07-26 17:00:44 +020014setlocal indentkeys-=0#
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
Bram Moolenaarc8734422012-06-01 22:38:45 +020016let b:undo_indent = "setl indentkeys< indentexpr<"
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018" Only define the function once.
19if exists("*GetVimIndent")
20 finish
21endif
Bram Moolenaar8e52a592012-05-18 21:49:28 +020022let s:keepcpo= &cpo
23set cpo&vim
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
25function GetVimIndent()
Bram Moolenaar9b451252012-08-15 17:43:31 +020026 let ignorecase_save = &ignorecase
27 try
28 let &ignorecase = 0
29 return GetVimIndentIntern()
30 finally
31 let &ignorecase = ignorecase_save
32 endtry
33endfunc
34
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020035let s:lineContPat = '^\s*\(\\\|"\\ \)'
36
Bram Moolenaar9b451252012-08-15 17:43:31 +020037function GetVimIndentIntern()
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 " Find a non-blank line above the current line.
39 let lnum = prevnonblank(v:lnum - 1)
40
Bram Moolenaare0e39172021-01-25 21:14:57 +010041 " The previous line, ignoring line continuation
42 let prev_text_end = lnum > 0 ? getline(lnum) : ''
43
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020044 " If the current line doesn't start with '\' or '"\ ' and below a line that
45 " starts with '\' or '"\ ', use the indent of the line above it.
Bram Moolenaar91e15e12014-09-19 22:38:48 +020046 let cur_text = getline(v:lnum)
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020047 if cur_text !~ s:lineContPat
48 while lnum > 0 && getline(lnum) =~ s:lineContPat
Bram Moolenaar071d4272004-06-13 20:20:40 +000049 let lnum = lnum - 1
50 endwhile
51 endif
52
53 " At the start of the file use zero indent.
54 if lnum == 0
55 return 0
56 endif
Bram Moolenaare0e39172021-01-25 21:14:57 +010057
58 " the start of the previous line, skipping over line continuation
Bram Moolenaar91e15e12014-09-19 22:38:48 +020059 let prev_text = getline(lnum)
Bram Moolenaar82be4842021-01-11 19:40:15 +010060 let found_cont = 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
62 " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020063 " and :else. Add it three times for a line that starts with '\' or '"\ '
64 " after a line that doesn't (or g:vim_indent_cont if it exists).
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 let ind = indent(lnum)
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +010066
67 " In heredoc indenting works completely differently.
68 if has('syntax_items')
69 let syn_here = synIDattr(synID(v:lnum, 1, 1), "name")
70 if syn_here =~ 'vimLetHereDocStop'
71 " End of heredoc: use indent of matching start line
72 let lnum = v:lnum - 1
73 while lnum > 0
Bram Moolenaar11e3c5b2021-04-21 18:09:37 +020074 let attr = synIDattr(synID(lnum, 1, 1), "name")
75 if attr != '' && attr !~ 'vimLetHereDoc'
Bram Moolenaar1ff14ba2019-11-02 14:09:23 +010076 return indent(lnum)
77 endif
78 let lnum -= 1
79 endwhile
80 return 0
81 endif
82 if syn_here =~ 'vimLetHereDoc'
83 if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc'
84 " First line in heredoc: increase indent
85 return ind + shiftwidth()
86 endif
87 " Heredoc continues: no change in indent
88 return ind
89 endif
90 endif
91
Bram Moolenaar67f8ab82018-09-11 22:37:29 +020092 if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat
Bram Moolenaar82be4842021-01-11 19:40:15 +010093 let found_cont = 1
Bram Moolenaard4755bb2004-09-02 19:12:26 +000094 if exists("g:vim_indent_cont")
95 let ind = ind + g:vim_indent_cont
96 else
Bram Moolenaar705ada12016-01-24 17:56:50 +010097 let ind = ind + shiftwidth() * 3
Bram Moolenaard4755bb2004-09-02 19:12:26 +000098 endif
Bram Moolenaare18dbe82016-07-02 21:42:23 +020099 elseif prev_text =~ '^\s*aug\%[roup]\s\+' && prev_text !~ '^\s*aug\%[roup]\s\+[eE][nN][dD]\>'
Bram Moolenaar705ada12016-01-24 17:56:50 +0100100 let ind = ind + shiftwidth()
Bram Moolenaarcab49df2011-03-22 17:40:10 +0100101 else
Bram Moolenaar91e15e12014-09-19 22:38:48 +0200102 " A line starting with :au does not increment/decrement indent.
Bram Moolenaar942db232021-02-13 18:14:48 +0100103 " A { may start a block or a dict. Assume that when a } follows it's a
104 " terminated dict.
105 if prev_text !~ '^\s*au\%[tocmd]' && prev_text !~ '^\s*{.*}'
Bram Moolenaard58a3bf2020-09-28 21:48:16 +0200106 let i = match(prev_text, '\(^\||\)\s*\(export\s\+\)\?\({\|\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\|finall\%[y]\|fu\%[nction]\|def\|el\%[seif]\)\>\)')
Bram Moolenaar91e15e12014-09-19 22:38:48 +0200107 if i >= 0
Bram Moolenaar705ada12016-01-24 17:56:50 +0100108 let ind += shiftwidth()
Bram Moolenaar91e15e12014-09-19 22:38:48 +0200109 if strpart(prev_text, i, 1) == '|' && has('syntax_items')
Bram Moolenaar113cb512021-11-07 20:27:04 +0000110 \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\|PatSep\)$'
Bram Moolenaar705ada12016-01-24 17:56:50 +0100111 let ind -= shiftwidth()
Bram Moolenaar91e15e12014-09-19 22:38:48 +0200112 endif
Bram Moolenaarcab49df2011-03-22 17:40:10 +0100113 endif
114 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 endif
116
117 " If the previous line contains an "end" after a pipe, but not in an ":au"
Bram Moolenaar520470a2005-06-16 21:59:56 +0000118 " command. And not when there is a backslash before the pipe.
Bram Moolenaardc27ac12005-07-06 22:35:45 +0000119 " And when syntax HL is enabled avoid a match inside a string.
Bram Moolenaar91e15e12014-09-19 22:38:48 +0200120 let i = match(prev_text, '[^\\]|\s*\(ene\@!\)')
121 if i > 0 && prev_text !~ '^\s*au\%[tocmd]'
Bram Moolenaardc27ac12005-07-06 22:35:45 +0000122 if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$'
Bram Moolenaar705ada12016-01-24 17:56:50 +0100123 let ind = ind - shiftwidth()
Bram Moolenaardc27ac12005-07-06 22:35:45 +0000124 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125 endif
126
Bram Moolenaar82be4842021-01-11 19:40:15 +0100127 " For a line starting with "}" find the matching "{". If it is at the start
128 " of the line align with it, probably end of a block.
129 " Use the mapped "%" from matchit to find the match, otherwise we may match
130 " a { inside a comment or string.
131 if cur_text =~ '^\s*}'
132 if maparg('%') != ''
133 exe v:lnum
134 silent! normal %
135 if line('.') < v:lnum && getline('.') =~ '^\s*{'
136 let ind = indent('.')
137 endif
138 else
139 " todo: use searchpair() to find a match
140 endif
141 endif
142
143 " Below a line starting with "}" find the matching "{". If it is at the
144 " end of the line we must be below the end of a dictionary.
145 if prev_text =~ '^\s*}'
146 if maparg('%') != ''
147 exe lnum
148 silent! normal %
149 if line('.') == lnum || getline('.') !~ '^\s*{'
150 let ind = ind - shiftwidth()
151 endif
152 else
153 " todo: use searchpair() to find a match
154 endif
155 endif
156
157 " Below a line starting with "]" we must be below the end of a list.
Bram Moolenaar942db232021-02-13 18:14:48 +0100158 " Include a "}" and "},} in case a dictionary ends too.
159 if prev_text_end =~ '^\s*\(},\=\s*\)\=]'
Bram Moolenaar82be4842021-01-11 19:40:15 +0100160 let ind = ind - shiftwidth()
161 endif
162
Bram Moolenaar942db232021-02-13 18:14:48 +0100163 let ends_in_comment = has('syntax_items')
Bram Moolenaar9faec4e2021-02-27 16:38:07 +0100164 \ && synIDattr(synID(lnum, len(getline(lnum)), 1), "name") =~ '\(Comment\|String\)$'
Bram Moolenaar942db232021-02-13 18:14:48 +0100165
Bram Moolenaar9faec4e2021-02-27 16:38:07 +0100166 " A line ending in "{" or "[" is most likely the start of a dict/list literal,
Bram Moolenaar942db232021-02-13 18:14:48 +0100167 " indent the next line more. Not for a continuation line or {{{.
168 if !ends_in_comment && prev_text_end =~ '\s[{[]\s*$' && !found_cont
Bram Moolenaar82be4842021-01-11 19:40:15 +0100169 let ind = ind + shiftwidth()
170 endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171
172 " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 " :endfun, :enddef, :else and :augroup END.
Bram Moolenaar82be4842021-01-11 19:40:15 +0100174 if cur_text =~ '^\s*\(ene\@!\|cat\|finall\|el\|aug\%[roup]\s\+[eE][nN][dD]\)'
Bram Moolenaar705ada12016-01-24 17:56:50 +0100175 let ind = ind - shiftwidth()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 endif
177
178 return ind
179endfunction
180
Bram Moolenaar8e52a592012-05-18 21:49:28 +0200181let &cpo = s:keepcpo
182unlet s:keepcpo
183
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184" vim:sw=2