Skip to content

Commit f64bafd

Browse files
habamaxchrisbra
authored andcommitted
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>
1 parent a420547 commit f64bafd

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

runtime/pack/dist/opt/comment/autoload/comment.vim

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,38 @@ export def Toggle(...args: list<string>): string
3535

3636
if len(cms_l) == 0 | return '' | endif
3737
if len(cms_l) == 1 | call add(cms_l, '') | endif
38-
var comment = 0
38+
var comment = false
39+
var indent_spaces = false
40+
var indent_tabs = false
3941
var indent_min = indent(lnum1)
4042
var indent_start = matchstr(getline(lnum1), '^\s*')
4143
for lnum in range(lnum1, lnum2)
4244
if getline(lnum) =~ '^\s*$' | continue | endif
45+
var indent_str = matchstr(getline(lnum), '^\s*')
4346
if indent_min > indent(lnum)
4447
indent_min = indent(lnum)
45-
indent_start = matchstr(getline(lnum), '^\s*')
48+
indent_start = indent_str
4649
endif
50+
indent_spaces = indent_spaces || (stridx(indent_str, ' ') != -1)
51+
indent_tabs = indent_tabs || (stridx(indent_str, "\t") != -1)
4752
if getline(lnum) !~ $'^\s*{cms_l[0]}.*{cms_l[1]}$'
48-
comment = 1
53+
comment = true
4954
endif
5055
endfor
56+
var mixed_indent = indent_spaces && indent_tabs
5157
var lines = []
5258
var line = ''
5359
for lnum in range(lnum1, lnum2)
5460
if getline(lnum) =~ '^\s*$'
5561
line = getline(lnum)
5662
elseif comment
5763
if exists("g:comment_first_col") || exists("b:comment_first_col")
58-
# handle % with substitute
5964
line = printf(substitute(cms, '%s\@!', '%%', 'g'), getline(lnum))
6065
else
61-
line = getline(lnum)
62-
var indent_start_len = strlen(indent_start)
63-
# handle % with substitute,
6466
# consider different whitespace indenting
65-
line = printf(indent_start .. substitute(cms, '%s\@!', '%%', 'g'),
66-
strpart(line, (line[0 : strlen(indent_start_len) - 1] =~ '\t' ?
67-
indent_start_len / &tabstop : indent_start_len)))
67+
var indent_current = mixed_indent ? matchstr(getline(lnum), '^\s*') : indent_start
68+
line = printf(indent_current .. substitute(cms, '%s\@!', '%%', 'g'),
69+
strpart(getline(lnum), strlen(indent_current)))
6870
endif
6971
else
7072
line = substitute(getline(lnum), $'^\s*\zs{cms_l[0]} \?\| \?{cms_l[1]}$', '', 'g')

0 commit comments

Comments
 (0)