Skip to content

Commit 1d08ceb

Browse files
chris-morganthestinger
authored andcommitted
Fix comment indenting properly for Vim files.
Indentation now works correctly on subsequent lines of a multi-line comment, whether there are leaders (` * `) or not. (Formerly it was incorrectly doing a two-space indent if there was no leader.) By default, this no longer puts a ` * ` leader on `/*!` comments, as that appears to be the current convention in the Rust source code, but that can easily be re-enabled if desired: let g:rust_bang_comment_leader = 1
1 parent 00da76d commit 1d08ceb

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

src/etc/vim/ftplugin/rust.vim

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
" Vim syntax file
22
" Language: Rust
33
" Maintainer: Chris Morgan <[email protected]>
4-
" Last Change: 2013 Jul 6
4+
" Last Change: 2013 Jul 10
55

66
if exists("b:did_ftplugin")
77
finish
88
endif
99
let b:did_ftplugin = 1
1010

11-
setlocal comments=s1:/*,mb:*,ex:*/,:///,://!,://
11+
" The rust source code at present seems to typically omit a leader on /*!
12+
" comments, so we'll use that as our default, but make it easy to switch.
13+
" This does not affect indentation at all (I tested it with and without
14+
" leader), merely whether a leader is inserted by default or not.
15+
if exists("g:rust_bang_comment_leader") && g:rust_bang_comment_leader == 1
16+
" Why is the `,s0:/*,mb:\ ,ex:*/` there, you ask? I don't understand why,
17+
" but without it, */ gets indented one space even if there were no
18+
" leaders. I'm fairly sure that's a Vim bug.
19+
setlocal comments=s1:/*,mb:*,ex:*/,s0:/*,mb:\ ,ex:*/,:///,://!,://
20+
else
21+
setlocal comments=s0:/*!,m:\ ,ex:*/,s1:/*,mb:*,ex:*/,:///,://!,://
22+
endif
1223
setlocal commentstring=//%s
1324
setlocal formatoptions-=t formatoptions+=croqnlj
1425

src/etc/vim/indent/rust.vim

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,23 @@ function GetRustIndent(lnum)
6666
" Starting assumption: cindent (called at the end) will do it right
6767
" normally. We just want to fix up a few cases.
6868

69+
let line = getline(a:lnum)
70+
6971
if has('syntax_items')
70-
if synIDattr(synID(a:lnum, 1, 1), "name") == "rustString"
72+
let synname = synIDattr(synID(a:lnum, 1, 1), "name")
73+
if synname == "rustString"
7174
" If the start of the line is in a string, don't change the indent
7275
return -1
73-
elseif synIDattr(synID(a:lnum, 1, 1), "name") =~ "\\(Comment\\|Todo\\)"
74-
\ && getline(a:lnum) !~ "^\\s*/\\*"
76+
elseif synname =~ "\\(Comment\\|Todo\\)"
77+
\ && line !~ "^\\s*/\\*" " not /* opening line
78+
if synname =~ "CommentML" " multi-line
79+
if line !~ "^\\s*\\*" && getline(a:lnum - 1) =~ "^\\s*/\\*"
80+
" This is (hopefully) the line after a /*, and it has no
81+
" leader, so the correct indentation is that of the
82+
" previous line.
83+
return GetRustIndent(a:lnum - 1)
84+
endif
85+
endif
7586
" If it's in a comment, let cindent take care of it now. This is
7687
" for cases like "/*" where the next line should start " * ", not
7788
" "* " as the code below would otherwise cause for module scope
@@ -114,7 +125,6 @@ function GetRustIndent(lnum)
114125
" start with these two main cases (square brackets and not returning to
115126
" column zero)
116127

117-
let line = getline(a:lnum)
118128
call cursor(a:lnum, 1)
119129
if searchpair('{\|(', '', '}\|)', 'nbW') == 0
120130
if searchpair('\[', '', '\]', 'nbW') == 0

src/etc/vim/syntax/rust.vim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ syn match rustFloat display "\<[0-9][0-9_]*\.[0-9_]\+\%([eE][+-]\=[0-9
117117
syn match rustLifetime display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*"
118118
syn match rustCharacter "'\([^'\\]\|\\\(['nrt\\\"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'"
119119

120-
syn region rustComment start="/\*" end="\*/" contains=rustTodo
120+
syn region rustCommentML start="/\*" end="\*/" contains=rustTodo
121121
syn region rustComment start="//" skip="\\$" end="$" contains=rustTodo keepend
122-
syn region rustCommentDoc start="/\*\%(!\|\*/\@!\)" end="\*/" contains=rustTodo
122+
syn region rustCommentMLDoc start="/\*\%(!\|\*/\@!\)" end="\*/" contains=rustTodo
123123
syn region rustCommentDoc start="//[/!]" skip="\\$" end="$" contains=rustTodo keepend
124124

125125
syn keyword rustTodo contained TODO FIXME XXX NB NOTE
@@ -151,7 +151,9 @@ hi def link rustModPath Include
151151
hi def link rustModPathSep Delimiter
152152
hi def link rustFuncName Function
153153
hi def link rustFuncCall Function
154+
hi def link rustCommentMLDoc rustCommentDoc
154155
hi def link rustCommentDoc SpecialComment
156+
hi def link rustCommentML rustComment
155157
hi def link rustComment Comment
156158
hi def link rustAssert PreCondit
157159
hi def link rustFail PreCondit

0 commit comments

Comments
 (0)