From 89b60342d71ca3badf6e0cf2bd411d103480bff4 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 3 Nov 2022 15:17:15 +0900 Subject: [PATCH] Impl overrides for alternate commenting syntax --- README.md | 16 ++++++++++++++++ plugin/quickcomment.lua | 24 +++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 69f2e31..b38513f 100644 --- a/README.md +++ b/README.md @@ -20,3 +20,19 @@ Or in init.vim: nmap q :lua vim.g.quickcomment_togglecommentline() vmap q :luado vim.g.quickcomment_togglecommentline(linenr) + +## Overrides + +Say you want to use `// ` to comment lines in C files instead of `/* ... */`. +Set an override string like the following: + + let b:quickcomment_commentstring_override = '// %s' + +This will override how the comment is set for the current buffer. +It can also be set globally: + + let g:quickcomment_commentstring_override = '/* a comment: %s */' + +Note that `b:quickcomment_...` has precedence over `g:quickcomment_...`. Also +note that `%s` must be in the override `commentstring` which denotes where the +line content is relative to the comment symbols. diff --git a/plugin/quickcomment.lua b/plugin/quickcomment.lua index 2c6818f..b112e1b 100644 --- a/plugin/quickcomment.lua +++ b/plugin/quickcomment.lua @@ -40,12 +40,27 @@ vim.g.quickcomment_escapestring = function(string_to_escape) end vim.g.quickcomment_togglecommentlines = function (line_start, line_end) - local comment_string = vim.api.nvim_buf_get_option(0, 'commentstring') + -- get comment_string + local comment_string = vim.b.quickcomment_commentstring_override if comment_string == nil then - print('QuickComment: ERROR: Unable to get comment string, "commentstring" is not defined!') + comment_string = vim.g.quickcomment_commentstring_override + if comment_string == nil then + comment_string = vim.api.nvim_buf_get_option(0, 'commentstring') + if comment_string == nil then + print('QuickComment: ERROR: Unable to get comment string') + return + end + end + end + + -- validate comment_string + local sub_find, sub_end = comment_string:find('%%s') + if sub_find == nil then + print('QuickComment: ERROR: comment_string doesn\'t have "%s"!') return end - local sub_find, sub_end = comment_string:find('%%s') + + -- get escaped_string local escaped_string = '' if sub_find ~= nil and sub_find > 1 then escaped_string = vim.g.quickcomment_escapestring(comment_string:sub(1, sub_find - 1)) @@ -55,11 +70,14 @@ vim.g.quickcomment_togglecommentlines = function (line_start, line_end) escaped_string = escaped_string .. vim.g.quickcomment_escapestring(comment_string:sub(sub_end + 1)) 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 + -- not commented, comment line lines[i] = comment_string:format(line) else + -- commented, uncomment line local match_cap = line:gmatch(escaped_string) local captured = match_cap() if captured ~= nil then