简体   繁体   English

如何使用VIM在CoffeeScript中嵌入HTML字符串语法?

[英]How to embed HTML string syntax in CoffeeScript using VIM?

I have looked at how to embed HTML syntax in JavaScript string from HTML syntax highlighting in javascript strings in vim . 我已经研究了如何从vim的javascript字符串中突出显示 HTML语法的方式将HTML语法嵌入到JavaScript字符串中

However, when I use CoffeeScript I cannot get the same thing working by editing coffee.vim syntax file in a similar way. 但是,当我使用CoffeeScript时,无法通过以类似方式编辑coffee.vim语法文件来获得相同的效果。 I got recursive errors which said including html.vim make it too nested. 我遇到了递归错误,其中包括html.vim使其嵌套过多。

I have some HTML template in CoffeeScript like the following:: 我在CoffeeScript中有一些HTML模板,如下所示:

angular.module('m', [])
  .directive(
    'myDirective'
    [
      ->
        template: """
        <div>
          <div>This is <b>bold</b> text</div>
          <div><i>This should be italic.</i></div>
        </div>
        """
    ]
  )

How do I get the template HTML syntax in CoffeeScript string properly highlighted in VIM? 如何在VIM中正确突出显示CoffeeScript字符串中的模板HTML语法?

I would proceed as follows: 我将进行如下操作:

Find out the syntax groups that should be highlighted as pure html would be. 找出应突出显示为纯html的语法组。 Add html syntax highlighting to these groups. 向这些组添加html语法突出显示。

To find the valid syntax group under the cursor you can follow the instructions here . 要在光标下找到有效的语法组,可以按照此处的说明进行操作。

In your example the syntax group of interest is coffeeHereDoc . 在您的示例中,感兴趣的语法组是coffeeHereDoc

To add html highlighting to this group execute the following commands 要将html突出显示添加到该组,请执行以下命令

unlet b:current_syntax
syntax include @HTML syntax/html.vim
syn region HtmlEmbeddedInCoffeeScript start="" end=""
\    contains=@HTML containedin=coffeeHereDoc

Since vim complains about recursion if you add these lines to coffee.vim i would go with an autocommand: 由于vim抱怨递归,如果将这些行添加到coffee.vim我将使用自动命令:

function! Coffee_syntax()
    if !empty(b:current_syntax)
        unlet b:current_syntax
    endif
    syn include @HTML syntax/html.vim
    syn region HtmlEmbeddedInCoffeeScript start="" end="" contains=@HTML
\       containedin=coffeeHereDoc
endfunction

autocmd BufEnter *.coffee call Coffee_syntax()

I was also running into various issues while trying to get this to work. 在尝试使其正常工作时,我还遇到了各种问题。 After some experimentation, here's what I came up with. 经过一些试验,这就是我的想法。 Just create .vim/after/syntax/coffee.vim with the following contents: 只需使用以下内容创建.vim/after/syntax/coffee.vim

unlet b:current_syntax
syntax include @HTML $VIMRUNTIME/syntax/html.vim
syntax region coffeeHtmlString matchgroup=coffeeHeredoc
\      start=+'''\\(\\_\\s*<\\w\\)\\@=+ end=+\\(\\w>\\_\\s*\\)\\@<='''+ 
\      contains=@HTML
syn sync minlines=300

The unlet b:current_syntax line disables the current syntax matching and lets the HTML syntax definition take over for matching regions. 取消unlet b:current_syntax行将禁用当前语法匹配,并让HTML语法定义代替匹配区域。

Using an absolute path for the html.vim inclusion avoids the recursion problem (described more below). 对html.vim包含使用绝对路径可以避免递归问题(下面将详细介绍)。

The region definition matches heredoc strings that look like they contain HTML. 区域定义与看起来包含HTML的heredoc字符串匹配。 Specifically, the start pattern looks for three single quotes followed by something that looks like the beginning of an HTML tag (there can be whitespace between the two), and the end pattern looks for the end of an HTML tag followed by three single quotes. 具体来说,开始模式查找三个单引号,后跟一个看起来像HTML标记的开头(两个之间可以有空格),结束模式查找一个HTML标记的结尾,后跟三个单引号。 Heredoc strings that don't look like they contain HTML are still matched using the coffeeHeredoc pattern. 看起来不包含HTML的Heredoc字符串仍使用coffeeHeredoc模式进行匹配。 This works because this syntax file is being loaded after the syntax definitions from the coffeescript plugin, so we get a chance to make the more specific match (a heredoc containing HTML) before the more general match (the coffeeHeredoc region) happens. 之所以可行,是因为该语法文件是在coffeescript插件的语法定义之后加载的,因此我们有机会在更常规的匹配( coffeeHeredoc区域)发生之前进行更具体的匹配(包含HTML的coffeeHeredoc )。

The syn sync minlines=300 widens the matching region. syn sync minlines=300扩大了匹配区域。 My embedded HTML strings sometimes stretched over 50 lines, and Vim's syntax highlighter would get confused about how the string should be highlighted. 我的嵌入式HTML字符串有时会超过50行,而Vim的语法突出显示工具会混淆应该如何突出显示该字符串。 For complete surety you could use syn sync fromstart , but for large files this could theoretically be slow (I didn't try it). 为了完全保证,您可以使用syn sync fromstart ,但是对于大文件,从理论上讲这可能很慢(我没有尝试过)。

The recursion problem originally experienced by @heartbreaker was caused by the html.vim script that comes with the vim-coffeescript plugin (I'm assuming that was being used). @heartbreaker最初遇到的递归问题是由vim-coffeescript插件随附的html.vim脚本引起的(我假设正在使用中)。 That plugin's html.vim file includes the its coffee.vim syntax file to add coffeescript highlighting to HTML files. 该插件的html.vim文件包括它的coffee.vim语法文件,用于在HTML文件中添加咖啡脚本突出显示。 Using a relative syntax include, a la 使用相对语法包括

syntax include @HTML syntax/html.vim

you get all the syntax/html.vim files in VIM's runtime path, including the one from the coffeescript plugin (which includes coffee.vim, hence the recursion). 您将在VIM的运行时路径中获得所有的语法/html.vim文件,包括来自coffeescript插件的文件(其中包括coffee.vim,因此是递归的)。 Using an absolute path will restrict you to only getting the particular syntax file you specify, but this seems like a reasonable tradeoff since the HTML one would embed in a coffeescript string is likely fairly simple. 使用绝对路径将限制您仅获取您指定的特定语法文件,但这似乎是一个合理的权衡,因为将HTML嵌入咖啡脚本字符串中可能很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM