Extending Javascript syntax highlighting in vim

六月ゝ 毕业季﹏ 提交于 2019-12-12 00:33:06

问题


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.


回答1:


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:

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


来源:https://stackoverflow.com/questions/16224423/extending-javascript-syntax-highlighting-in-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!