英文:
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("root").Parse(`
<!DOCTYPE html>
<html>
<head>
/SNIP/
</html>
`))
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=+<!DOCTYPE+ keepend end=+</html>+ 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 = "GoImports"
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=+<!DOCTYPE+ keepend end=+</html>+ 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=+<!DOCTYPE+ keepend end=+</html>+ contains=@html containedin=goRawString contained
endfunction
autocmd BufEnter *.go call GoHtml()
autocmd BufWrite *.go call GoHtml()
execute pathogen#infect()
" don't save automatically, let us handle this
let g:go_fmt_autosave = 0
" for golang: automatically run GoImports
let g:go_fmt_command = "GoImports"
That way I can have mixed go and html code colored the way I want.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论