Skip to content

Commit 44cb1c3

Browse files
committed
Highlight sigils and operators in Vim.
Sigil highlighting isn't perfect (especially how it handles ``&``) but after having used it for a week I feel it to be considerably nicer than nothing. As usual, if you don't like it, you can turn it off easily by overriding the default highlighting. Generics are not handled specially; this means that for something like ``S<T>``, the ``<`` and ``>`` are highlighted as operators. For myself, I like this, and there is no way to make it properly context aware without expanding the syntax matching enormously. Also, special characters are highlighted properly in strings/chars, e.g. ``"\x00"`` or ``'\Ufedcba98'`` appropriately.
1 parent efd6eaf commit 44cb1c3

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

src/etc/vim/syntax/rust.vim

Lines changed: 23 additions & 6 deletions
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 Jul 10
6+
" Last Change: 2013 Aug 1
77

88
if version < 600
99
syntax clear
@@ -17,7 +17,7 @@ syn keyword rustOperator as
1717
syn match rustAssert "\<assert\(\w\)*!" contained
1818
syn match rustFail "\<fail\(\w\)*!" contained
1919
syn keyword rustKeyword break copy do extern
20-
syn keyword rustKeyword for if impl let log
20+
syn keyword rustKeyword foreach in if impl let log
2121
syn keyword rustKeyword copy do extern
2222
syn keyword rustKeyword for impl let log
2323
syn keyword rustKeyword loop mod once priv pub
@@ -83,12 +83,26 @@ syn match rustModPathSep "::"
8383
syn match rustFuncCall "\w\(\w\)*("he=e-1,me=e-1
8484
syn match rustFuncCall "\w\(\w\)*::<"he=e-3,me=e-3 " foo::<T>();
8585

86+
" This is merely a convention; note also the use of [A-Z], restricting it to
87+
" latin identifiers rather than the full Unicode uppercase. I have not used
88+
" [:upper:] as it depends upon 'noignorecase'
89+
"syn match rustCapsIdent display "[A-Z]\w\(\w\)*"
90+
91+
syn match rustOperator display "\%(+\|-\|/\|*\|=\|\^\|&\||\|!\|>\|<\|%\)=\?"
92+
" This one isn't *quite* right, as we could have binary-& with a reference
93+
syn match rustSigil display /&\s\+[&~@*][^)= \t\r\n]/he=e-1,me=e-1
94+
syn match rustSigil display /[&~@*][^)= \t\r\n]/he=e-1,me=e-1
95+
" This isn't actually correct; a closure with no arguments can be `|| { }`.
96+
" Last, because the & in && isn't a sigil
97+
syn match rustOperator display "&&\|||"
98+
8699
syn match rustMacro '\w\(\w\)*!' contains=rustAssert,rustFail
87100
syn match rustMacro '#\w\(\w\)*' contains=rustAssert,rustFail
88101

89102
syn match rustFormat display "%\(\d\+\$\)\=[-+' #0*]*\(\d*\|\*\|\*\d\+\$\)\(\.\(\d*\|\*\|\*\d\+\$\)\)\=\([hlLjzt]\|ll\|hh\)\=\([aAbdiuoxXDOUfFeEgGcCsSpn?]\|\[\^\=.[^]]*\]\)" contained
90103
syn match rustFormat display "%%" contained
91-
syn region rustString start=+L\="+ skip=+\\\\\|\\"+ end=+"+ contains=rustTodo,rustFormat
104+
syn match rustSpecial display contained /\\\([nrt\\'"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)/
105+
syn region rustString start=+L\="+ skip=+\\\\\|\\"+ end=+"+ contains=rustTodo,rustFormat,rustSpecial
92106

93107
syn region rustAttribute start="#\[" end="\]" contains=rustString,rustDeriving
94108
syn region rustDeriving start="deriving(" end=")" contained contains=rustTrait
@@ -114,13 +128,13 @@ syn match rustFloat display "\<[0-9][0-9_]*\.[0-9_]\+\%([eE][+-]\=[0-9
114128
syn match rustFloat display "\<[0-9][0-9_]*\.[0-9_]\+\%([eE][+-]\=[0-9_]\+\)\(f\|f32\|f64\)\>"
115129

116130
" For the benefit of delimitMate
117-
syn region rustLifetimeCandidate display start=/&'\%(\([^'\\]\|\\\(['nrt\\\"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'\)\@!/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustLifetime
131+
syn region rustLifetimeCandidate display start=/&'\%(\([^'\\]\|\\\(['nrt\\\"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'\)\@!/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime
118132
syn region rustGenericRegion display start=/<\%('\|[^[cntrl:][:space:][:punct:]]\)\@=')\S\@=/ end=/>/ contains=rustGenericLifetimeCandidate
119-
syn region rustGenericLifetimeCandidate display start=/\%(<\|,\s*\)\@<='/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustLifetime
133+
syn region rustGenericLifetimeCandidate display start=/\%(<\|,\s*\)\@<='/ end=/[[:cntrl:][:space:][:punct:]]\@=\|$/ contains=rustSigil,rustLifetime
120134

121135
"rustLifetime must appear before rustCharacter, or chars will get the lifetime highlighting
122136
syn match rustLifetime display "\'\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*"
123-
syn match rustCharacter "'\([^'\\]\|\\\(['nrt\\\"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'"
137+
syn match rustCharacter /'\([^'\\]\|\\\([nrt\\'"]\|x\x\{2}\|u\x\{4}\|U\x\{8}\)\)'/ contains=rustSpecial
124138

125139
syn region rustCommentML start="/\*" end="\*/" contains=rustTodo
126140
syn region rustComment start="//" skip="\\$" end="$" contains=rustTodo keepend
@@ -140,7 +154,9 @@ hi def link rustBinNumber rustNumber
140154
hi def link rustIdentifierPrime rustIdentifier
141155
hi def link rustTrait rustType
142156

157+
hi def link rustSigil StorageClass
143158
hi def link rustFormat Special
159+
hi def link rustSpecial Special
144160
hi def link rustString String
145161
hi def link rustCharacter Character
146162
hi def link rustNumber Number
@@ -152,6 +168,7 @@ hi def link rustOperator Operator
152168
hi def link rustKeyword Keyword
153169
hi def link rustConditional Conditional
154170
hi def link rustIdentifier Identifier
171+
hi def link rustCapsIdent rustIdentifier
155172
hi def link rustModPath Include
156173
hi def link rustModPathSep Delimiter
157174
hi def link rustFuncName Function

0 commit comments

Comments
 (0)