generated from ellisonleao/nvim-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(actions): add action to toggle comment log statements
- Loading branch information
Showing
8 changed files
with
363 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
local M = {} | ||
|
||
local config = require("timber.config") | ||
local utils = require("timber.utils") | ||
local log_statements = require("timber.actions.log_statements") | ||
|
||
-- Using grep to search all files globally | ||
local function toggle_comment_global(log_marker) | ||
local processed = {} | ||
|
||
for bufnr, lnum in log_statements.iter_global(log_marker) do | ||
vim.api.nvim_buf_call(bufnr, function() | ||
vim.api.nvim_win_set_cursor(0, { lnum, 0 }) | ||
vim.cmd("normal gcc") | ||
end) | ||
|
||
if not processed[bufnr] then | ||
processed[bufnr] = true | ||
end | ||
end | ||
|
||
-- Save all modified buffers | ||
for bufnr, _ in pairs(processed) do | ||
vim.api.nvim_buf_call(bufnr, function() | ||
vim.cmd("silent! write") | ||
end) | ||
end | ||
end | ||
|
||
---@param opts {global: boolean} | ||
function M.toggle_comment(opts) | ||
local log_marker = config.config.log_marker | ||
|
||
if not log_marker or log_marker == "" then | ||
utils.notify("config.log_marker is not configured", "warn") | ||
return | ||
end | ||
|
||
if opts.global then | ||
toggle_comment_global(log_marker) | ||
else | ||
for linenr in log_statements.iter_local(log_marker) do | ||
vim.api.nvim_win_set_cursor(0, { linenr, 0 }) | ||
vim.cmd("normal gcc") | ||
end | ||
end | ||
end | ||
|
||
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
local M = {} | ||
|
||
---@param log_marker string | ||
function M.iter_global(log_marker) | ||
vim.cmd(string.format("silent! grep! %s", log_marker)) | ||
|
||
local qf_list = vim.fn.getqflist() | ||
|
||
-- Sort quickfix entries by buffer and line number (in reverse) | ||
table.sort(qf_list, function(a, b) | ||
if a.bufnr == b.bufnr then | ||
return a.lnum > b.lnum | ||
end | ||
return a.bufnr > b.bufnr | ||
end) | ||
|
||
-- Iterator function | ||
local i = 0 | ||
return function() | ||
i = i + 1 | ||
local item = qf_list[i] | ||
if item then | ||
return item.bufnr, item.lnum | ||
end | ||
end | ||
end | ||
|
||
---@param log_marker string | ||
function M.iter_local(log_marker) | ||
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false) | ||
local lines_to_delete = {} | ||
|
||
for i, line in ipairs(lines) do | ||
if string.find(line, log_marker, 1, true) then | ||
table.insert(lines_to_delete, i) | ||
end | ||
end | ||
|
||
-- Iterator function | ||
local i = #lines_to_delete | ||
return function() | ||
if i > 0 then | ||
local item = lines_to_delete[i] | ||
i = i - 1 | ||
return item | ||
end | ||
end | ||
end | ||
|
||
return M |
Oops, something went wrong.