How can I add a "::" operator to my VIM syntax highlighting file? - vim

I am using a language embedded inside of Scala that has a :: operator. I would like to modify my scala.vim syntax file to recognize this new operator.
This is what I've tried (the first line works fine, but I don't know how to add the :: operator to it):
syn match COperator "[&|~><!)(*#%#+/=?:;}{,.\^\-\[\]]"
syn match COperator "\v::"
syn match COperator "\v\:\:"
hi link COperator Special
Any advice?

An existing scalaOperator ":\{2,\}" was causing all sequences of : to match (if there are two or more series of :), overriding my own syn match COperator statements.
The solution to get :: to match as a COperator was to simply rename the scalaOperator that matches series of : to a COperator:
syn match COperator ":\{2,\}"
Summary: check for existing match rules that override your own match rules.

Related

Why taskinfo syntax file does not work as expect?

I have following syntax file:
syn match TaskName /^\[.*\]/
syn match TaskType /^\[.*\]\s*\zs[a-z]*/
syn match TaskDescription /^\[.*\]\s*[a-z]*\s\+\zs.*/
hi def link TaskName Title
hi def link TaskType Todo
hi def link TaskDescription Comment
and the context is:
Task Type Command
[file-run] local no description
why only [file-run] is matched?
If I type /^\[.*\]\s*\zs[a-z]* in normal mode, local will be matched.
The reason it is not matching is that :syn match does not try and
evaluate text it has already matched. So, it cannot match against the text already matched by :syn match TaskName.
Additionally, there's a lot that could be improved upon your patterns, notably:
You have a lot of pattern atoms that will match the empty string.
This makes pattern matching slow, as patterns like [a-z]* will match
everywhere (this pattern in particular is even pointed out as an example of
a pattern to avoid in the help documentation for :syn-pattern). In most
cases it is better to match 1 or more matches with \+ than 0 or
more matches with *.
You can make all of your patterns more brief and more clear using other
arguments of :syn match.
I would suggest leveraging the power of the nextgroup argument in combination
with the skipwhite argument of :syn match:
nextgroup allows you to tell
Vim to try and match the groups specified after this match.
The skipwhite argument allows you to skip over tabs and spaces when
trying to match the next group with nextgroup.
Keeping these in mind, you could rewrite your patterns to look like:
syn match TaskName /^\[.\+\]/ nextgroup=TaskType skipwhite
syn match TaskType /[a-z]\+/ nextgroup=TaskDescription skipwhite
syn match TaskDescription /\w\+\(\s\+\w\+\)*/
In order to do this, I've also edited your TaskDescription match to be “a word,
followed by 0 or more words separated by whitespace".
You can see that utilizing nextgroup and skipwhite makes each syntax match
more brief, in addition to making the contents of each group more clear.
Relevant :help queries:
:h :syn-match
:h :syn-pattern
:h :syn-nextgroup
:h :syn-skipwhite

Vim syntax error highlight

