@@ -3,6 +3,7 @@ scriptencoding utf-8
33
44let s: installed_repeat_vim = (globpath (&runtimepath , ' autoload/repeat.vim' ) !=# ' ' )
55let s: installed_context_filetype = (globpath (&runtimepath , ' autoload/context_filetype.vim' ) !=# ' ' )
6+ let s: installed_ts_context_commentstring = (globpath (&runtimepath , ' plugin/ts_context_commentstring.vim' ) !=# ' ' )
67let s: op_args = ' '
78let 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
105120endfunction
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+
107150function ! 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
173212function ! 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'
340379endfunction
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
0 commit comments