Skip to content

Commit 5f57105

Browse files
committed
Dynamically set comments setting
I had a problem where Vim doesn't automatically insert the comment character to the start of the line when I press <CR> in a comment in Vue files even if I have the `r` option in my `formatoptions`. The problem turned out to be because Vue files have the `comments` setting set to `s:<!--,m: ,e:-->` which is valid for HTML, but not for the sections that include other languages. The `comments` setting should be modified automatically in the exact same way that `commentstring` is currently. So, I thought that it might make sense to add this functionality to this plugin.
1 parent ffcbd3b commit 5f57105

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

autoload/context/commentstring.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ let g:context#commentstring#table.vue = {
4747
\ 'cssStyle' : '/*%s*/',
4848
\}
4949

50+
let g:context#commentstring#comments_table = {}
51+
let g:context#commentstring#comments_table.vue = {
52+
\ 'htmlTag': 's:<!--,m: ,e:-->',
53+
\ 'vue_typescript': 's1:/*,mb:*,ex:*/,://',
54+
\ 'cssStyle': 's1:/*,mb:*,ex:*/,://',
55+
\ }

doc/context-commentstring.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ Pope's |commentary.txt| plugin, a utility to comment and uncomment lines of
5555
code. Find the plugin at:
5656
https://github.com/tpope/vim-commentary
5757

58+
Additionally, this plugin sets the value of the 'comments' setting
59+
automatically. The 'comments' setting describes how inserting new lines should
60+
be handled when inside a comment region. This automatic formatting can be
61+
configured via the 'formatoptions' setting.
62+
5863
------------------------------------------------------------------------------
5964
INSTALLATION *context-commentstring-installation*
6065

@@ -112,6 +117,10 @@ syntax groups. When you are done with your testing, remove the echo doing: >
112117
augroup END
113118
<
114119

120+
The 'comments' setting can be configured the exact same way as 'commentstring'
121+
but the variable to change is >
122+
g:context#commentstring#comments_table
123+
115124
116125
==============================================================================
117126
BUGS *context-commentstring-bugs*

plugin/context-commentstring.vim

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ function! s:Setup()
2222
let b:original_commentstring=&l:commentstring
2323
autocmd CursorMoved <buffer> call <SID>UpdateCommentString()
2424
endif
25+
if !empty(&filetype) && has_key(g:context#commentstring#comments_table, &filetype)
26+
let b:original_comments=&l:comments
27+
autocmd CursorMoved <buffer> call <SID>UpdateComments()
28+
endif
2529
augroup END
2630
endfunction
2731

@@ -40,4 +44,19 @@ function! s:UpdateCommentString()
4044
endfunction
4145

4246

47+
function! s:UpdateComments()
48+
let stack = synstack(line('.'), col('.'))
49+
call reverse(stack)
50+
if !empty(stack)
51+
for name in map(stack, 'synIDattr(v:val, "name")')
52+
if has_key(g:context#commentstring#comments_table[&filetype], name)
53+
let &l:comments = g:context#commentstring#comments_table[&filetype][name]
54+
return
55+
endif
56+
endfor
57+
endif
58+
let &l:commentstring = b:original_comments
59+
endfunction
60+
61+
4362
let g:loaded_context_commentstring = 1

0 commit comments

Comments
 (0)