aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/_comment.lua
diff options
context:
space:
mode:
authorEvgeni Chasnovski <evgeni.chasnovski@gmail.com>2024-05-23 23:30:53 +0300
committerGitHub <noreply@github.com>2024-05-23 15:30:53 -0500
commit0a2218f965ac8cd7967d33b8c52e5b06fb284aac (patch)
tree69d79f792771da62a27c565cc1045287fd9ec40d /runtime/lua/vim/_comment.lua
parentc614969570ac13cfc1ff6642fcaf56ebce6d40be (diff)
downloadrneovim-0a2218f965ac8cd7967d33b8c52e5b06fb284aac.tar.gz
rneovim-0a2218f965ac8cd7967d33b8c52e5b06fb284aac.tar.bz2
rneovim-0a2218f965ac8cd7967d33b8c52e5b06fb284aac.zip
fix(comment): fall back to using trimmed comment markers (#28938)
Problem: Currently comment detection, addition, and removal are done by matching 'commentstring' exactly. This has the downside when users want to add comment markers with space (like with `-- %s` commentstring) but also be able to uncomment lines that do not contain space (like `--aaa`). Solution: Use the following approach: - Line is commented if it matches 'commentstring' with trimmed parts. - Adding comment is 100% relying on 'commentstring' parts (as is now). - Removing comment is first trying exact 'commentstring' parts with fallback on trying its trimmed parts.
Diffstat (limited to 'runtime/lua/vim/_comment.lua')
-rw-r--r--runtime/lua/vim/_comment.lua19
1 files changed, 8 insertions, 11 deletions
diff --git a/runtime/lua/vim/_comment.lua b/runtime/lua/vim/_comment.lua
index b6cb6c9884..044cd69716 100644
--- a/runtime/lua/vim/_comment.lua
+++ b/runtime/lua/vim/_comment.lua
@@ -77,14 +77,11 @@ local function make_comment_check(parts)
local l_esc, r_esc = vim.pesc(parts.left), vim.pesc(parts.right)
-- Commented line has the following structure:
- -- <possible whitespace> <left> <anything> <right> <possible whitespace>
- local nonblank_regex = '^%s-' .. l_esc .. '.*' .. r_esc .. '%s-$'
-
- -- Commented blank line can have any amount of whitespace around parts
- local blank_regex = '^%s-' .. vim.trim(l_esc) .. '%s*' .. vim.trim(r_esc) .. '%s-$'
+ -- <whitespace> <trimmed left> <anything> <trimmed right> <whitespace>
+ local regex = '^%s-' .. vim.trim(l_esc) .. '.*' .. vim.trim(r_esc) .. '%s-$'
return function(line)
- return line:find(nonblank_regex) ~= nil or line:find(blank_regex) ~= nil
+ return line:find(regex) ~= nil
end
end
@@ -153,14 +150,14 @@ end
---@return fun(line: string): string
local function make_uncomment_function(parts)
local l_esc, r_esc = vim.pesc(parts.left), vim.pesc(parts.right)
- local nonblank_regex = '^(%s*)' .. l_esc .. '(.*)' .. r_esc .. '(%s-)$'
- local blank_regex = '^(%s*)' .. vim.trim(l_esc) .. '(%s*)' .. vim.trim(r_esc) .. '(%s-)$'
+ local regex = '^(%s*)' .. l_esc .. '(.*)' .. r_esc .. '(%s-)$'
+ local regex_trimmed = '^(%s*)' .. vim.trim(l_esc) .. '(.*)' .. vim.trim(r_esc) .. '(%s-)$'
return function(line)
- -- Try both non-blank and blank regexes
- local indent, new_line, trail = line:match(nonblank_regex)
+ -- Try regex with exact comment parts first, fall back to trimmed parts
+ local indent, new_line, trail = line:match(regex)
if new_line == nil then
- indent, new_line, trail = line:match(blank_regex)
+ indent, new_line, trail = line:match(regex_trimmed)
end
-- Return original if line is not commented