@@ -57,10 +57,11 @@ function! caw#keymapping_stub(mode, action, method) abort
5757 call caw#set_context (context)
5858
5959 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 )
60+ let ts_cms = luaeval (' require("ts_context_commentstring.internal").calculate_commentstring()' )
61+ if ts_cms !=# &l: commentstring
62+ let l: UndoVariables = caw#update_comments_from_commentstring (ts_cms)
63+ else
64+ let l: UndoVariables = {- > ' nop' }
6465 endif
6566 elseif conft !=# &l: filetype
6667 call caw#load_ftplugin (conft)
@@ -100,7 +101,9 @@ function! caw#keymapping_stub(mode, action, method) abort
100101 echomsg ' [' . v: exception . ' ]::[' . v: throwpoint . ' ]'
101102 echohl None
102103 finally
103- if conft !=# &l: filetype
104+ if integration == # ' ts_context_commentstring'
105+ call l: UndoVariables ()
106+ elseif conft !=# &l: filetype
104107 call caw#load_ftplugin (&l: filetype )
105108 endif
106109 " Free context.
@@ -379,44 +382,49 @@ function! caw#load_ftplugin(ft) abort
379382endfunction
380383
381384function ! 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-
392385 let parsed = caw#comments#parse_commentstring (a: cms )
386+ let variables = []
387+
388+ if exists (' b:caw_oneline_comment' )
389+ let variables += [' let b:caw_oneline_comment = ' . string (b: caw_oneline_comment )]
390+ else
391+ let variables += [' unlet! b:caw_oneline_comment' ]
392+ endif
393393 if has_key (parsed, ' oneline' )
394394 let b: caw_oneline_comment = parsed.oneline
395- let undo += [' b:caw_oneline_comment' ]
396395 else
397396 unlet ! b: caw_oneline_comment
398397 endif
398+
399+ if exists (' b:caw_wrap_oneline_comment' )
400+ let variables += [' let b:caw_wrap_oneline_comment = ' . string (b: caw_wrap_oneline_comment )]
401+ else
402+ let variables += [' unlet! b:caw_wrap_oneline_comment' ]
403+ endif
399404 if has_key (parsed, ' wrap_oneline' )
400405 let b: caw_wrap_oneline_comment = parsed.wrap_oneline
401- let undo += [' b:caw_wrap_oneline_comment' ]
402406 else
403407 unlet ! b: caw_wrap_oneline_comment
404408 endif
409+
410+ if exists (' b:caw_wrap_multiline_comment' )
411+ let variables += [' let b:caw_wrap_multiline_comment = ' . string (b: caw_wrap_multiline_comment )]
412+ else
413+ let variables += [' unlet! b:caw_wrap_multiline_comment' ]
414+ endif
405415 if has_key (parsed, ' wrap_multiline' )
406416 let b: caw_wrap_multiline_comment = parsed.wrap_multiline
407- let undo += [' b:caw_wrap_multiline_comment' ]
408417 else
409418 unlet ! b: caw_wrap_multiline_comment
410419 endif
411420
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
421+ function ! s: undo_variables () abort closure
422+ for undo in variables
423+ execute undo
424+ endfor
425+ endfunction
426+
427+ return funcref (' s:undo_variables' )
420428endfunction
421429
422430
0 commit comments