Skip to content

Highlight variable definitions and constant names #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions syntax/rust.vim
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ syn match rustExternCrateString /".*"\_s*as/ contained nextgroup=rustIdentifie
syn keyword rustObsoleteExternMod mod contained nextgroup=rustIdentifier skipwhite skipempty

syn match rustIdentifier contains=rustIdentifierPrime "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained
syn match rustIdentifier "\v((let|static|const) (ref )?(mut )?)@<=[a-z_]+"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is insufficient: e.g. let Foo(bar) = baz;, these things are patterns, not identifiers. Also there are three other constructs that can introduce new bindings (match branches, function arguments, closure arguments). In general, I don’t like things that make it work sometimes but not always. I don’t think a good solution exists, when using a regular expression parser. You could make it highlight identifiers inside a pattern in each of these cases, but it’d slow things down a lot, and I don’t think that people actually care so very much about the precise highlighting, but they do about the performance of the highlighter. (Do take what I say with a grain of salt, however: I’m biased towards simple highlighting, and have Identifier cleared.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a good start for tuple destructuring:

syn match rustIdentifier "\v((let|static|const) (ref )?(mut )?)@<=([a-z_]+|\((\w+(, )?)+\))"

It currently matches the parentheses and commas as well though.

The following works for simple cases (i.e. no destructuring) with for loops:

syn match rustIdentifier "\v(for (&)?)@<=\w+"

syn match rustIdentifier "\v<[A-Z_][A-Z0-9_]+>" " Leave out the numbers on the first char to avoid matching numbers
syn match rustFuncName "\%([^[:cntrl:][:space:][:punct:][:digit:]]\|_\)\%([^[:cntrl:][:punct:][:space:]]\|_\)*" display contained

syn region rustMacroRepeat matchgroup=rustMacroRepeatDelimiters start="$(" end=")" contains=TOP nextgroup=rustMacroRepeatCount
Expand Down