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

Commit: 
https://github.com/vim/vim/commit/f64bafd98a775de4b8a762c411ee11d6411bba7d
Author: Maxim Kim <haba...@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 <haba...@gmail.com>
    Signed-off-by: Christian Brabandt <c...@256bit.org>

diff --git a/runtime/pack/dist/opt/comment/autoload/comment.vim 
b/runtime/pack/dist/opt/comment/autoload/comment.vim
index d78b69b2a..086d5bff0 100644
--- 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>): 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, "     ") != -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] =~ ' 
' ?
-                    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')

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to vim_dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/E1t02Pu-00EbFW-Gj%40256bit.org.

Raspunde prin e-mail lui