Created
July 20, 2019 15:55
-
-
Save olmokramer/17a77fd0ab49c4e34714b2ea1489d221 to your computer and use it in GitHub Desktop.
Update 'commentstring' in HTML files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" ~/.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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"~/.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