blob: b27d93c3a23d654b42dfe92ded2c177ceb14cb66 [file] [log] [blame]
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01001" Vim indent file
2" Language: Rust
3" Author: Chris Morgan <me@chrismorgan.info>
Bram Moolenaar3ec574f2017-06-13 18:12:01 +02004" Last Change: 2017 Jun 13
dkearns0382f052023-08-29 05:32:59 +10005" 2023 Aug 28 by Vim Project (undo_indent)
Bram Moolenaar3c2881d2017-03-21 19:18:29 +01006" For bugs, patches and license go to https://github.com/rust-lang/rust.vim
7
8" Only load this indent file when no other was loaded.
9if exists("b:did_indent")
10 finish
11endif
12let b:did_indent = 1
13
14setlocal cindent
15setlocal cinoptions=L0,(0,Ws,J1,j1
16setlocal cinkeys=0{,0},!^F,o,O,0[,0]
17" Don't think cinwords will actually do anything at all... never mind
18setlocal cinwords=for,if,else,while,loop,impl,mod,unsafe,trait,struct,enum,fn,extern
19
20" Some preliminary settings
21setlocal nolisp " Make sure lisp indenting doesn't supersede us
22setlocal autoindent " indentexpr isn't much help otherwise
23" Also do indentkeys, otherwise # gets shoved to column 0 :-/
24setlocal indentkeys=0{,0},!^F,o,O,0[,0]
25
26setlocal indentexpr=GetRustIndent(v:lnum)
27
dkearns0382f052023-08-29 05:32:59 +100028let b:undo_indent = "setlocal cindent< cinoptions< cinkeys< cinwords< lisp< autoindent< indentkeys< indentexpr<"
29
Bram Moolenaar3c2881d2017-03-21 19:18:29 +010030" Only define the function once.
31if exists("*GetRustIndent")
32 finish
33endif
34
35let s:save_cpo = &cpo
36set cpo&vim
37
38" Come here when loading the script the first time.
39
40function! s:get_line_trimmed(lnum)
41 " Get the line and remove a trailing comment.
42 " Use syntax highlighting attributes when possible.
43 " NOTE: this is not accurate; /* */ or a line continuation could trick it
44 let line = getline(a:lnum)
45 let line_len = strlen(line)
46 if has('syntax_items')
47 " If the last character in the line is a comment, do a binary search for
48 " the start of the comment. synID() is slow, a linear search would take
49 " too long on a long line.
50 if synIDattr(synID(a:lnum, line_len, 1), "name") =~ 'Comment\|Todo'
51 let min = 1
52 let max = line_len
53 while min < max
54 let col = (min + max) / 2
55 if synIDattr(synID(a:lnum, col, 1), "name") =~ 'Comment\|Todo'
56 let max = col
57 else
58 let min = col + 1
59 endif
60 endwhile
61 let line = strpart(line, 0, min - 1)
62 endif
63 return substitute(line, "\s*$", "", "")
64 else
65 " Sorry, this is not complete, nor fully correct (e.g. string "//").
66 " Such is life.
67 return substitute(line, "\s*//.*$", "", "")
68 endif
69endfunction
70
71function! s:is_string_comment(lnum, col)
72 if has('syntax_items')
73 for id in synstack(a:lnum, a:col)
74 let synname = synIDattr(id, "name")
75 if synname == "rustString" || synname =~ "^rustComment"
76 return 1
77 endif
78 endfor
79 else
80 " without syntax, let's not even try
81 return 0
82 endif
83endfunction
84
85function GetRustIndent(lnum)
86
87 " Starting assumption: cindent (called at the end) will do it right
88 " normally. We just want to fix up a few cases.
89
90 let line = getline(a:lnum)
91
92 if has('syntax_items')
93 let synname = synIDattr(synID(a:lnum, 1, 1), "name")
94 if synname == "rustString"
95 " If the start of the line is in a string, don't change the indent
96 return -1
97 elseif synname =~ '\(Comment\|Todo\)'
98 \ && line !~ '^\s*/\*' " not /* opening line
99 if synname =~ "CommentML" " multi-line
100 if line !~ '^\s*\*' && getline(a:lnum - 1) =~ '^\s*/\*'
101 " This is (hopefully) the line after a /*, and it has no
102 " leader, so the correct indentation is that of the
103 " previous line.
104 return GetRustIndent(a:lnum - 1)
105 endif
106 endif
107 " If it's in a comment, let cindent take care of it now. This is
108 " for cases like "/*" where the next line should start " * ", not
109 " "* " as the code below would otherwise cause for module scope
110 " Fun fact: " /*\n*\n*/" takes two calls to get right!
111 return cindent(a:lnum)
112 endif
113 endif
114
115 " cindent gets second and subsequent match patterns/struct members wrong,
116 " as it treats the comma as indicating an unfinished statement::
117 "
118 " match a {
119 " b => c,
120 " d => e,
121 " f => g,
122 " };
123
124 " Search backwards for the previous non-empty line.
125 let prevlinenum = prevnonblank(a:lnum - 1)
126 let prevline = s:get_line_trimmed(prevlinenum)
127 while prevlinenum > 1 && prevline !~ '[^[:blank:]]'
128 let prevlinenum = prevnonblank(prevlinenum - 1)
129 let prevline = s:get_line_trimmed(prevlinenum)
130 endwhile
131
132 " Handle where clauses nicely: subsequent values should line up nicely.
133 if prevline[len(prevline) - 1] == ","
134 \ && prevline =~# '^\s*where\s'
135 return indent(prevlinenum) + 6
136 endif
137
Raphael47866802023-08-21 02:42:39 +0800138 "match newline after struct with generic bound like
139 "struct SomeThing<T>
140 "| <-- newline indent should same as prevline
141 if prevline[len(prevline) - 1] == ">"
142 \ && prevline =~# "\s*struct.*>$"
143 return indent(prevlinenum)
144 endif
145
146 "match newline after where like:
147 "struct SomeThing<T>
148 "where
149 " T: Display,
150 if prevline =~# '^\s*where$'
151 return indent(prevlinenum) + 4
152 endif
153
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100154 if prevline[len(prevline) - 1] == ","
155 \ && s:get_line_trimmed(a:lnum) !~ '^\s*[\[\]{}]'
156 \ && prevline !~ '^\s*fn\s'
157 \ && prevline !~ '([^()]\+,$'
158 \ && s:get_line_trimmed(a:lnum) !~ '^\s*\S\+\s*=>'
159 " Oh ho! The previous line ended in a comma! I bet cindent will try to
160 " take this too far... For now, let's normally use the previous line's
161 " indent.
162
163 " One case where this doesn't work out is where *this* line contains
164 " square or curly brackets; then we normally *do* want to be indenting
165 " further.
166 "
167 " Another case where we don't want to is one like a function
168 " definition with arguments spread over multiple lines:
169 "
170 " fn foo(baz: Baz,
171 " baz: Baz) // <-- cindent gets this right by itself
172 "
173 " Another case is similar to the previous, except calling a function
174 " instead of defining it, or any conditional expression that leaves
175 " an open paren:
176 "
177 " foo(baz,
178 " baz);
179 "
180 " if baz && (foo ||
181 " bar) {
182 "
183 " Another case is when the current line is a new match arm.
184 "
185 " There are probably other cases where we don't want to do this as
186 " well. Add them as needed.
187 return indent(prevlinenum)
188 endif
189
190 if !has("patch-7.4.355")
191 " cindent before 7.4.355 doesn't do the module scope well at all; e.g.::
192 "
193 " static FOO : &'static [bool] = [
194 " true,
195 " false,
196 " false,
197 " true,
198 " ];
199 "
200 " uh oh, next statement is indented further!
201
202 " Note that this does *not* apply the line continuation pattern properly;
203 " that's too hard to do correctly for my liking at present, so I'll just
204 " start with these two main cases (square brackets and not returning to
205 " column zero)
206
207 call cursor(a:lnum, 1)
208 if searchpair('{\|(', '', '}\|)', 'nbW',
209 \ 's:is_string_comment(line("."), col("."))') == 0
210 if searchpair('\[', '', '\]', 'nbW',
211 \ 's:is_string_comment(line("."), col("."))') == 0
212 " Global scope, should be zero
213 return 0
214 else
215 " At the module scope, inside square brackets only
216 "if getline(a:lnum)[0] == ']' || search('\[', '', '\]', 'nW') == a:lnum
217 if line =~ "^\\s*]"
218 " It's the closing line, dedent it
219 return 0
220 else
Bram Moolenaar3ec574f2017-06-13 18:12:01 +0200221 return shiftwidth()
Bram Moolenaar3c2881d2017-03-21 19:18:29 +0100222 endif
223 endif
224 endif
225 endif
226
227 " Fall back on cindent, which does it mostly right
228 return cindent(a:lnum)
229endfunction
230
231let &cpo = s:save_cpo
232unlet s:save_cpo