Skip to content

Commit

Permalink
Integrate JoosepAlviste/nvim-ts-context-commentstring
Browse files Browse the repository at this point in the history
- Add g:caw_integrated_plugin
- Add caw#update_comments_from_commentstring()
  • Loading branch information
tyru committed Sep 20, 2021
1 parent 8978f97 commit 7067fe9
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 30 deletions.
120 changes: 100 additions & 20 deletions autoload/caw.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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('<Plug>(caw:%s:%s)', a:old_action, a:method)
let newmap = printf('<Plug>(caw:%s:%s)', a:action, a:method)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions autoload/caw/comments.vim
Original file line number Diff line number Diff line change
@@ -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
11 changes: 8 additions & 3 deletions autoload/caw/comments/oneline.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 7 additions & 2 deletions autoload/caw/comments/wrap_oneline.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 31 additions & 5 deletions doc/caw.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -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* {{{

Expand Down Expand Up @@ -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

}}}

}}}
Expand All @@ -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.
Expand All @@ -688,11 +712,13 @@ A. You have several options.
(after/ftplugin/<filetype>/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`
Expand Down
2 changes: 2 additions & 0 deletions plugin/caw.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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
" }}}
Expand Down

0 comments on commit 7067fe9

Please sign in to comment.