From 123adca013e55f4a8c4f9e48461cd7563736e912 Mon Sep 17 00:00:00 2001 From: tyru Date: Sun, 6 Jun 2021 11:22:27 +0900 Subject: [PATCH 1/7] Integrate JoosepAlviste/nvim-ts-context-commentstring - Add g:caw_integrated_plugin - Add caw#update_comments_from_commentstring() --- autoload/caw.vim | 120 ++++++++++++++++++++----- autoload/caw/comments.vim | 17 ++++ autoload/caw/comments/oneline.vim | 11 ++- autoload/caw/comments/wrap_oneline.vim | 9 +- doc/caw.txt | 36 ++++++-- plugin/caw.vim | 2 + 6 files changed, 165 insertions(+), 30 deletions(-) create mode 100644 autoload/caw/comments.vim diff --git a/autoload/caw.vim b/autoload/caw.vim index 3868647..17abe70 100644 --- a/autoload/caw.vim +++ b/autoload/caw.vim @@ -3,6 +3,7 @@ scriptencoding utf-8 let s:installed_repeat_vim = (globpath(&runtimepath, 'autoload/repeat.vim') !=# '') let s:installed_context_filetype = (globpath(&runtimepath, 'autoload/context_filetype.vim') !=# '') +let s:installed_ts_context_commentstring = (globpath(&runtimepath, 'plugin/ts_context_commentstring.vim') !=# '') let s:op_args = '' let s:op_doing = 0 @@ -17,9 +18,17 @@ function! caw#keymapping_stub(mode, action, method) abort normal! ^ endif + " When integration == 'context_filetype' + " Load comment string from the context filetype + " When integration == 'ts_context_commentstring' + " Load comment string from ts_context_commentstring plugin + " When integration == '' + " No additional setup will be done + let integration = s:get_integrated_plugin() + " Context filetype support. " https://github.com/Shougo/context_filetype.vim - if s:installed_context_filetype + if integration ==# 'context_filetype' let conft = context_filetype#get_filetype() else let conft = &l:filetype @@ -47,7 +56,13 @@ function! caw#keymapping_stub(mode, action, method) abort endif call caw#set_context(context) - if conft !=# &l:filetype + if integration ==# 'ts_context_commentstring' + let old_commentstring = &l:commentstring + lua require('ts_context_commentstring.internal').update_commentstring() + if &l:commentstring !=# old_commentstring + call caw#update_comments_from_commentstring(&l:commentstring) + endif + elseif conft !=# &l:filetype call caw#load_ftplugin(conft) endif @@ -104,6 +119,34 @@ function! caw#keymapping_stub(mode, action, method) abort endtry endfunction +" Returns "context_filetype" or "ts_context_commentstring" or "" +function! s:get_integrated_plugin() abort + let integration = caw#get_var('caw_integrated_plugin') + if integration ==# 'context_filetype' + if !s:installed_context_filetype + echohl ErrorMsg + echomsg 'Shougo/context_filetype.vim is not installed!' + echohl None + return '' + endif + return 'context_filetype' + elseif integration ==# 'ts_context_commentstring' + if !s:installed_ts_context_commentstring + echohl ErrorMsg + echomsg 'JoosepAlviste/nvim-ts-context-commentstring is not installed!' + echohl None + return '' + endif + return 'ts_context_commentstring' + elseif s:installed_context_filetype + return 'context_filetype' + elseif s:installed_ts_context_commentstring + return 'ts_context_commentstring' + else + return '' + endif +endfunction + function! caw#keymapping_stub_deprecated(mode, action, method, old_action) abort let oldmap = printf('(caw:%s:%s)', a:old_action, a:method) let newmap = printf('(caw:%s:%s)', a:action, a:method) @@ -150,25 +193,21 @@ endfunction " Utilities: Misc. functions. {{{ -if s:installed_context_filetype - function! caw#get_related_filetypes(ft) abort - let filetypes = get(context_filetype#filetypes(), a:ft, []) - let dup = {a:ft : 1} - let related = [] - for ft in map(copy(filetypes), 'v:val.filetype') - if !has_key(dup, ft) - let related += [ft] - let dup[ft] = 1 - endif - endfor - return related - endfunction -else - " vint: next-line -ProhibitUnusedVariable - function! caw#get_related_filetypes(ft) abort +function! caw#get_related_filetypes(ft) abort + if s:get_integrated_plugin() !=# 'context_filetype' return [] - endfunction -endif + endif + let filetypes = get(context_filetype#filetypes(), a:ft, []) + let dup = {a:ft : 1} + let related = [] + for ft in map(copy(filetypes), 'v:val.filetype') + if !has_key(dup, ft) + let related += [ft] + let dup[ft] = 1 + endif + endfor + return related +endfunction function! caw#assert(cond, msg) abort if !a:cond @@ -339,6 +378,47 @@ function! caw#load_ftplugin(ft) abort execute 'runtime! after/ftplugin/' . a:ft . '/caw.vim' endfunction +function! caw#update_comments_from_commentstring(cms) abort + if !exists('b:did_caw_ftplugin') + " Raise error when caw ftplugin would override comment variables + echohl ErrorMsg + echomsg 'Call caw#update_comments_from_commentstring() after caw ftplugin is loaded' + echohl None + return + endif + + let undo = [] + + let parsed = caw#comments#parse_commentstring(a:cms) + if has_key(parsed, 'oneline') + let b:caw_oneline_comment = parsed.oneline + let undo += ['b:caw_oneline_comment'] + else + unlet! b:caw_oneline_comment + endif + if has_key(parsed, 'wrap_oneline') + let b:caw_wrap_oneline_comment = parsed.wrap_oneline + let undo += ['b:caw_wrap_oneline_comment'] + else + unlet! b:caw_wrap_oneline_comment + endif + if has_key(parsed, 'wrap_multiline') + let b:caw_wrap_multiline_comment = parsed.wrap_multiline + let undo += ['b:caw_wrap_multiline_comment'] + else + unlet! b:caw_wrap_multiline_comment + endif + + if !empty(undo) + if exists('b:undo_ftplugin') + let b:undo_ftplugin .= ' | ' + else + let b:undo_ftplugin = '' + endif + let b:undo_ftplugin .= 'unlet! ' . join(undo) + endif +endfunction + " '.../autoload/caw' " vint: next-line -ProhibitUnusedVariable diff --git a/autoload/caw/comments.vim b/autoload/caw/comments.vim new file mode 100644 index 0000000..6d80603 --- /dev/null +++ b/autoload/caw/comments.vim @@ -0,0 +1,17 @@ +scriptencoding utf-8 + +function! caw#comments#parse_commentstring(cms) abort + let parsed = {} + + let oneline = caw#comments#oneline#new().parse_commentstring(a:cms) + if !empty(oneline) + let parsed.oneline = oneline + endif + + let wrap_oneline = caw#comments#wrap_oneline#new().parse_commentstring(a:cms) + if !empty(wrap_oneline) + let parsed.wrap_oneline = wrap_oneline + endif + + return parsed +endfunction diff --git a/autoload/caw/comments/oneline.vim b/autoload/caw/comments/oneline.vim index a7affad..f4efc61 100644 --- a/autoload/caw/comments/oneline.vim +++ b/autoload/caw/comments/oneline.vim @@ -32,9 +32,14 @@ function! s:oneline.get_comment_vars() abort endfunction function! s:oneline.get_comment_detect() abort - let m = matchlist(&l:commentstring, '^\(.\{-}\)[ \t]*%s[ \t]*\(.*\)$') + let c = self.parse_commentstring(&l:commentstring) + return !empty(c) ? [c] : [] +endfunction + +function! s:oneline.parse_commentstring(cms) abort + let m = matchlist(a:cms, '^\(.\{-}\)[ \t]*%s[ \t]*\(.*\)$') if !empty(m) && m[1] !=# '' && m[2] ==# '' - return [m[1]] + return m[1] endif - return [] + return '' endfunction diff --git a/autoload/caw/comments/wrap_oneline.vim b/autoload/caw/comments/wrap_oneline.vim index 22baad8..97c1d0a 100644 --- a/autoload/caw/comments/wrap_oneline.vim +++ b/autoload/caw/comments/wrap_oneline.vim @@ -38,9 +38,14 @@ function! s:wrap_oneline.get_comment_vars() abort endfunction function! s:wrap_oneline.get_comment_detect() abort - let m = matchlist(&l:commentstring, '^\(.\{-}\)[ \t]*%s[ \t]*\(.*\)$') + let c = self.parse_commentstring(&l:commentstring) + return !empty(c) ? [c] : [] +endfunction + +function! s:wrap_oneline.parse_commentstring(cms) abort + let m = matchlist(a:cms, '^\(.\{-}\)[ \t]*%s[ \t]*\(.*\)$') if !empty(m) && m[1] !=# '' && m[2] !=# '' - return [m[1:2]] + return m[1:2] endif return [] endfunction diff --git a/doc/caw.txt b/doc/caw.txt index 8873d74..2055cc9 100644 --- a/doc/caw.txt +++ b/doc/caw.txt @@ -25,6 +25,7 @@ Introduction |caw-introduction| Features |caw-features| Supported filetypes |caw-supported-filetypes| Interface |caw-interface| + Functions |caw-functions| Keymappings |caw-keymappings| Default keymappings |caw-keymappings-default| Prefix keymapping |caw-keymappings-prefix| @@ -165,6 +166,16 @@ See "after/ftplugin/*" in this repository for current supported filetypes. }}} ============================================================================== INTERFACE *caw-interface* {{{ + +------------------------------------------------------------------------------ +FUNCTIONS *caw-functions* {{{ + + *caw#update_comments_from_commentstring()* +caw#update_comments_from_commentstring({commentstring}) + + This function parses {commentstring} and sets / unsets + |caw-variables-comments|. + ------------------------------------------------------------------------------ KEYMAPPINGS *caw-keymappings* {{{ @@ -654,6 +665,19 @@ g:caw_operator_keymappings *g:caw_operator_keymappings* g:caw_find_another_action *g:caw_find_another_action* (Default: 1) +g:caw_integrated_plugin *g:caw_integrated_plugin* + (Default: "auto") + + This variable detemines that how caw detects comment string + from integrated plugins. + + "context_filetype" + Use https://github.com/Shougo/context_filetype.vim + "ts_context_commentstring" + Use https://github.com/JoosepAlviste/nvim-ts-context-commentstring + "auto" + Use context_filetype or ts_context_commentstring if it's installed + }}} }}} @@ -673,7 +697,7 @@ A. You can change prefix keymapping Q. How do I support a new filetype? A. You have several options. -1. Set caw variables on |FileType| event +1. Set caw variables on |FileType| event (preferred) caw supports comment settings by variables. See |caw-variables-comments| for the examples. @@ -688,11 +712,13 @@ A. You have several options. (after/ftplugin//caw.vim in this repository), caw detects oneline / wrap oneline comment string by 'commentstring'. - I implemented this for fallback purpose because it may not be accurate, - and wrap multiline comment can't be detected by this method. - - It seems tpope/vim-commentary also supports this type of settings. + If you want to use 'commentstring' instead of caw variables, + you must tell caw to use 'commentstring' instead. > + " Use &commentstring in javascriptreact and typescriptreact buffers + autocmd Filetype javascriptreact,typescriptreact + \ call caw#update_comments_from_commentstring(&commentstring) +< 3. If you are interested in sending pull request :) 3.1. Put comment string to macros/generate-ftplugins.vim 3.2. Run `vim -u NONE -i NONE -N -S macros/generate-ftplugins.vim -c quit` diff --git a/plugin/caw.vim b/plugin/caw.vim index 098025a..49c4132 100644 --- a/plugin/caw.vim +++ b/plugin/caw.vim @@ -96,6 +96,8 @@ call s:def('caw_box_sp_right', ' ') call s:def('caw_find_another_action', 1) +call s:def('caw_integrated_plugin', 'auto') + delfunction s:def_deprecated delfunction s:def " }}} From 703db47ca390d939f58c4c16836f84369098986a Mon Sep 17 00:00:00 2001 From: tyru Date: Mon, 7 Jun 2021 18:10:38 +0900 Subject: [PATCH 2/7] wip --- autoload/caw.vim | 60 +++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/autoload/caw.vim b/autoload/caw.vim index 17abe70..d6f2496 100644 --- a/autoload/caw.vim +++ b/autoload/caw.vim @@ -57,10 +57,11 @@ function! caw#keymapping_stub(mode, action, method) abort call caw#set_context(context) if integration ==# 'ts_context_commentstring' - let old_commentstring = &l:commentstring - lua require('ts_context_commentstring.internal').update_commentstring() - if &l:commentstring !=# old_commentstring - call caw#update_comments_from_commentstring(&l:commentstring) + let ts_cms = luaeval('require("ts_context_commentstring.internal").calculate_commentstring()') + if ts_cms !=# &l:commentstring + let l:UndoVariables = caw#update_comments_from_commentstring(ts_cms) + else + let l:UndoVariables = {-> 'nop'} endif elseif conft !=# &l:filetype call caw#load_ftplugin(conft) @@ -100,7 +101,9 @@ function! caw#keymapping_stub(mode, action, method) abort echomsg '[' . v:exception . ']::[' . v:throwpoint . ']' echohl None finally - if conft !=# &l:filetype + if integration ==# 'ts_context_commentstring' + call l:UndoVariables() + elseif conft !=# &l:filetype call caw#load_ftplugin(&l:filetype) endif " Free context. @@ -379,44 +382,49 @@ function! caw#load_ftplugin(ft) abort endfunction function! caw#update_comments_from_commentstring(cms) abort - if !exists('b:did_caw_ftplugin') - " Raise error when caw ftplugin would override comment variables - echohl ErrorMsg - echomsg 'Call caw#update_comments_from_commentstring() after caw ftplugin is loaded' - echohl None - return - endif - - let undo = [] - let parsed = caw#comments#parse_commentstring(a:cms) + let variables = [] + + if exists('b:caw_oneline_comment') + let variables += ['let b:caw_oneline_comment = ' . string(b:caw_oneline_comment)] + else + let variables += ['unlet! b:caw_oneline_comment'] + endif if has_key(parsed, 'oneline') let b:caw_oneline_comment = parsed.oneline - let undo += ['b:caw_oneline_comment'] else unlet! b:caw_oneline_comment endif + + if exists('b:caw_wrap_oneline_comment') + let variables += ['let b:caw_wrap_oneline_comment = ' . string(b:caw_wrap_oneline_comment)] + else + let variables += ['unlet! b:caw_wrap_oneline_comment'] + endif if has_key(parsed, 'wrap_oneline') let b:caw_wrap_oneline_comment = parsed.wrap_oneline - let undo += ['b:caw_wrap_oneline_comment'] else unlet! b:caw_wrap_oneline_comment endif + + if exists('b:caw_wrap_multiline_comment') + let variables += ['let b:caw_wrap_multiline_comment = ' . string(b:caw_wrap_multiline_comment)] + else + let variables += ['unlet! b:caw_wrap_multiline_comment'] + endif if has_key(parsed, 'wrap_multiline') let b:caw_wrap_multiline_comment = parsed.wrap_multiline - let undo += ['b:caw_wrap_multiline_comment'] else unlet! b:caw_wrap_multiline_comment endif - if !empty(undo) - if exists('b:undo_ftplugin') - let b:undo_ftplugin .= ' | ' - else - let b:undo_ftplugin = '' - endif - let b:undo_ftplugin .= 'unlet! ' . join(undo) - endif + function! s:undo_variables() abort closure + for undo in variables + execute undo + endfor + endfunction + + return funcref('s:undo_variables') endfunction From f5e098c847cf0cc85e7ae380b2d640a8fac33355 Mon Sep 17 00:00:00 2001 From: tyru Date: Sat, 21 Aug 2021 18:42:29 +0900 Subject: [PATCH 3/7] call caw#update_comments_from_commentstring() always --- autoload/caw.vim | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/autoload/caw.vim b/autoload/caw.vim index d6f2496..8b45f34 100644 --- a/autoload/caw.vim +++ b/autoload/caw.vim @@ -58,11 +58,12 @@ function! caw#keymapping_stub(mode, action, method) abort if integration ==# 'ts_context_commentstring' let ts_cms = luaeval('require("ts_context_commentstring.internal").calculate_commentstring()') - if ts_cms !=# &l:commentstring - let l:UndoVariables = caw#update_comments_from_commentstring(ts_cms) - else - let l:UndoVariables = {-> 'nop'} - endif + let l:UndoVariables = caw#update_comments_from_commentstring(ts_cms) + " if ts_cms !=# &l:commentstring + " let l:UndoVariables = caw#update_comments_from_commentstring(ts_cms) + " else + " let l:UndoVariables = {-> 'nop'} + " endif elseif conft !=# &l:filetype call caw#load_ftplugin(conft) endif From b7d38313b2dc0ca1fd14248af46a2e2d52dbc0fc Mon Sep 17 00:00:00 2001 From: tyru Date: Sat, 21 Aug 2021 19:00:04 +0900 Subject: [PATCH 4/7] don't :unlet! caw variables --- autoload/caw.vim | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/autoload/caw.vim b/autoload/caw.vim index 8b45f34..e0b2278 100644 --- a/autoload/caw.vim +++ b/autoload/caw.vim @@ -59,11 +59,6 @@ function! caw#keymapping_stub(mode, action, method) abort if integration ==# 'ts_context_commentstring' let ts_cms = luaeval('require("ts_context_commentstring.internal").calculate_commentstring()') let l:UndoVariables = caw#update_comments_from_commentstring(ts_cms) - " if ts_cms !=# &l:commentstring - " let l:UndoVariables = caw#update_comments_from_commentstring(ts_cms) - " else - " let l:UndoVariables = {-> 'nop'} - " endif elseif conft !=# &l:filetype call caw#load_ftplugin(conft) endif @@ -393,8 +388,6 @@ function! caw#update_comments_from_commentstring(cms) abort endif if has_key(parsed, 'oneline') let b:caw_oneline_comment = parsed.oneline - else - unlet! b:caw_oneline_comment endif if exists('b:caw_wrap_oneline_comment') @@ -404,8 +397,6 @@ function! caw#update_comments_from_commentstring(cms) abort endif if has_key(parsed, 'wrap_oneline') let b:caw_wrap_oneline_comment = parsed.wrap_oneline - else - unlet! b:caw_wrap_oneline_comment endif if exists('b:caw_wrap_multiline_comment') @@ -415,8 +406,6 @@ function! caw#update_comments_from_commentstring(cms) abort endif if has_key(parsed, 'wrap_multiline') let b:caw_wrap_multiline_comment = parsed.wrap_multiline - else - unlet! b:caw_wrap_multiline_comment endif function! s:undo_variables() abort closure From c52d0c0c888daced7128a9d544d69e79aa42bd51 Mon Sep 17 00:00:00 2001 From: tyru Date: Sat, 21 Aug 2021 19:08:31 +0900 Subject: [PATCH 5/7] add debug message --- autoload/caw.vim | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/autoload/caw.vim b/autoload/caw.vim index e0b2278..faf82f2 100644 --- a/autoload/caw.vim +++ b/autoload/caw.vim @@ -56,8 +56,11 @@ function! caw#keymapping_stub(mode, action, method) abort endif call caw#set_context(context) + echom 'integration:' integration if integration ==# 'ts_context_commentstring' let ts_cms = luaeval('require("ts_context_commentstring.internal").calculate_commentstring()') + echom 'ts_cms:' ts_cms + echom 'b:caw_*' string(filter(copy(b:), 'v:key =~# "^caw_"')) let l:UndoVariables = caw#update_comments_from_commentstring(ts_cms) elseif conft !=# &l:filetype call caw#load_ftplugin(conft) @@ -408,6 +411,9 @@ function! caw#update_comments_from_commentstring(cms) abort let b:caw_wrap_multiline_comment = parsed.wrap_multiline endif + echom 'parsed:' string(parsed) + echom 'variables:' string(variables) + function! s:undo_variables() abort closure for undo in variables execute undo From 3b4b7e507b8094e28b9f35f70762dec2d80b1c67 Mon Sep 17 00:00:00 2001 From: tyru Date: Sat, 21 Aug 2021 19:16:08 +0900 Subject: [PATCH 6/7] revert :unlet! --- autoload/caw.vim | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/autoload/caw.vim b/autoload/caw.vim index faf82f2..73658ca 100644 --- a/autoload/caw.vim +++ b/autoload/caw.vim @@ -391,6 +391,8 @@ function! caw#update_comments_from_commentstring(cms) abort endif if has_key(parsed, 'oneline') let b:caw_oneline_comment = parsed.oneline + else + unlet! b:caw_oneline_comment endif if exists('b:caw_wrap_oneline_comment') @@ -400,6 +402,8 @@ function! caw#update_comments_from_commentstring(cms) abort endif if has_key(parsed, 'wrap_oneline') let b:caw_wrap_oneline_comment = parsed.wrap_oneline + else + unlet! b:caw_wrap_oneline_comment endif if exists('b:caw_wrap_multiline_comment') @@ -409,6 +413,8 @@ function! caw#update_comments_from_commentstring(cms) abort endif if has_key(parsed, 'wrap_multiline') let b:caw_wrap_multiline_comment = parsed.wrap_multiline + else + unlet! b:caw_wrap_multiline_comment endif echom 'parsed:' string(parsed) From 0edb74505cd55a7af3e30981f0bd1dd6009a7836 Mon Sep 17 00:00:00 2001 From: tyru Date: Sat, 21 Aug 2021 19:18:11 +0900 Subject: [PATCH 7/7] add more debug --- autoload/caw.vim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/autoload/caw.vim b/autoload/caw.vim index 73658ca..ffe256a 100644 --- a/autoload/caw.vim +++ b/autoload/caw.vim @@ -60,8 +60,9 @@ function! caw#keymapping_stub(mode, action, method) abort if integration ==# 'ts_context_commentstring' let ts_cms = luaeval('require("ts_context_commentstring.internal").calculate_commentstring()') echom 'ts_cms:' ts_cms - echom 'b:caw_*' string(filter(copy(b:), 'v:key =~# "^caw_"')) + echom '(1) b:caw_*' string(filter(copy(b:), 'v:key =~# "^caw_"')) let l:UndoVariables = caw#update_comments_from_commentstring(ts_cms) + echom '(2) b:caw_*' string(filter(copy(b:), 'v:key =~# "^caw_"')) elseif conft !=# &l:filetype call caw#load_ftplugin(conft) endif