# HG changeset patch # User Christian Brabandt # Date 1728840606 -7200 # Node ID fb04b58dd2b6e9364bdf2924eaa1048734307a28 # Parent fc1aeadd8e5c5fd70a97e35f07e56b468a0f788c runtime(comment): fix commment toggle with mixed tabs & spaces Commit: https://github.com/vim/vim/commit/f64bafd98a775de4b8a762c411ee11d6411bba7d Author: Maxim Kim Date: Sun Oct 13 19:20:53 2024 +0200 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 Signed-off-by: Christian Brabandt diff --git a/runtime/pack/dist/opt/comment/autoload/comment.vim b/runtime/pack/dist/opt/comment/autoload/comment.vim --- a/runtime/pack/dist/opt/comment/autoload/comment.vim +++ b/runtime/pack/dist/opt/comment/autoload/comment.vim @@ -35,19 +35,25 @@ export def Toggle(...args: list) 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) 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')