Skip to content

Commit 1191ae8

Browse files
committed
auto merge of #10793 : chris-morgan/rust/2013-12-04-vim-updates, r=Aatch
- Implement comment nesting (the implementation is quite ugly at present and is not quite correct; note the big comment in that area). - Highlight invalid escape sequences as errors. - Fix up various inconsistencies and incorrectnesses in number highlighting. - Update prelude items (``std::io::{Buffer, Writer, Reader, Seek}``). - Highlight the ``proc`` keyword. - Remove %-formatting sequence highlighting (a relic of old formatting). - Don't highlight TODO in strings (it's unconventional).
2 parents 9635c76 + c9eed57 commit 1191ae8

File tree

1 file changed

+42
-36
lines changed

1 file changed

+42
-36
lines changed

Diff for: src/etc/vim/syntax/rust.vim

+42-36
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" Maintainer: Patrick Walton <[email protected]>
44
" Maintainer: Ben Blum <[email protected]>
55
" Maintainer: Chris Morgan <[email protected]>
6-
" Last Change: 2013 Oct 29
6+
" Last Change: 2013 Dec 04
77

88
if version < 600
99
syntax clear
@@ -28,6 +28,7 @@ syn keyword rustKeyword use nextgroup=rustModPath skipwhite
2828
" FIXME: Scoped impl's name is also fallen in this category
2929
syn keyword rustKeyword mod trait struct enum type nextgroup=rustIdentifier skipwhite
3030
syn keyword rustKeyword fn nextgroup=rustFuncName skipwhite
31+
syn keyword rustKeyword proc
3132
syn keyword rustStorage const mut ref static
3233

3334
syn match rustIdentifier contains=rustIdentifierPrime "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
@@ -89,6 +90,7 @@ syn keyword rustTrait Orderable Signed Unsigned Round
8990
syn keyword rustTrait Primitive Int Float ToStrRadix ToPrimitive FromPrimitive
9091
syn keyword rustTrait GenericPath Path PosixPath WindowsPath
9192
syn keyword rustTrait RawPtr
93+
syn keyword rustTrait Buffer Writer Reader Seek
9294
syn keyword rustTrait SendStr SendStrOwned SendStrStatic IntoSendStr
9395
syn keyword rustTrait Str StrVector StrSlice OwnedStr
9496
syn keyword rustTrait IterBytes
@@ -143,38 +145,29 @@ syn match rustOperator display "&&\|||"
143145
syn match rustMacro '\w\(\w\)*!' contains=rustAssert,rustFail
144146
syn match rustMacro '#\w\(\w\)*' contains=rustAssert,rustFail
145147

