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

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

GoImports kills html syntax highlighting

问题

在以下的Go代码中:

var rootTemplate = template.Must(template.New("root").Parse(`
<!DOCTYPE html>
<html>
<head>
 /SNIP/
</html>
`))

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

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

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

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

英文:

In the following go code:

var rootTemplate = template.Must(template.New(&quot;root&quot;).Parse(`
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
 /SNIP/
&lt;/html&gt;
`))

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

function! GoHtml()
    if !empty(b:current_syntax)
        unlet b:current_syntax
    endif
    syn include @html syntax/html.vim
    syntax region htmlCode start=+&lt;!DOCTYPE+ keepend end=+&lt;/html&gt;+ contains=@html containedIn=goRawString contained
endfunction
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

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

function! GoHtml()
    GoFmt
    if !empty(b:current_syntax)
            unlet b:current_syntax
    endif
    syn include @html syntax/html.vim
    syntax region htmlCode start=+&lt;!DOCTYPE+ keepend end=+&lt;/html&gt;+ contains=@html containedin=goRawString contained
endfunction

autocmd BufEnter *.go call GoHtml()
autocmd BufWrite *.go call GoHtml()

execute pathogen#infect()

" 不要自动保存,让我们自己处理
let g:go_fmt_autosave = 0

" 对于 Golang:自动运行 GoImports
let g:go_fmt_command = "GoImports"

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

英文:

Finally I nailed it to this:

function! GoHtml()
    GoFmt
    if !empty(b:current_syntax)
            unlet b:current_syntax
    endif
    syn include @html syntax/html.vim
    syntax region htmlCode start=+&lt;!DOCTYPE+ keepend end=+&lt;/html&gt;+ contains=@html containedin=goRawString contained
endfunction

autocmd BufEnter *.go call GoHtml()
autocmd BufWrite *.go call GoHtml()

execute pathogen#infect()

&quot; don&#39;t save automatically, let us handle this
let g:go_fmt_autosave = 0

&quot; for golang: automatically run GoImports
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:

确定