runtime(comment): add gC mapping to (un)comment rest of line

fixes: #15727
closes: #15737

Signed-off-by: Konfekt <Konfekt@users.noreply.github.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Konfekt
2024-09-29 10:46:41 +02:00
committed by Christian Brabandt
parent 8feed3a525
commit 9142136161
3 changed files with 31 additions and 10 deletions

View File

@ -1,7 +1,7 @@
vim9script
# Maintainer: Maxim Kim <habamax@gmail.com>
# Last Update: 2024-04-26
# Last Update: 2024-09-24
#
# Toggle comments
# Usage:
@ -10,6 +10,7 @@ vim9script
# nnoremap <silent> <expr> gc comment.Toggle()
# xnoremap <silent> <expr> gc comment.Toggle()
# nnoremap <silent> <expr> gcc comment.Toggle() .. '_'
# nnoremap <silent> <expr> gC comment.Toggle() .. '$'
export def Toggle(...args: list<string>): string
if len(args) == 0
&opfunc = matchstr(expand('<stack>'), '[^. ]*\ze[')
@ -19,6 +20,19 @@ export def Toggle(...args: list<string>): string
var cms = substitute(substitute(&cms, '\S\zs%s\s*', ' %s', ''), '%s\ze\S', '%s ', '')
var [lnum1, lnum2] = [line("'["), line("']")]
var cms_l = split(escape(cms, '*.'), '\s*%s\s*')
var first_col = indent(lnum1)
var start_col = getpos("'[")[2]
if len(cms_l) == 1 && lnum1 == lnum2 && first_col < start_col
var line_start = getline(lnum1)[0 : max(0, start_col - 2)]
var line_end = getline(lnum1)[start_col - 1 : -1]
line_end = line_end =~# $'^\s*{cms_l[0]}' ?
\ substitute(line_end, $'^\s*\zs{cms_l[0]}', '', '') :
\ printf(substitute(cms, '%s\@!', '%%', 'g'), line_end)
setline(lnum1, line_start .. line_end)
return ''
endif
if len(cms_l) == 0 | return '' | endif
if len(cms_l) == 1 | call add(cms_l, '') | endif
var comment = 0