changeset 36302:fb04b58dd2b6 draft

runtime(comment): fix commment toggle with mixed tabs & spaces Commit: https://github.com/vim/vim/commit/f64bafd98a775de4b8a762c411ee11d6411bba7d Author: Maxim Kim <habamax@gmail.com> 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 <habamax@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Sun, 13 Oct 2024 19:30:06 +0200
parents fc1aeadd8e5c
children 65c66e5a1524
files runtime/pack/dist/opt/comment/autoload/comment.vim
diffstat 1 files changed, 12 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- 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<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>)
             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')