runtime(comment): fix commment toggle with mixed tabs & spaces

- fix regression where toggling doesn't properly remove comment chars in
  files with tabs indents only.
- refactor toggling comments for mixed tabs & spaces sources

closes: #15861

Signed-off-by: Maxim Kim <habamax@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
This commit is contained in:
Maxim Kim
2024-10-13 19:20:53 +02:00
committed by Christian Brabandt
parent a4205471ad
commit f64bafd98a

View File

@ -35,19 +35,25 @@ export def Toggle(...args: list<string>): string
if len(cms_l) == 0 | return '' | endif
if len(cms_l) == 1 | call add(cms_l, '') | endif
var comment = 0
var comment = false
var indent_spaces = false
var indent_tabs = false
var indent_min = indent(lnum1)
var indent_start = matchstr(getline(lnum1), '^\s*')
for lnum in range(lnum1, lnum2)
if getline(lnum) =~ '^\s*$' | continue | endif
var indent_str = matchstr(getline(lnum), '^\s*')
if indent_min > indent(lnum)
indent_min = indent(lnum)
indent_start = matchstr(getline(lnum), '^\s*')
indent_start = indent_str
endif
indent_spaces = indent_spaces || (stridx(indent_str, ' ') != -1)
indent_tabs = indent_tabs || (stridx(indent_str, "\t") != -1)
if getline(lnum) !~ $'^\s*{cms_l[0]}.*{cms_l[1]}$'
comment = 1
comment = true
endif
endfor
var mixed_indent = indent_spaces && indent_tabs
var lines = []
var line = ''
for lnum in range(lnum1, lnum2)
@ -55,16 +61,12 @@ export def Toggle(...args: list<string>): string
line = getline(lnum)
elseif comment
if exists("g:comment_first_col") || exists("b:comment_first_col")
# handle % with substitute
line = printf(substitute(cms, '%s\@!', '%%', 'g'), getline(lnum))
else
line = getline(lnum)
var indent_start_len = strlen(indent_start)
# handle % with substitute,
# consider different whitespace indenting
line = printf(indent_start .. substitute(cms, '%s\@!', '%%', 'g'),
strpart(line, (line[0 : strlen(indent_start_len) - 1] =~ '\t' ?
indent_start_len / &tabstop : indent_start_len)))
var indent_current = mixed_indent ? matchstr(getline(lnum), '^\s*') : indent_start
line = printf(indent_current .. substitute(cms, '%s\@!', '%%', 'g'),
strpart(getline(lnum), strlen(indent_current)))
endif
else
line = substitute(getline(lnum), $'^\s*\zs{cms_l[0]} \?\| \?{cms_l[1]}$', '', 'g')