Skip to content

Commit

Permalink
Support for choose: Conditionally select a comment definition from a …
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtom committed Mar 14, 2020
1 parent 20e85e8 commit 0628c96
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
14 changes: 12 additions & 2 deletions autoload/tcomment.vim
Expand Up @@ -2,8 +2,8 @@
" @Website: http://www.vim.org/account/profile.php?user_id=4037
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Created: 2007-09-17.
" @Last Change: 2019-03-17.
" @Revision: 2036
" @Last Change: 2020-03-14.
" @Revision: 2039

scriptencoding utf-8

Expand Down Expand Up @@ -279,6 +279,16 @@ endf
" postprocess_uncomment .. Run a |printf()| expression with 2
" placeholders on uncommented lines, e.g.
" 'norm! %sgg=%sgg'.
" choose ... A list of commend definitions (a
" dictionary as defined above) that may
" contain an `if` key referring to an
" expression; if this condition evaluates
" to true, the item will be selected; the
" last item in the list will be selected
" anyway (see the bib definition for an
" example)
" if ... an |eval()|able expression (only valid
" within a choose list, see above)
" 2. 1-2 values for: ?commentPrefix, ?commentPostfix
" 3. a dictionary (internal use only)
"
Expand Down
33 changes: 29 additions & 4 deletions autoload/tcomment/type.vim
Expand Up @@ -2,8 +2,8 @@
" @Website: http://www.vim.org/account/profile.php?user_id=4037
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Created: 2007-09-17.
" @Last Change: 2018-09-03.
" @Revision: 33
" @Last Change: 2020-03-12.
" @Revision: 42

if exists(':Tlibtrace') != 2
command! -nargs=+ -bang Tlibtrace :
Expand Down Expand Up @@ -90,11 +90,36 @@ endf
" Return b:tcomment_def_{NAME} if the variable exists. Otherwise return
" the comment definition as set with |tcomment#type#Define|.
function! tcomment#type#GetDefinition(name, ...) abort
Tlibtrace 'tcomment', a:name
if exists('b:tcomment_def_'. a:name)
return b:tcomment_def_{a:name}
let def = b:tcomment_def_{a:name}
else
return get(s:definitions, a:name, a:0 >= 1 ? a:1 : '')
let def = get(s:definitions, a:name, a:0 >= 1 ? a:1 : '')
endif
Tlibtrace 'tcomment', def
if has_key(def, 'choose')
let def = copy(def)
let defs = def.choose
let ndefs = len(defs)
Tlibtrace 'tcomment', ndefs
for idef in range(ndefs)
let cdef = defs[idef]
Tlibtrace 'tcomment', idef, cdef
if idef == ndefs - 1
let choose = 1
else
let choose = eval(cdef.if)
endif
Tlibtrace 'tcomment', choose
if choose
call remove(def, 'choose')
let def = extend(def, cdef)
break
endif
endfor
endif
Tlibtrace 'tcomment', def
return def
endf


Expand Down
6 changes: 3 additions & 3 deletions autoload/tcomment/types/default.vim
@@ -1,8 +1,8 @@
" @Author: Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @Website: https://github.com/tomtom
" @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Last Change: 2019-03-17
" @Revision: 18
" @Last Change: 2020-03-12
" @Revision: 23

call tcomment#type#Define('aap', '# %s' )
call tcomment#type#Define('ada', '-- %s' )
Expand All @@ -14,7 +14,7 @@ call tcomment#type#Define('applescript_inline','# %s' )
call tcomment#type#Define('asciidoc', '// %s' )
call tcomment#type#Define('asm', '; %s' )
call tcomment#type#Define('asterisk', '; %s' )
call tcomment#type#Define('bib', '%% %s' )
call tcomment#type#Define('bib', {'choose': [{'if': 'col(".") <= 1', 'commentstring': '@Comment{%s}'}, {'commentstring': '%% %s'}]})
call tcomment#type#Define('blade', '{{-- %s --}}' )
call tcomment#type#Define('blade_block', '{{--%s--}}' )
call tcomment#type#Define('blade_inline', '{{-- %s --}}' )
Expand Down
10 changes: 10 additions & 0 deletions doc/tcomment.txt
Expand Up @@ -479,6 +479,16 @@ tcomment#Comment(beg, end, ...)
postprocess_uncomment .. Run a |printf()| expression with 2
placeholders on uncommented lines, e.g.
'norm! %sgg=%sgg'.
choose ... A list of commend definitions (a
dictionary as defined above) that may
contain an `if` key referring to an
expression; if this condition evaluates
to true, the item will be selected; the
last item in the list will be selected
anyway (see the bib definition for an
example)
if ... an |eval()|able expression (only valid
within a choose list, see above)
2. 1-2 values for: ?commentPrefix, ?commentPostfix
3. a dictionary (internal use only)

Expand Down

0 comments on commit 0628c96

Please sign in to comment.