From ddeffdce4f96c3c32fa11702c086217098f44e40 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sat, 19 Nov 2022 18:15:19 +0900 Subject: [PATCH] Impl uncommenting with leading whitespace --- README.md | 20 ++++++++++++++++++++ plugin/quickcomment.lua | 14 ++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0a887de..13f0da5 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,26 @@ Or in init.vim: nmap q :lua vim.g.quickcomment_togglecommentline() vmap q :luado vim.g.quickcomment_togglecommentline(linenr) +## 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 `/* ... */`. diff --git a/plugin/quickcomment.lua b/plugin/quickcomment.lua index fea1ffb..24cddf7 100644 --- a/plugin/quickcomment.lua +++ b/plugin/quickcomment.lua @@ -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