Impl uncommenting with leading whitespace

This commit is contained in:
Stephen Seo 2022-11-19 18:15:19 +09:00
parent aba093a42c
commit ddeffdce4f
2 changed files with 32 additions and 2 deletions

View File

@ -21,6 +21,26 @@ Or in init.vim:
nmap q :lua vim.g.quickcomment_togglecommentline()<CR>
vmap q :luado vim.g.quickcomment_togglecommentline(linenr)<CR>
## Whitespace before comment string
If `b:quickcomment_whitespaceprefix` or `g:quickcomment_whitespaceprefix` is set
to true, then quickcomment will uncomment comments with leading whitespace.
In `init.vim` this option can be set globally with:
let g:quickcomment_whitespaceprefix = 1
or locally with:
let b:quickcomment_whitespaceprefix = 1
Or in `init.lua` set this option with:
vim.g.quickcomment_whitespaceprefix = true
vim.b.quickcomment_whitespaceprefix = true
The `b` (local) variant holds precendence over the `g` (global) variant.
## Overrides
Say you want to use `// ` to comment lines in C files instead of `/* ... */`.

View File

@ -88,15 +88,25 @@ vim.g.quickcomment_togglecommentlines = function (line_start, line_end)
pe_comment_string = pe_comment_string .. vim.g.quickcomment_escapepercent(comment_string:sub(sub_end + 1))
end
local escaped_string_prefix = ''
if vim.b.quickcomment_whitespaceprefix ~= nil then
if vim.b.quickcomment_whitespaceprefix then
escaped_string_prefix = '%s*'
end
elseif vim.g.quickcomment_whitespaceprefix ~= nil
and vim.g.quickcomment_whitespaceprefix then
escaped_string_prefix = '%s*'
end
-- get lines to comment/uncomment
local lines = vim.api.nvim_buf_get_lines(0, line_start, line_end, false)
for i, line in ipairs(lines) do
if line:find('^' .. escaped_string) == nil then
if line:find('^' .. escaped_string_prefix .. escaped_string) == nil then
-- not commented, comment line
lines[i] = pe_comment_string:format(line)
else
-- commented, uncomment line
local match_cap = line:gmatch(escaped_string)
local match_cap = line:gmatch(escaped_string_prefix .. escaped_string)
local captured = match_cap()
if captured ~= nil then
lines[i] = captured