vim-ocaml icon indicating copy to clipboard operation
vim-ocaml copied to clipboard

Highlight function names the same way as vs code?

Open rbjorklin opened this issue 5 years ago • 2 comments

I quite like how vs-code highlights the function name in a color different from its arguments. Would that be possible in vim as well?

VS Code vs-code-highlight

Vim vim-highlight

rbjorklin avatar Sep 14 '20 17:09 rbjorklin

I guess that would be possible, though you'd have to be careful with tuple bindings like let f1, f2 = ... or other irrefutable matches. I assume you'd be fine with coloring ordinary bindings like let x = 3, too. I'm not sure how much it would add to readability, but feel free to try implementing this if you like it. Just be warned: Vim highlighting rules are notoriously brittle and finicky so test well.

mmottl avatar Sep 28 '20 21:09 mmottl

I did that at home! This is how course hacky and fairly limited, hence I did not propose it for merging. Put this for example in your ~/.vim/after/syntax/ocaml.vim:

" Highlight let‐binders, ie. identifiers which are given an immediate value.
" Limitations:
"   — does not recognize let-patterns like “let (a,b) = …” or “let (C x) = …”
" [ — does not recognize “let[@inline] f = …” nor “let (*lol*) f = …” ]
"   — does not support object values and methods
" Highlighting other binders (function arguments, “fun”, “function”, “try with”,
" match with”…) is much more difficult and would require writing a more accurate
" parser.
"syn match ocamlLetBinder /\(\<\(let\(\_s\+rec\)\?\|and\|for\)\>\_s\+\)\@<=\<\(\l\|_\)\(\w\|'\)*\>/
" This is a coarse approximation that recognize simple PPX annotations, as in
" “let[@inline] f = ...”.
syn match ocamlLetBinder /\(\<\(let\(\_s\+rec\)\?\|and\|for\)\>\_s*\(\[@\(@\|\w\|\.\|\_s\)*\]\_s*\)*\)\@<=\<\(\l\|_\)\(\w\|'\)*\>/

hi link ocamlLetBinder Identifier

Maelan avatar Oct 27 '21 19:10 Maelan