146-
syn match rustFormat display "%\(\d\+\$\)\=[-+' #0*]*\(\d*\|\*\|\*\d\+\$\)\(\.\(\d*\|\*\|\*\d\+\$\)\)\=\([hlLjzt]\|ll\|hh\)\=\([aAbdiuoxXDOUfFeEgGcCsSpn?]\|\[\^\=.[^]]*\]\)" contained
147-
syn match rustFormat display "%%" contained
148+
syn match rustSpecialError display contained /\\./
148149
syn match rustSpecial display contained /\\\([nrt0\\'"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)/
149150
syn match rustStringContinuation display contained /\\\n\s*/
150-
syn region rustString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=rustTodo,rustFormat,rustSpecial,rustStringContinuation
151+
syn region rustString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=rustSpecial,rustSpecialError,rustStringContinuation
151152
syn region rustString start='r\z(#*\)"' end='"\z1'
152153

153154
syn region rustAttribute start="#\[" end="\]" contains=rustString,rustDeriving
154155
syn region rustDeriving start="deriving(" end=")" contained contains=rustTrait
155156

156157
" Number literals
157-
syn match rustNumber display "\<[0-9][0-9_]*\>"
158-
syn match rustNumber display "\<[0-9][0-9_]*\(u\|u8\|u16\|u32\|u64\)\>"
159-
syn match rustNumber display "\<[0-9][0-9_]*\(i\|i8\|i16\|i32\|i64\)\>"
160-
161-
syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\>"
162-
syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\(u\|u8\|u16\|u32\|u64\)\>"
163-
syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\(i8\|i16\|i32\|i64\)\>"
164-
syn match rustOctNumber display "\<0o[0-7_]\+\>"
165-
syn match rustOctNumber display "\<0o[0-7_]\+\(u\|u8\|u16\|u32\|u64\)\>"
166-
syn match rustOctNumber display "\<0o[0-7_]\+\(i8\|i16\|i32\|i64\)\>"
167-
syn match rustBinNumber display "\<0b[01_]\+\>"
168-
syn match rustBinNumber display "\<0b[01_]\+\(u\|u8\|u16\|u32\|u64\)\>"
169-
syn match rustBinNumber display "\<0b[01_]\+\(i8\|i16\|i32\|i64\)\>"
170-
171-
syn match rustFloat display "\<[0-9][0-9_]*\(f\|f32\|f64\)\>"
172-
syn match rustFloat display "\<[0-9][0-9_]*\([eE][+-]\=[0-9_]\+\)\>"
173-
syn match rustFloat display "\<[0-9][0-9_]*\([eE][+-]\=[0-9_]\+\)\(f\|f32\|f64\)\>"
174-
syn match rustFloat display "\<[0-9][0-9_]*\.[0-9_]\+\>"
175-
syn match rustFloat display "\<[0-9][0-9_]*\.[0-9_]\+\(f\|f32\|f64\)\>"
176-
syn match rustFloat display "\<[0-9][0-9_]*\.[0-9_]\+\%([eE][+-]\=[0-9_]\+\)\>"
177-
syn match rustFloat display "\<[0-9][0-9_]*\.[0-9_]\+\%([eE][+-]\=[0-9_]\+\)\(f\|f32\|f64\)\>"
158+
syn match rustDecNumber display "\<[0-9][0-9_]*\%([iu]\%(8\|16\|32\|64\)\=\)\="
159+
syn match rustHexNumber display "\<0x[a-fA-F0-9_]\+\%([iu]\%(8\|16\|32\|64\)\=\)\="
160+
syn match rustOctNumber display "\<0o[0-7_]\+\%([iu]\%(8\|16\|32\|64\)\=\)\="
161+
syn match rustBinNumber display "\<0b[01_]\+\%([iu]\%(8\|16\|32\|64\)\=\)\="
162+
163+
" To mark it as a float, it must have at least one of the three things integral values don't have:
164+
" a decimal point and more numbers; an exponent; and a type suffix.
165+
syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\%([eE][+-]\=[0-9_]\+\)\=\(f32\|f64\)\="
166+
syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\=\%([eE][+-]\=[0-9_]\+\)\(f32\|f64\)\="
167+
syn match rustFloat display "\<[0-9][0-9_]*\%(\.[0-9][0-9_]*\)\=\%([eE][+-]\=[0-9_]\+\)\=\(f32\|f64\)"
168+
" Special case for numbers of the form "1." which are float literals, unless followed by
169+
" an identifier, which makes them integer literals with a method call or field access.
170+
syn match rustFloat display "\<[0-9][0-9_]*\.\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\@!"
178171

179172
" For the benefit of delimitMate
180173
syn region rustLifetimeCandidate display start=/&'\%(\([^'\\]\|\\\(['nrt0\\\"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'\)\@!/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime
@@ -183,12 +176,24 @@ syn region rustGenericLifetimeCandidate display start=/\%(<\|,\s*\)\@<='/ end=/[
183176

184177
"rustLifetime must appear before rustCharacter, or chars will get the lifetime highlighting
185178
syn match rustLifetime display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*"
186-
syn match rustCharacter /'\([^'\\]\|\\\([nrt0\\'"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'/ contains=rustSpecial
187-
188-
syn region rustCommentML start="/\*" end="\*/" contains=rustTodo
189-
syn region rustComment start="//" end="$" contains=rustTodo keepend
190-
syn region rustCommentMLDoc start="/\*\%(!\|\*[*/]\@!\)" end="\*/" contains=rustTodo
191-
syn region rustCommentDoc start="//\%(//\@!\|!\)" end="$" contains=rustTodo keepend
179+
syn match rustCharacter /'\([^'\\]\|\\\(.\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'/ contains=rustSpecial,rustSpecialError
180+
181+
syn cluster rustComment contains=rustCommentLine,rustCommentLineDoc,rustCommentBlock,rustCommentBlockDoc
182+
syn region rustCommentLine start="//" end="$" contains=rustTodo
183+
syn region rustCommentLineDoc start="//\%(//\@!\|!\)" end="$" contains=rustTodo
184+
syn region rustCommentBlock matchgroup=rustCommentBlock start="/\*\%(!\|\*[*/]\@!\)\@!" end="\*/" contains=rustTodo,@rustComment keepend extend
185+
syn region rustCommentBlockDoc matchgroup=rustCommentBlockDoc start="/\*\%(!\|\*[*/]\@!\)" end="\*/" contains=rustTodo,@rustComment keepend extend
186+
" FIXME: this is a really ugly and not fully correct implementation. Most
187+
" importantly, a case like ``/* */*`` should have the final ``*`` not being in
188+
" a comment, but in practice at present it leaves comments open two levels
189+
" deep. But as long as you stay away from that particular case, I *believe*
190+
" the highlighting is correct. Due to the way Vim's syntax engine works
191+
" (greedy for start matches, unlike Rust's tokeniser which is searching for
192+
" the earliest-starting match, start or end), I believe this cannot be solved.
193+
" Oh you who would fix it, don't bother with things like duplicating the Block
194+
" rules and putting ``\*\@<!`` at the start of them; it makes it worse, as
195+
" then you must deal with cases like ``/*/**/*/``. And don't try making it
196+
" worse with ``\%(/\@<!\*\)\@<!``, either...
192197

193198
syn keyword rustTodo contained TODO FIXME XXX NB NOTE
194199

@@ -200,15 +205,16 @@ syn region rustFoldBraces start="{" end="}" transparent fold
200205
" It's not enabled by default as it would drive some people mad.
201206

202207
" Default highlighting {{{1
208+
hi def link rustDecNumber rustNumber
203209
hi def link rustHexNumber rustNumber
204210
hi def link rustOctNumber rustNumber
205211
hi def link rustBinNumber rustNumber
206212
hi def link rustIdentifierPrime rustIdentifier
207213
hi def link rustTrait rustType
208214

209215
hi def link rustSigil StorageClass
210-
hi def link rustFormat Special
211216
hi def link rustSpecial Special
217+
hi def link rustSpecialError Error
212218
hi def link rustStringContinuation Special
213219
hi def link rustString String
214220
hi def link rustCharacter Character
@@ -229,10 +235,10 @@ hi def link rustModPathSep Delimiter
229235
hi def link rustFunction Function
230236
hi def link rustFuncName Function
231237
hi def link rustFuncCall Function
232-
hi def link rustCommentMLDoc rustCommentDoc
233-
hi def link rustCommentDoc SpecialComment
234-
hi def link rustCommentML rustComment
235-
hi def link rustComment Comment
238+
hi def link rustCommentLine Comment
239+
hi def link rustCommentLineDoc SpecialComment
240+
hi def link rustCommentBlock rustCommentLine
241+
hi def link rustCommentBlockDoc rustCommentLineDoc
236242
hi def link rustAssert PreCondit
237243
hi def link rustFail PreCondit
238244
hi def link rustMacro Macro

0 commit comments

Comments
 (0)