HTML syntax highlighting in javascript strings in vim

感情迁移 提交于 2019-11-28 10:26:23

Yes, it's possible if you don't mind some syntax file hacking. First you need to include the HTML syntax file from within the Javascript syntax file -- see :help syn-include for info on that; second you need to declare that HTML syntax can be found inside of certain elements (i.e. strings). Third, if you want to have the option of enabling and disabling it, you can make those commands dependent on a global variable, and write some mappings that set or unset the variable and then reload the syntax file.

For examples on how inclusion works, take a look at syntax/html.vim (which includes the Javascript and CSS syntax files), syntax/perl.vim (which includes the POD syntax file), or php.vim (which includes SQL syntax highlighting in strings, conditional on a global ariable).

Edit: did some work on actually making this happen in my copy.

In the head of syntax/javascript.vim, just below syn case ignore, add

syn include @javaScriptHTML syntax/html.vim
unlet b:current_syntax
syn spell default " HTML enables spell-checking globally, turn it off

Then add @javaScriptHTML to the contained= lists for javaScriptStringD and javaScriptStringS.

Finally you have to edit syntax/html.vim to prevent it from trying to include syntax/javascript.vim if it was loaded from javascript: find the line that reads

if main_syntax != 'java' || exists("java_javascript")

and change it to

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