nvim-quickcomment/plugin/quickcomment.lua

82 lines
3.1 KiB
Lua
Raw Normal View History

2022-11-03 04:10:39 +00:00
-- MIT License
--
-- Copyright (c) 2022 Stephen Seo
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
2022-11-03 02:29:06 +00:00
if vim.g.loaded_quickcomment then
return
end
vim.g.loaded_quickcomment = true
2022-11-03 03:54:02 +00:00
vim.g.quickcomment_escapestring = function(string_to_escape)
2022-11-03 02:29:06 +00:00
local escaped_string = ''
2022-11-03 03:54:02 +00:00
string_to_escape:gsub(".", function(c)
2022-11-03 02:29:06 +00:00
if c:find('[-?+*%]%[.%%()$^]') == nil then
escaped_string = escaped_string .. c
else
escaped_string = escaped_string .. '%' .. c
end
end)
return escaped_string
end
2022-11-03 03:54:02 +00:00
vim.g.quickcomment_togglecommentlines = function (line_start, line_end)
2022-11-03 02:41:27 +00:00
local comment_string = vim.api.nvim_buf_get_option(0, 'commentstring')
2022-11-03 03:54:02 +00:00
if comment_string == nil then
print('QuickComment: ERROR: Unable to get comment string, "commentstring" is not defined!')
2022-11-03 04:09:27 +00:00
return
2022-11-03 02:29:06 +00:00
end
2022-11-03 03:54:02 +00:00
local sub_find, sub_end = comment_string:find('%%s')
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))
2022-11-03 02:29:06 +00:00
end
2022-11-03 03:54:02 +00:00
escaped_string = escaped_string .. '(.*)'
if sub_end < comment_string:len() then
escaped_string = escaped_string .. vim.g.quickcomment_escapestring(comment_string:sub(sub_end + 1))
2022-11-03 02:29:06 +00:00
end
local lines = vim.api.nvim_buf_get_lines(0, line_start, line_end, false)
for i, line in ipairs(lines) do
2022-11-03 03:54:02 +00:00
if line:find(escaped_string) == nil then
lines[i] = comment_string:format(line)
2022-11-03 02:29:06 +00:00
else
2022-11-03 03:54:02 +00:00
local match_cap = line:gmatch(escaped_string)
local captured = match_cap()
if captured ~= nil then
lines[i] = captured
else
print('QuickComment: ERROR: Failed to uncomment!')
2022-11-03 04:09:27 +00:00
return
2022-11-03 03:54:02 +00:00
end
2022-11-03 02:29:06 +00:00
end
end
vim.api.nvim_buf_set_lines(0, line_start, line_end, false, lines)
end
2022-11-03 03:54:02 +00:00
vim.g.quickcomment_togglecommentline = function (linen)
2022-11-03 02:29:06 +00:00
if linen == nil then
linen = vim.api.nvim_win_get_cursor(0)[1]
end
2022-11-03 03:54:02 +00:00
vim.g.quickcomment_togglecommentlines(linen - 1, linen)
2022-11-03 02:29:06 +00:00
end