HTML syntax highlighting in javascript strings in vim

后端 未结 1 1150
灰色年华
灰色年华 2020-12-10 07:48

I don\'t know if this is possible/sensible, but I was curious to know if I can have my strings in javascript files have html highlighting. I discovered that strings in php c

相关标签:
1条回答
  • 2020-12-10 08:24

    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")
    
    0 讨论(0)
提交回复
热议问题