25

I am using Vim to program in Java 8, and am having a problem with syntax highlighting.

A new feature in Java 8 is lambda support, which resulted in the creation of the arrow operator: ->. Vim does not recognize this as a valid operator in a *.java file. As such, it highlights this operator in bright red to draw attention to the "syntax error." (Of course, this really is valid syntax.)

Is there a way to update Vim's "dictionary" of allowed operators and/or fix the syntax highlighting to avoid this issue?

The first two lines of :version state:

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jan 2 2014 19:39:32)

Included patches: 1-52

32

2016 update

The Vim syntax file was updated for this in v7.4.649 (March 2015); so if you have this patch you don't need to do this.

The easiest way to fix this is probably by downloading the newer syntax/java.vim file and putting it in ~/.vim/syntax/java.vim so it will override the default $VIMRUNTIME/syntax/java.vim. You will also need to symlink html.vim for this to work as java.vim depends on this:

ln -s /usr/share/vim/vim74/syntax/html.vim ~/.vim/syntax/

Original answer

My /usr/share/vim/vim74/syntax/java.vim has this:

syn match javaError "<<<\|\.\.\|=>\|||=\|&&=\|[^-]->\|\*\/"

See the [^-]-> part? That causes the error highlighting.

You can override this rule by adding a ~/.vim/after/syntax/java.vim file, with the same pattern minus the lambda syntax:

syn clear javaError
syn match javaError "<<<\|\.\.\|=>\|||=\|&&=\|\*\/"

And for bonus points, we can add this line to highlight this as a function:

syn match javaFuncDef "[^-]->"

Consider filing a bug report. :help bug-reports explains how. There seem to be more (new?) constructs that are highlighted as an error, such as the ... in public static void main(String... args).

  • 1
    The matching for the bonus points does not check if java_highlight_functions has been enabled. If you are neurotic about consistency, you should probably check it before the syn match call: if exists("java_highlight_functions") | syn match javaFuncDef "[^-]->" | endif – Zachary Murray Jul 14 '15 at 23:25
4

It's not necessary to edit the default syntax file. The vim-java plugin has support for this. Just install like any other plugin and the lambda operator will be highlighted correctly.

  • Are you sure? Because I don't see any code for it, and the only commit I see only removes the -> from javaError with the message "Don't match javaError if it's a lambda symbol" (but doesn't add highlighting as a function). – Martin Tournoij Mar 22 '16 at 18:56
  • @Caspetsmoker You're right. Just removes the error message. But I prefer like this. – adelarsq Mar 23 '16 at 14:10
  • 2
    I see that the runtime files have been updated in March 2015 btw, and that the repo you link to doesn't have many fixes that this updates introduces. FYI :-) – Martin Tournoij Mar 23 '16 at 14:18
  • @Carpetsmoker Nice. Did work as expected. Thanks =) – adelarsq Mar 23 '16 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.