Scala 3 Syntax Highlighting in vim

All through the Scala 2 releases, I’ve been happily using vim as my source code editor with Derek Wyatt’s vim-scala plugin. With the release of Scala 3, of course I’m looking for some highlighting that’s compatible with the new syntax.

I was delighted, therefore, to discover the vim logo on the scala-lang.org home page. But when I clicked it, it immediately sent me on a yak-shave of epic proportions:

  • Install node and yarn to support coc.nvim…
  • Install coc.nvim to install coc-metals…
  • Make about 120 additions to your .vimrc file…

And all of this with the intent of turning vim into a Scala IDE.

But the reason I use vim is because it is not an IDE. It’s a simple text editor. It simplifies programming to its bare essentials: Source text, plus a compiler. It does not do any thinking for me, nor hide anything from me.

I can handle my own formatting, import-sorting, et cetera.

Is there any way to get Scala 3 syntax support in vi / vim without turning vim into an IDE?

Is there some way I can have the banana, without the gorilla who’s holding the banana, and the whole blessed jungle?

Many thanks for your help!

2 Likes

Don’t confuse Scala LSP support via Metals with syntax highlighting.

If you want something that doesn’t have a Node dependency and is quite a bit more minimal, check out nvim-metals. The aim is to still bring Metals support to NeoVim, but in a more integrated way seeing that NeoVim 0.5 will ship with LSP baked in. However, if I understand you correctly having Scala language support in Vim isn’t actually what you want. While vim-scala was fantastic, I don’t see a lot of reasons to use it anymore. The syntax highlighting it provides is actually shipped by default in modern versions of Vim. Plus, the import sorting etc is done in a way that’s not semantic and can’t be reproduced by your colleagues or checked in CI. So using something like Scalafix, which is integrated in Metals is a much better approach imo.

Again, don’t confuse LSP support with syntax highlighting. While LSP can provide semantic tokens for highlighting, Metals doesn’t provide this at the moment. So turning vim into an IDE does literally nothing for syntax highlighting.

As for actual syntax support, as far as I know, there is no effort currently to make that happen. However, if someone was going to put effort into it, I’d highly highly recommend to not do it the regex based approach and to instead complete the tree-sitter-scala grammar. This would then be able to be used in multiple ecosystem and in NeoVim, which will ship tree-sitter support in 0.5, which will provide a much faster, more accurate, and featureful syntax highlighting than the old regex based one.

3 Likes