Mercurial > vim
annotate runtime/indent/vim.vim @ 23737:34b4eb3a8458
Update runtime files.
Commit: https://github.com/vim/vim/commit/e0e3917554327f2524066f89fbbef9c83c1535da
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Jan 25 21:14:57 2021 +0100
Update runtime files.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 25 Jan 2021 21:30:05 +0100 |
parents | e2e2cc5d0856 |
children | 5b37a0bf7e3a |
rev | line source |
---|---|
7 | 1 " Vim indent file |
2 " Language: Vim script | |
3 " Maintainer: Bram Moolenaar <Bram@vim.org> | |
23737 | 4 " Last Change: 2021 Jan 21 |
7 | 5 |
6 " Only load this indent file when no other was loaded. | |
7 if exists("b:did_indent") | |
8 finish | |
9 endif | |
10 let b:did_indent = 1 | |
11 | |
12 setlocal indentexpr=GetVimIndent() | |
22441 | 13 setlocal indentkeys+==end,=},=else,=cat,=finall,=END,0\\,0=\"\\\ |
21499 | 14 setlocal indentkeys-=0# |
7 | 15 |
3557 | 16 let b:undo_indent = "setl indentkeys< indentexpr<" |
17 | |
7 | 18 " Only define the function once. |
19 if exists("*GetVimIndent") | |
20 finish | |
21 endif | |
3526
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2729
diff
changeset
|
22 let s:keepcpo= &cpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2729
diff
changeset
|
23 set cpo&vim |
7 | 24 |
25 function GetVimIndent() | |
3750 | 26 let ignorecase_save = &ignorecase |
27 try | |
28 let &ignorecase = 0 | |
29 return GetVimIndentIntern() | |
30 finally | |
31 let &ignorecase = ignorecase_save | |
32 endtry | |
33 endfunc | |
34 | |
14714
bdbb049c2aa8
patch 8.1.0369: continuation lines cannot contain comments
Christian Brabandt <cb@256bit.org>
parents:
9407
diff
changeset
|
35 let s:lineContPat = '^\s*\(\\\|"\\ \)' |
bdbb049c2aa8
patch 8.1.0369: continuation lines cannot contain comments
Christian Brabandt <cb@256bit.org>
parents:
9407
diff
changeset
|
36 |
3750 | 37 function GetVimIndentIntern() |
7 | 38 " Find a non-blank line above the current line. |
39 let lnum = prevnonblank(v:lnum - 1) | |
40 | |
23737 | 41 " The previous line, ignoring line continuation |
42 let prev_text_end = lnum > 0 ? getline(lnum) : '' | |
43 | |
14714
bdbb049c2aa8
patch 8.1.0369: continuation lines cannot contain comments
Christian Brabandt <cb@256bit.org>
parents:
9407
diff
changeset
|
44 " If the current line doesn't start with '\' or '"\ ' and below a line that |
bdbb049c2aa8
patch 8.1.0369: continuation lines cannot contain comments
Christian Brabandt <cb@256bit.org>
parents:
9407
diff
changeset
|
45 " starts with '\' or '"\ ', use the indent of the line above it. |
6238 | 46 let cur_text = getline(v:lnum) |
14714
bdbb049c2aa8
patch 8.1.0369: continuation lines cannot contain comments
Christian Brabandt <cb@256bit.org>
parents:
9407
diff
changeset
|
47 if cur_text !~ s:lineContPat |
bdbb049c2aa8
patch 8.1.0369: continuation lines cannot contain comments
Christian Brabandt <cb@256bit.org>
parents:
9407
diff
changeset
|
48 while lnum > 0 && getline(lnum) =~ s:lineContPat |
7 | 49 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 | |
23737 | 57 |
58 " the start of the previous line, skipping over line continuation | |
6238 | 59 let prev_text = getline(lnum) |
23573 | 60 let found_cont = 0 |
7 | 61 |
62 " Add a 'shiftwidth' after :if, :while, :try, :catch, :finally, :function | |
14714
bdbb049c2aa8
patch 8.1.0369: continuation lines cannot contain comments
Christian Brabandt <cb@256bit.org>
parents:
9407
diff
changeset
|
63 " and :else. Add it three times for a line that starts with '\' or '"\ ' |
bdbb049c2aa8
patch 8.1.0369: continuation lines cannot contain comments
Christian Brabandt <cb@256bit.org>
parents:
9407
diff
changeset
|
64 " after a line that doesn't (or g:vim_indent_cont if it exists). |
7 | 65 let ind = indent(lnum) |
18489 | 66 |
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 | |
74 if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc' | |
75 return indent(lnum) | |
76 endif | |
77 let lnum -= 1 | |
78 endwhile | |
79 return 0 | |
80 endif | |
81 if syn_here =~ 'vimLetHereDoc' | |
82 if synIDattr(synID(lnum, 1, 1), "name") !~ 'vimLetHereDoc' | |
83 " First line in heredoc: increase indent | |
84 return ind + shiftwidth() | |
85 endif | |
86 " Heredoc continues: no change in indent | |
87 return ind | |
88 endif | |
89 endif | |
90 | |
14714
bdbb049c2aa8
patch 8.1.0369: continuation lines cannot contain comments
Christian Brabandt <cb@256bit.org>
parents:
9407
diff
changeset
|
91 if cur_text =~ s:lineContPat && v:lnum > 1 && prev_text !~ s:lineContPat |
23573 | 92 let found_cont = 1 |
22 | 93 if exists("g:vim_indent_cont") |
94 let ind = ind + g:vim_indent_cont | |
95 else | |
7742
5f6f35a3cb12
commit https://github.com/vim/vim/commit/705ada1aff27ecd9c47c690df817d043c2ceb5e2
Christian Brabandt <cb@256bit.org>
parents:
6447
diff
changeset
|
96 let ind = ind + shiftwidth() * 3 |
22 | 97 endif |
9407
619a98a67f67
commit https://github.com/vim/vim/commit/e18dbe865d190e74fb5d43ac8bc6ac22507d0223
Christian Brabandt <cb@256bit.org>
parents:
8951
diff
changeset
|
98 elseif prev_text =~ '^\s*aug\%[roup]\s\+' && prev_text !~ '^\s*aug\%[roup]\s\+[eE][nN][dD]\>' |
7742
5f6f35a3cb12
commit https://github.com/vim/vim/commit/705ada1aff27ecd9c47c690df817d043c2ceb5e2
Christian Brabandt <cb@256bit.org>
parents:
6447
diff
changeset
|
99 let ind = ind + shiftwidth() |
2729
12f838be9c59
Updated runtime file. Fix Italian translations.
Bram Moolenaar <bram@vim.org>
parents:
395
diff
changeset
|
100 else |
6238 | 101 " A line starting with :au does not increment/decrement indent. |
102 if prev_text !~ '^\s*au\%[tocmd]' | |
22441 | 103 let i = match(prev_text, '\(^\||\)\s*\(export\s\+\)\?\({\|\(if\|wh\%[ile]\|for\|try\|cat\%[ch]\|fina\|finall\%[y]\|fu\%[nction]\|def\|el\%[seif]\)\>\)') |
6238 | 104 if i >= 0 |
7742
5f6f35a3cb12
commit https://github.com/vim/vim/commit/705ada1aff27ecd9c47c690df817d043c2ceb5e2
Christian Brabandt <cb@256bit.org>
parents:
6447
diff
changeset
|
105 let ind += shiftwidth() |
6238 | 106 if strpart(prev_text, i, 1) == '|' && has('syntax_items') |
107 \ && synIDattr(synID(lnum, i, 1), "name") =~ '\(Comment\|String\)$' | |
7742
5f6f35a3cb12
commit https://github.com/vim/vim/commit/705ada1aff27ecd9c47c690df817d043c2ceb5e2
Christian Brabandt <cb@256bit.org>
parents:
6447
diff
changeset
|
108 let ind -= shiftwidth() |
6238 | 109 endif |
2729
12f838be9c59
Updated runtime file. Fix Italian translations.
Bram Moolenaar <bram@vim.org>
parents:
395
diff
changeset
|
110 endif |
12f838be9c59
Updated runtime file. Fix Italian translations.
Bram Moolenaar <bram@vim.org>
parents:
395
diff
changeset
|
111 endif |
7 | 112 endif |
113 | |
114 " If the previous line contains an "end" after a pipe, but not in an ":au" | |
333 | 115 " command. And not when there is a backslash before the pipe. |
395 | 116 " And when syntax HL is enabled avoid a match inside a string. |
6238 | 117 let i = match(prev_text, '[^\\]|\s*\(ene\@!\)') |
118 if i > 0 && prev_text !~ '^\s*au\%[tocmd]' | |
395 | 119 if !has('syntax_items') || synIDattr(synID(lnum, i + 2, 1), "name") !~ '\(Comment\|String\)$' |
7742
5f6f35a3cb12
commit https://github.com/vim/vim/commit/705ada1aff27ecd9c47c690df817d043c2ceb5e2
Christian Brabandt <cb@256bit.org>
parents:
6447
diff
changeset
|
120 let ind = ind - shiftwidth() |
395 | 121 endif |
7 | 122 endif |
123 | |
23573 | 124 " For a line starting with "}" find the matching "{". If it is at the start |
125 " of the line align with it, probably end of a block. | |
126 " Use the mapped "%" from matchit to find the match, otherwise we may match | |
127 " a { inside a comment or string. | |
128 if cur_text =~ '^\s*}' | |
129 if maparg('%') != '' | |
130 exe v:lnum | |
131 silent! normal % | |
132 if line('.') < v:lnum && getline('.') =~ '^\s*{' | |
133 let ind = indent('.') | |
134 endif | |
135 else | |
136 " todo: use searchpair() to find a match | |
137 endif | |
138 endif | |
139 | |
140 " Below a line starting with "}" find the matching "{". If it is at the | |
141 " end of the line we must be below the end of a dictionary. | |
142 if prev_text =~ '^\s*}' | |
143 if maparg('%') != '' | |
144 exe lnum | |
145 silent! normal % | |
146 if line('.') == lnum || getline('.') !~ '^\s*{' | |
147 let ind = ind - shiftwidth() | |
148 endif | |
149 else | |
150 " todo: use searchpair() to find a match | |
151 endif | |
152 endif | |
153 | |
154 " Below a line starting with "]" we must be below the end of a list. | |
23737 | 155 if prev_text_end =~ '^\s*]' |
23573 | 156 let ind = ind - shiftwidth() |
157 endif | |
158 | |
159 " A line ending in "{"/"[} is most likely the start of a dict/list literal, | |
160 " indent the next line more. Not for a continuation line. | |
23737 | 161 if prev_text_end =~ '[{[]\s*$' && !found_cont |
23573 | 162 let ind = ind + shiftwidth() |
163 endif | |
7 | 164 |
165 " Subtract a 'shiftwidth' on a :endif, :endwhile, :catch, :finally, :endtry, | |
19181
94eda51ba9ba
patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents:
18489
diff
changeset
|
166 " :endfun, :enddef, :else and :augroup END. |
23573 | 167 if cur_text =~ '^\s*\(ene\@!\|cat\|finall\|el\|aug\%[roup]\s\+[eE][nN][dD]\)' |
7742
5f6f35a3cb12
commit https://github.com/vim/vim/commit/705ada1aff27ecd9c47c690df817d043c2ceb5e2
Christian Brabandt <cb@256bit.org>
parents:
6447
diff
changeset
|
168 let ind = ind - shiftwidth() |
7 | 169 endif |
170 | |
171 return ind | |
172 endfunction | |
173 | |
3526
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2729
diff
changeset
|
174 let &cpo = s:keepcpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2729
diff
changeset
|
175 unlet s:keepcpo |
dd6c2497c997
Fix more 'cpo' issues in runtime files.
Bram Moolenaar <bram@vim.org>
parents:
2729
diff
changeset
|
176 |
7 | 177 " vim:sw=2 |