I'm vim newbie, and I been using someone's vim configuration(I've cloned his .vim directory to my home directory).
It seems that his configuration has a bug about syntax checking. As you can see:
The bracket should not be highlighted, cause there is not error there.
So my question is, which part of the vim configuration should I check for this issue.
Thanks
UPDATE: 0
I think I found the line that cause this bug.
I have a extra.vim file in ~/.vim/syntax/c/extra.vim the file is looks like:
"========================================================
" Highlight All Function
"========================================================
syn match cFunction "\<[a-zA-Z_][a-zA-Z_0-9]*\>[^()]*)("me=e-2
syn match cFunction "\<[a-zA-Z_][a-zA-Z_0-9]*\>\s*("me=e-1
"========================================================
" Highlight All Math Operator
"========================================================
" C math operators
syn match cMathOperator display "[-+\*/%=]"
"" C pointer operators
syn match cPointerOperator display "->\|\."
"" C logical operators - boolean results
syn match cLogicalOperator display "[!<>]=\="
syn match cLogicalOperator display "=="
"" C bit operators
syn match cBinaryOperator display "\(&\||\|\^\|<<\|>>\)=\="
syn match cBinaryOperator display "\~"
syn match cBinaryOperatorError display "\~="
"" More C logical operators - highlight in preference to binary
syn match cLogicalOperator display "&&\|||"
syn match cLogicalOperatorError display "\(&&\|||\)="
" Math Operator
hi def link cMathOperator cOperator
hi def link cPointerOperator cOperator
hi def link cLogicalOperator cOperator
hi def link cBinaryOperator cOperator
hi def link cBinaryOperatorError cOperator
hi def link cLogicalOperator cOperator
hi def link cLogicalOperatorError cOperator
hi def link cFunction Function
hi def link cOperator Operator
" hi Operator guifg=LightGoldenrod
When I comment out the line below:
syn match cMathOperator display "[-+\*/%=]"
The issue is gone.
So how can I fix this, and why ?
Vim probably has not checked the entire file. It is a kind of optimization which sometimes fails.
Usually scrolling the file backwards and forwards a couple of screens solves the problem.
You can also force a analysis on the entire file:
:syn sync fromstart
For more information, check Vim FAQ 24.8: Vim syntax highlighting is broken. When I am editing a file, some parts of the file is not syntax highlighted or syntax highlighted incorrectly.
OK, This Bug is corrected with:
-"syn match cMathOperator display "[-+\*/%=]"
+syn match cMathOperator display "[-+/*/%=]"

Vim Syntax-File, Comment after brace not styled

I have the following three lines to style comments in my syntax file. Comments start with # and are allowed to be inline or on a seperate line.
syn keyword myTodo contained TODO FIXME
syn match myComment "^#.*" contains=myTodo
syn match myComment "\s#.*"ms=s+1 contains=myTodo
It does work as long as there is no character (includes braces, etc) right before the #.
I tryed to create a rule like this:
syn match myComment ".*#.*"ms=s+1 contains=myTodo
but this would style the whole line as comment.
What do I have to do to make it style correctly, even if there is a character right before the #?
EDIT
syn match myComment "\s*#.*"ms=s+1 contains=myTodo
Hightlights the text after # correctly and the text before # is not styled as a comment but the # isn't styled as comment.
If I understood well, there is no need to describe the match before the sharp sign.
What happens if you simply try this:
syn keyword myTodo contained TODO FIXME
syn match myComment "#.*$" contains=myTodo
It's a simple case, which doesn't handle the case where a sharp sign is included into a string for example (if there is some strings in your syntax). To handle this additionnally, you can add:
syn match Constant /\v"([^\\]|\\.)*"/
syn match Normal /^.*$/ contains=Constant,myComment

vim regex & highlight syntax: find a match and ignore sub-match in it

I am trying to write a syntax highlighter in VIM. How do you highlight a match within another match?
To find each match, I created two syn match lines, which work where the matches are separate.
syn match celString "^xpath=.\{-};" -> matches "xpath=.........;"
syn match celComment "\${.\{-}}" -> matches "${LIB_METADATA};"
The first line is pink for the xpath string and blue for the ${..} string.
The second line is pink for the xpath string, but the ${..} contained inside that string is ignored.
I've tried to change the order of the syn match lines, but that doesn't have any effect.
I'd appreciate your ideas.
By default, Vim only applies the syntax groups to text that hasn't yet been assigned a syntax. To specify that one group can contain other groups, use the contains=... attribute:
:syn match celString "^xpath=.\{-};" contains=celComment
The order of definition shouldn't matter here. See :help :syn-contains for more information.

Sub-match syntax highlighting in Vim

First, I'll show the specific problem I'm having, but I think the problem can be generalized.
I'm working with a language that has explicit parenthesis syntax (like Lisp), but has keywords that are only reserved against the left paren. Example:
(key key)
the former is a reserved word, but the latter is a reference to the variable named "key"
Unfortunately, I find highlighting the left paren annoying, so I end up using
syn keyword classification key
instead of
syn keyword classification (key
but the former triggers on the variable uses as well.
I'd take a hack to get around my problem, but I'd be more interested in a general method to highlight just a subset of a given match.
Using syn keyword alone for this situation doesn't work right because you want your highlighting to be more aware of the surrounding syntax. A combination of syn region, syn match, and syn keyword works well.
hi link lispFuncs Function
hi link lispFunc Identifier
hi link sExpr Statement
syn keyword lispFuncs key foo bar contained containedin=lispFunc
syn match lispFunc "(\#<=\w\+" contained containedin=sExpr contains=lispFuncs
syn region sExpr matchgroup=Special start="(" end=")" contains=sExpr,lispFuncs
The above will only highlight key, foo, and bar using the Function highlight group, only if they're also matched by lispFunc.
If there are any words other than key, foo, and bar which come after a (, they will be highlighted using the Identifier highlight group. This allows you to distinguish between standard function names and user-created ones.
The ( and ) will be highlighted using the Special highlight group, and anything inside the () past the first word will be highlighted using the Statement highlight group.
There does appear to be some capability for layered highlighting, as seen here: Highlighting matches in Vim over an inverted pattern
which gives ex commands
:match myBaseHighlight /foo/
:2match myGroup /./
I haven't been able to get anything like that to work in my syntax files, though. I tried something like:
syn match Keyword "(key"
syn match Normal "("
The highlighting goes to Normal or Keyword over the whole bit depending on what gets picked up first (altered by arrangement in the file)
Vim soundly rejected using "2match" as a keyword after "syn".

Resources