Skip to content

Commit c910d20

Browse files
committed
Integrate JoosepAlviste/nvim-ts-context-commentstring
- Add g:caw_integrated_plugin - Add caw#update_comments_from_commentstring()
1 parent 4263742 commit c910d20

6 files changed

Lines changed: 165 additions & 30 deletions

File tree

autoload/caw.vim

Lines changed: 100 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ scriptencoding utf-8
33

44
let s:installed_repeat_vim = (globpath(&runtimepath, 'autoload/repeat.vim') !=# '')
55
let s:installed_context_filetype = (globpath(&runtimepath, 'autoload/context_filetype.vim') !=# '')
6+
let s:installed_ts_context_commentstring = (globpath(&runtimepath, 'plugin/ts_context_commentstring.vim') !=# '')
67
let s:op_args = ''
78
let s:op_doing = 0
89

@@ -17,9 +18,17 @@ function! caw#keymapping_stub(mode, action, method) abort
1718
normal! ^
1819
endif
1920

21+
" When integration == 'context_filetype'
22+
" Load comment string from the context filetype
23+
" When integration == 'ts_context_commentstring'
24+
" Load comment string from ts_context_commentstring plugin
25+
" When integration == ''
26+
" No additional setup will be done
27+
let integration = s:get_integrated_plugin()
28+
2029
" Context filetype support.
2130
" https://github.com/Shougo/context_filetype.vim
22-
if s:installed_context_filetype
31+
if integration ==# 'context_filetype'
2332
let conft = context_filetype#get_filetype()
2433
else
2534
let conft = &l:filetype
@@ -47,7 +56,13 @@ function! caw#keymapping_stub(mode, action, method) abort
4756
endif
4857
call caw#set_context(context)
4958

50-
if conft !=# &l:filetype
59+
if integration ==# 'ts_context_commentstring'
60+
let old_commentstring = &l:commentstring
61+
lua require('ts_context_commentstring.internal').update_commentstring()
62+
if &l:commentstring !=# old_commentstring
63+
call caw#update_comments_from_commentstring(&l:commentstring)
64+
endif
65+
elseif conft !=# &l:filetype
5166
call caw#load_ftplugin(conft)
5267
endif
5368

@@ -104,6 +119,34 @@ function! caw#keymapping_stub(mode, action, method) abort
104119
endtry
105120
endfunction
106121

122+
" Returns "context_filetype" or "ts_context_commentstring" or ""
123+
function! s:get_integrated_plugin() abort
124+
let integration = caw#get_var('caw_integrated_plugin')
125+
if integration ==# 'context_filetype'
126+
if !s:installed_context_filetype
127+
echohl ErrorMsg
128+
echomsg 'Shougo/context_filetype.vim is not installed!'
129+
echohl None
130+
return ''
131+
endif
132+
return 'context_filetype'
133+
elseif integration ==# 'ts_context_commentstring'
134+
if !s:installed_ts_context_commentstring
135+
echohl ErrorMsg
136+
echomsg 'JoosepAlviste/nvim-ts-context-commentstring is not installed!'
137+
echohl None
138+
return ''
139+
endif
140+
return 'ts_context_commentstring'
141+
elseif s:installed_context_filetype
142+
return 'context_filetype'
143+
elseif s:installed_ts_context_commentstring
144+
return 'ts_context_commentstring'
145+
else
146+
return ''
147+
endif
148+
endfunction
149+
107150
function! caw#keymapping_stub_deprecated(mode, action, method, old_action) abort
108151
let oldmap = printf('<Plug>(caw:%s:%s)', a:old_action, a:method)
109152
let newmap = printf('<Plug>(caw:%s:%s)', a:action, a:method)
@@ -150,25 +193,21 @@ endfunction
150193

151194
" Utilities: Misc. functions. {{{
152195

153-
if s:installed_context_filetype
154-
function! caw#get_related_filetypes(ft) abort
155-
let filetypes = get(context_filetype#filetypes(), a:ft, [])
156-
let dup = {a:ft : 1}
157-
let related = []
158-
for ft in map(copy(filetypes), 'v:val.filetype')
159-
if !has_key(dup, ft)
160-
let related += [ft]
161-
let dup[ft] = 1
162-
endif
163-
endfor
164-
return related
165-
endfunction
166-
else
167-
" vint: next-line -ProhibitUnusedVariable
168-
function! caw#get_related_filetypes(ft) abort
196+
function! caw#get_related_filetypes(ft) abort
197+
if s:get_integrated_plugin() !=# 'context_filetype'
169198
return []
170-
endfunction
171-
endif
199+
endif
200+
let filetypes = get(context_filetype#filetypes(), a:ft, [])
201+
let dup = {a:ft : 1}
202+
let related = []
203+
for ft in map(copy(filetypes), 'v:val.filetype')
204+
if !has_key(dup, ft)
205+
let related += [ft]
206+
let dup[ft] = 1
207+
endif
208+
endfor
209+
return related
210+
endfunction
172211

173212
function! caw#assert(cond, msg) abort
174213
if !a:cond
@@ -339,6 +378,47 @@ function! caw#load_ftplugin(ft) abort
339378
execute 'runtime! after/ftplugin/' . a:ft . '/caw.vim'
340379
endfunction
341380

381+
function! caw#update_comments_from_commentstring(cms) abort
382+
if !exists('b:did_caw_ftplugin')
383+
" Raise error when caw ftplugin would override comment variables
384+
echohl ErrorMsg
385+
echomsg 'Call caw#update_comments_from_commentstring() after caw ftplugin is loaded'
386+
echohl None
387+
return
388+
endif
389+
390+
let undo = []
391+
392+
let parsed = caw#comments#parse_commentstring(a:cms)
393+
if has_key(parsed, 'oneline')
394+
let b:caw_oneline_comment = parsed.oneline
395+
let undo += ['b:caw_oneline_comment']
396+
else
397+
unlet! b:caw_oneline_comment
398+
endif
399+
if has_key(parsed, 'wrap_oneline')
400+
let b:caw_wrap_oneline_comment = parsed.wrap_oneline
401+
let undo += ['b:caw_wrap_oneline_comment']
402+
else
403+
unlet! b:caw_wrap_oneline_comment
404+
endif
405+
if has_key(parsed, 'wrap_multiline')
406+
let b:caw_wrap_multiline_comment = parsed.wrap_multiline
407+
let undo += ['b:caw_wrap_multiline_comment']
408+
else
409+
unlet! b:caw_wrap_multiline_comment
410+
endif
411+
412+
if !empty(undo)
413+
if exists('b:undo_ftplugin')
414+
let b:undo_ftplugin .= ' | '
415+
else
416+
let b:undo_ftplugin = ''
417+
endif
418+
let b:undo_ftplugin .= 'unlet! ' . join(undo)
419+
endif
420+
endfunction
421+
342422

343423
" '.../autoload/caw'
344424
" vint: next-line -ProhibitUnusedVariable

autoload/caw/comments.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
scriptencoding utf-8
2+
3+
function! caw#comments#parse_commentstring(cms) abort
4+
let parsed = {}
5+
6+
let oneline = caw#comments#oneline#new().parse_commentstring(a:cms)
7+
if !empty(oneline)
8+
let parsed.oneline = oneline
9+
endif
10+
11+
let wrap_oneline = caw#comments#wrap_oneline#new().parse_commentstring(a:cms)
12+
if !empty(wrap_oneline)
13+
let parsed.wrap_oneline = wrap_oneline
14+
endif
15+
16+
return parsed
17+
endfunction

autoload/caw/comments/oneline.vim

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,14 @@ function! s:oneline.get_comment_vars() abort
3232
endfunction
3333

3434
function! s:oneline.get_comment_detect() abort
35-
let m = matchlist(&l:commentstring, '^\(.\{-}\)[ \t]*%s[ \t]*\(.*\)$')
35+
let c = self.parse_commentstring(&l:commentstring)
36+
return !empty(c) ? [c] : []
37+
endfunction
38+
39+
function! s:oneline.parse_commentstring(cms) abort
40+
let m = matchlist(a:cms, '^\(.\{-}\)[ \t]*%s[ \t]*\(.*\)$')
3641
if !empty(m) && m[1] !=# '' && m[2] ==# ''
37-
return [m[1]]
42+
return m[1]
3843
endif
39-
return []
44+
return ''
4045
endfunction

autoload/caw/comments/wrap_oneline.vim

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ function! s:wrap_oneline.get_comment_vars() abort
3838
endfunction
3939

4040
function! s:wrap_oneline.get_comment_detect() abort
41-
let m = matchlist(&l:commentstring, '^\(.\{-}\)[ \t]*%s[ \t]*\(.*\)$')
41+
let c = self.parse_commentstring(&l:commentstring)
42+
return !empty(c) ? [c] : []
43+
endfunction
44+
45+
function! s:wrap_oneline.parse_commentstring(cms) abort
46+
let m = matchlist(a:cms, '^\(.\{-}\)[ \t]*%s[ \t]*\(.*\)$')
4247
if !empty(m) && m[1] !=# '' && m[2] !=# ''
43-
return [m[1:2]]
48+
return m[1:2]
4449
endif
4550
return []
4651
endfunction

doc/caw.txt

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Introduction |caw-introduction|
2525
Features |caw-features|
2626
Supported filetypes |caw-supported-filetypes|
2727
Interface |caw-interface|
28+
Functions |caw-functions|
2829
Keymappings |caw-keymappings|
2930
Default keymappings |caw-keymappings-default|
3031
Prefix keymapping |caw-keymappings-prefix|
@@ -165,6 +166,16 @@ See "after/ftplugin/*" in this repository for current supported filetypes.
165166
}}}
166167
==============================================================================
167168
INTERFACE *caw-interface* {{{
169+
170+
------------------------------------------------------------------------------
171+
FUNCTIONS *caw-functions* {{{
172+
173+
*caw#update_comments_from_commentstring()*
174+
caw#update_comments_from_commentstring({commentstring})
175+
176+
This function parses {commentstring} and sets / unsets
177+
|caw-variables-comments|.
178+
168179
------------------------------------------------------------------------------
169180
KEYMAPPINGS *caw-keymappings* {{{
170181

@@ -654,6 +665,19 @@ g:caw_operator_keymappings *g:caw_operator_keymappings*
654665
g:caw_find_another_action *g:caw_find_another_action*
655666
(Default: 1)
656667

668+
g:caw_integrated_plugin *g:caw_integrated_plugin*
669+
(Default: "auto")
670+
671+
This variable detemines that how caw detects comment string
672+
from integrated plugins.
673+
674+
"context_filetype"
675+
Use https://github.com/Shougo/context_filetype.vim
676+
"ts_context_commentstring"
677+
Use https://github.com/JoosepAlviste/nvim-ts-context-commentstring
678+
"auto"
679+
Use context_filetype or ts_context_commentstring if it's installed
680+
657681
}}}
658682

659683
}}}
@@ -673,7 +697,7 @@ A. You can change prefix keymapping
673697
Q. How do I support a new filetype?
674698
A. You have several options.
675699

676-
1. Set caw variables on |FileType| event
700+
1. Set caw variables on |FileType| event (preferred)
677701

678702
caw supports comment settings by variables.
679703
See |caw-variables-comments| for the examples.
@@ -688,11 +712,13 @@ A. You have several options.
688712
(after/ftplugin/<filetype>/caw.vim in this repository),
689713
caw detects oneline / wrap oneline comment string by 'commentstring'.
690714

691-
I implemented this for fallback purpose because it may not be accurate,
692-
and wrap multiline comment can't be detected by this method.
693-
694-
It seems tpope/vim-commentary also supports this type of settings.
715+
If you want to use 'commentstring' instead of caw variables,
716+
you must tell caw to use 'commentstring' instead. >
695717
718+
" Use &commentstring in javascriptreact and typescriptreact buffers
719+
autocmd Filetype javascriptreact,typescriptreact
720+
\ call caw#update_comments_from_commentstring(&commentstring)
721+
<
696722
3. If you are interested in sending pull request :)
697723
3.1. Put comment string to macros/generate-ftplugins.vim
698724
3.2. Run `vim -u NONE -i NONE -N -S macros/generate-ftplugins.vim -c quit`

plugin/caw.vim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ call s:def('caw_box_sp_right', ' ')
9696

9797
call s:def('caw_find_another_action', 1)
9898

99+
call s:def('caw_integrated_plugin', 'auto')
100+
99101
delfunction s:def_deprecated
100102
delfunction s:def
101103
" }}}

0 commit comments

Comments
 (0)