wyc
wyc

Reputation: 55293

Vim Custom Syntax Highlight: How to highlight a variable that stores a function (CoffeScript)?

I want to highlight the function names in CoffeScript code:

hightlight = ->

hightlight = (args) ->

Not including the following:

noHighlight ->

noHighlight =
  key:

How to accoplish that with a custom Vim syntax declaration?

Upvotes: 1

Views: 571

Answers (2)

user3803222
user3803222

Reputation:

Here's how I did it. Assuming you have vim-coffee-script, which you should, edit its syntax/coffee.vim (e.g. ~/.vim/bundle/vim-coffee-script/syntax/coffee.vim if you're using Pathogen).

Add this:

syn region coffeeFunction start=/\S\s*[a-zA-Z0-9_]\+ = \((.*) \)\?->/ end=// oneline transparent
syn match coffeeFunctionName /\S\s*[a-zA-Z0-9]\+ / contained containedin=coffeeFunction
hi def link coffeeFunctionName Identifier

This highlights function definitions in the same way that class methods are highlighted.

Definitely not perfect since this is my first time working with Vim's syntax highlighting, but it works for me. The big hack is using syn region instead of syn match, because the latter overrides the existing syntax and I don't know how else to prevent that.

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172658

You need to find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.

If the syntax script you're using provides a dedicated syntax group (something named coffeeFunction), changing the highlighting is as easy as putting

:highlight link coffeeFunction Function

into your ~/.vimrc. If there's no dedicated group, you'd have to extend the syntax script (or ask the script's author to do). The challenge there is that the new definition has to fit in with the others (especially with regards to contained=... relations. You may also search for different syntax scripts. AFAIK, no CoffeeScript syntax script presently ships with Vim, so there may be several competing versions out there.

Upvotes: 1

Related Questions