Search code examples
javascriptvimsyntax-highlighting

Extending Javascript syntax highlighting in vim


I would like to extend my JS syntax highlighting with highlight certain functions that will be commonly used in my program. I am using janus to keep all my plugins in order. Right now I have a file in there called vim-chino and then in there I have a syntax folder and a ftdetect folder. In both I have a chino.vim file. This is my syntax/chino.vim file:

if !exists("main_syntax")
  if version < 600
  syntax clear
elseif exists("b:current_syntax")
  finish
endif
let main_syntax = 'javascript'
endif


syn match chinoKeywords "ChinoView"
hi def link chinoKeywords Function


let b:current_syntax = "javascript"
if main_syntax == 'javascript'
  unlet main_syntax
endif

and in my ftdetect/chino.vim I have:

function! s:DetectJS()
    if getline(1) =~# '^#!.*/bin/env\s\+node\>'
        setfiletype javascript
    endif
endfunction
autocmd BufNewFile,BufRead * call s:DetectJS()

I would like it to highlight ChinoView in any javascript file. I feel like the syntax highlighting for JS is either overriding it or it this is not being read.

EDIT: If I had to guess where something was happening was that when it looks at b:current_syntax it already has a syntax so it quits.


Solution

  • Your ftplugin/chino.vim sets the syntax to javascript, but the extra highlighting you've defined is for the new chino syntax. That doesn't fit together.

    If you just want an extra keyword highlighted in all Javascript, you can just let the default Vim detection happen and add those lines to after/syntax/javascript.vim (you may have to create this directory and file):

    syn match chinoKeywords "ChinoView"
    hi def link chinoKeywords Function
    

    However, if you want to define a different chino filetype depending on the file's shebang line, you need to :setfiletype chino in your ftplugin/chino.vim, and then include the default javascript syntax in your syntax/chino.vim (after the initial checks, before you set b:current_syntax):

    runtime! syntax/javascript.vim syntax/javascript/*.vim