Skip to content

Instantly share code, notes, and snippets.

@olmokramer
Created July 20, 2019 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olmokramer/17a77fd0ab49c4e34714b2ea1489d221 to your computer and use it in GitHub Desktop.
Save olmokramer/17a77fd0ab49c4e34714b2ea1489d221 to your computer and use it in GitHub Desktop.
Update 'commentstring' in HTML files
" ~/.vim/autoload/commentstring.vim
function! s:matchcontext(stack, context) abort
for group in split(a:context, ',')
if group[0] == '!'
if index(a:stack, group[1:]) != -1
return 0
endif
else
if index(a:stack, group) == -1
return 0
endif
endif
endfor
return 1
endfunction
function! commentstring#update(contexts) abort
let stack = synstack(line('.'), col('.'))
let stack = map(stack, 'synIDattr(v:val, "name")')
for [context, commentstring] in items(a:contexts)
if s:matchcontext(stack, context)
execute 'setlocal commentstring=' . commentstring
return
endif
endfor
execute 'setlocal commentstring=' . a:contexts.default
endfunction
"~/.vim/after/ftplugin/html.vim
let b:commentstrings = {
\ 'javaScript,!htmlScriptTag': '//\ %s',
\ 'cssStyle,!htmlTag': '/*\ %s\ */',
\ 'default': '<!--\ %s\ -->',
\ }
augroup html_auto_commentstring
autocmd!
autocmd CursorMoved <buffer> call commentstring#update(b:commentstrings)
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment