Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate JoosepAlviste/nvim-ts-context-commentstring #165

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 112 additions & 21 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'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure how Shougo/context_filetype.vim works, but I think that a user might use both context_filetype.vim and nvim-ts-context-commentstring (in different files). Some languages like vimscript don't have a treesitter parser, so using context_filetype.vim for those makes sense. Right now, it looks like only one of those integrations would work.

let conft = context_filetype#get_filetype()
else
let conft = &l:filetype
Expand Down Expand Up @@ -47,7 +56,14 @@ function! caw#keymapping_stub(mode, action, method) abort
endif
call caw#set_context(context)

if conft !=# &l:filetype
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 '(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

Expand Down Expand Up @@ -85,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.
Expand All @@ -104,6 +122,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 +196,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 +381,55 @@ function! caw#load_ftplugin(ft) abort
execute 'runtime! after/ftplugin/' . a:ft . '/caw.vim'
endfunction

function! caw#update_comments_from_commentstring(cms) abort
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
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
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
else
unlet! b:caw_wrap_multiline_comment
endif

echom 'parsed:' string(parsed)
echom 'variables:' string(variables)

function! s:undo_variables() abort closure
for undo in variables
execute undo
endfor
endfunction

return funcref('s:undo_variables')
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