GoImports破坏了HTML语法高亮显示。

huangapple go评论112阅读模式
英文:

GoImports kills html syntax highlighting

问题

在以下的Go代码中:

  1. var rootTemplate = template.Must(template.New("root").Parse(`
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. /SNIP/
  6. </html>
  7. `))

我能够使用以下函数将html部分标记为html:

  1. function! GoHtml()
  2. if !empty(b:current_syntax)
  3. unlet b:current_syntax
  4. endif
  5. syn include @html syntax/html.vim
  6. syntax region htmlCode start=+<!DOCTYPE+ keepend end=+</html>+ contains=@html containedIn=goRawString contained
  7. endfunction
  8. autocmd BufEnter *.go call GoHtml()

然而,当我保存文档后,调用GoImports时,html语法高亮消失了:let g:go_fmt_command = "GoImports"

有没有办法保持嵌入的html高亮显示?

英文:

In the following go code:

  1. var rootTemplate = template.Must(template.New(&quot;root&quot;).Parse(`
  2. &lt;!DOCTYPE html&gt;
  3. &lt;html&gt;
  4. &lt;head&gt;
  5. /SNIP/
  6. &lt;/html&gt;
  7. `))

I am able to make the html part highlighted as html with this function:

  1. function! GoHtml()
  2. if !empty(b:current_syntax)
  3. unlet b:current_syntax
  4. endif
  5. syn include @html syntax/html.vim
  6. syntax region htmlCode start=+&lt;!DOCTYPE+ keepend end=+&lt;/html&gt;+ contains=@html containedIn=goRawString contained
  7. endfunction
  8. autocmd BufEnter *.go call GoHtml()

However after I saved the document, the html syntax highlighting disappears when calling GoImports: let g:go_fmt_command = &quot;GoImports&quot;

Is there a way to keep embeeded html highlighted?

答案1

得分: 0

最后我将它修改为以下内容:

  1. function! GoHtml()
  2. GoFmt
  3. if !empty(b:current_syntax)
  4. unlet b:current_syntax
  5. endif
  6. syn include @html syntax/html.vim
  7. syntax region htmlCode start=+&lt;!DOCTYPE+ keepend end=+&lt;/html&gt;+ contains=@html containedin=goRawString contained
  8. endfunction
  9. autocmd BufEnter *.go call GoHtml()
  10. autocmd BufWrite *.go call GoHtml()
  11. execute pathogen#infect()
  12. " 不要自动保存,让我们自己处理
  13. let g:go_fmt_autosave = 0
  14. " 对于 Golang:自动运行 GoImports
  15. let g:go_fmt_command = "GoImports"

这样我就可以按照我想要的方式对混合的 Go 和 HTML 代码进行着色。

英文:

Finally I nailed it to this:

  1. function! GoHtml()
  2. GoFmt
  3. if !empty(b:current_syntax)
  4. unlet b:current_syntax
  5. endif
  6. syn include @html syntax/html.vim
  7. syntax region htmlCode start=+&lt;!DOCTYPE+ keepend end=+&lt;/html&gt;+ contains=@html containedin=goRawString contained
  8. endfunction
  9. autocmd BufEnter *.go call GoHtml()
  10. autocmd BufWrite *.go call GoHtml()
  11. execute pathogen#infect()
  12. &quot; don&#39;t save automatically, let us handle this
  13. let g:go_fmt_autosave = 0
  14. &quot; for golang: automatically run GoImports
  15. let g:go_fmt_command = &quot;GoImports&quot;

That way I can have mixed go and html code colored the way I want.

huangapple
  • 本文由 发表于 2016年3月12日 07:01:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/35951440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定