vim中JavaScript字符串中的HTML语法高亮显示 [英] HTML syntax highlighting in javascript strings in vim

查看:145
本文介绍了vim中JavaScript字符串中的HTML语法高亮显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可行/明智,但我很想知道是否可以让我的JavaScript文件中的字符串具有HTML突出显示。我发现php中的字符串可能有SQL语法突出显示,所以我相信这是可能的。



但是,我不知道vim脚本,所以对此有帮助赞赏。



我使用更好的Javascript语法



PS:如果在编辑时可以选择打开和关闭它, js文件,那就太好了

谢谢 解决方案

如果您不介意某些语法文件被黑客入侵,则可能是有可能的。首先,您需要在Javascript语法文件中包含HTML语法文件 - 有关该信息,请参阅:help syn-include ;其次,您需要声明HTML语法可以在某些元素(即字符串)中找到。第三,如果您想要启用和禁用它,您可以使这些命令依赖于全局变量,并编写一些设置或取消设置变量的映射,然后重新加载语法文件。



有关包含如何工作的示例,请查看 syntax / html.vim (其中包括Javascript和CSS语法文件), syntax / perl.vim (其中包括POD语法文件)或 php.vim (其中包括字符串中的SQL语法高亮显示,在全球范围内可变)

编辑:在我的副本中做了一些实际操作。



语法/ javascript.vim 的头部, syn case ignore ,添加

  syn include @javaScriptHTML语法/ html.vim 
unlet b:current_syntax
syn spell default HTML使全局拼写检查成为可能,将它关闭

然后添加 @javaScriptHTML 到t他 contains = 列出了 javaScriptStringD javaScriptStringS p>

最后,您必须编辑 syntax / html.vim 以防止它包含语法/javascript.vim 如果从加载 javascript:找到读取的行

  if main_syntax!='java'||存在(java_javascript)

并将其更改为

  if main_syntax!='javascript'&& (main_syntax!='java'|| exists(java_javascript)


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 could have SQL syntax highlighting, so I believe it is possible.

But, I don't know vim-scripting, so any help on this appreciated.

I am using the Better Javascript syntax.

PS: If there could be an option to turn it on and off while editing a js file, that would be wonderful

Thanks

解决方案

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")

这篇关于vim中JavaScript字符串中的HTML语法高亮显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