Vim now highlights types, feedback welcome

A patch just made its way to the community-maintained Vim files for OCaml (not propagated to the official Vim distribution, yet), that tries to highlight types. IMHO the patch is large and hacky so you may want to try it cautiously, and feedback would be appreciated. :slight_smile:

The former behavior was to highlight identifiers that happened to be the name of a builtin type (such as int or list), regardless of where they appeared. Now, in principle, all type expressions can be highlighted, and be so only when in a type context. By default, only builtin types are highlighted, but you can unleash the full power of the new linter:

" put this in ~/.vim/after/syntax/ocaml.vim for instance:
hi link ocamlTypeConstr   Type
hi link ocamlTypeBuiltin  Type
hi link ocamlTypeVar      Type
hi link ocamlTypeAnyVar   Type

or fancier (if you like excess :rainbow:):

" 112 = light green (the color of the “Type“ hl group with my theme)
hi ocamlTypeConstr       ctermfg=112
hi ocamlTypeBuiltin      ctermfg=112 cterm=bold
hi ocamlTypeVar          ctermfg=112 cterm=italic
hi ocamlTypeAnyVar       ctermfg=112 cterm=bold

Even if you don’t care about highlighting types, allowing the linter to discriminate between types and expressions has some tangential benefits.

9 Likes

Thanks a lot !

One thing I particularly like is highlighting type variables differently:

hi link ocamlTypeVar Identifier
hi link ocamlTypeAnyVar Special

These tiny 'as and 'bs are sometimes hard to see in the middle of a long type expression.

1 Like

Using tree-sitter-based NeoVim parser would be probably more promising:

This is what I use myself since the NeoVim 0.7.0 release.

2 Likes

Certainly! Unfortunately, Tree-Sitter for NeoVim was merely a thing at the time this hack was written (3 years ago). Worse, it wasn’t available for old Vim, and (I haven’t followed the news but) I don’t think this has changed (I’d be curious if you have pointers; I found this discussion from last year, in which alternatives are considered but there no conclusion is drawn).

Sadly, old Vim is likely to stay around (and it is apparently taking great delight in diverging from NeoVim more and more… grmbl).

Yes, Vim has no plans to adopt Tree-Sitter, at least not in the near future. And yes, with Vim9 substituting old VimL in old Vim and Lua in NeoVim they will divert quite significantly. I am afraid the era of universal plugins that would work in both is gone.

The neovim community re-implemented a lot of the best plugin in Lua and added more modern features to the point where I do not see universal plugins as a desirable goal in my set-up.

I think it’s a shame Vim didn’t try to bridge the gap and bring the Neovim community into the fold but re-invented a new imcompatible language instead…

@Maelan a bit off topic, but how do you manage mli in your Vim set-up? I always have some trouble with (Neo)Vim trying to treat them as ml files and I somebody (I think you?) posted about it somewhere but I can’t find the details.

1 Like

That was not me, but there is an open issue on GitHub (by @undu). I don’t remember encountering problems with ML vs MLI (the linter encompasses both, and Merlin can tell the difference), but it seems it is causing trouble to people with NeoVim and Tree-Sitter.

1 Like

Just want to say thanks for sharing/mentioning this. I had somehow completely missed Treesitter and been unhappy with the syntax highlighting in vim/neovim, this is quite the improvement!