Skip to content

Commit

Permalink
feat(actions): add action to toggle comment log statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Goose97 committed Dec 8, 2024
1 parent fb4d1df commit 9d8b973
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 91 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ require("timber.actions").clear_log_statements({ global = true })

Be aware of [potential limitations](https://github.com/Goose97/timber.nvim/blob/a2faec8a7525d49a2e033ce54246cd50a4fb9021/doc/timber.nvim.txt#L245-L250).

### Comment log statements

Comment/uncomment all log statements in the current buffer:

```lua
require("timber.actions").toggle_comment_log_statements({ global = false })
```

or from all buffers:

```lua
require("timber.actions").toggle_comment_log_statements({ global = true })
```

### Capture log results

`timber.nvim` can monitor multiple sources and capture the log results. For example, a common use case is to capture the log results from a test runner or from a log file.
Expand Down
14 changes: 14 additions & 0 deletions doc/timber.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Users can invoke actions via the API. Action APIs are defined in the
:h timber.actions.insert_batch_log
:h timber.actions.add_log_targets_to_batch
:h timber.actions.clear_log_statements
:h timber.actions.toggle_comment_log_statements


actions.insert_log({opts}) *timber.actions.insert_log()*
Expand Down Expand Up @@ -257,6 +258,19 @@ actions.clear_log_statements({opts}) *timber.actions.clear_log_statements()*
or just the current one (false).
Default: false


actions.toggle_comment_log_statements({opts}) *timber.actions.toggle_comment_log_statements()*

Comment/uncomment all log statements in the current buffer or all buffers.

Parameters: ~
{opts} (table) options to pass to the action

Options: ~
{global} (boolean) whether to toggle all buffers (true)
or just the current one (false).
Default: false

==============================================================================
3. Watchers *timber.nvim-watchers*

Expand Down
8 changes: 8 additions & 0 deletions lua/timber/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,14 @@ function M.clear_log_statements(opts)
require("timber.actions.clear").clear(opts)
end

---@class Timber.Actions.CommentLogStatementsOptions
---@field global? boolean Whether to comment all buffers, or just the current buffer. Defaults to `false`
---@param opts Timber.Actions.CommentLogStatementsOptions?
function M.toggle_comment_log_statements(opts)
opts = vim.tbl_deep_extend("force", { global = false }, opts or {})
require("timber.actions.comment").toggle_comment(opts)
end

function M.setup()
treesitter.setup()
end
Expand Down
43 changes: 5 additions & 38 deletions lua/timber/actions/clear.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,15 @@ 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 clear_global(log_marker)
vim.cmd(string.format("silent! grep! %s", log_marker))

local qf_list = vim.fn.getqflist()
local processed = {}

-- 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)

-- Delete lines (starting from bottom to preserve line numbers)
for _, item in ipairs(qf_list) do
local bufnr = item.bufnr
local lnum = item.lnum

-- Delete the line
for bufnr, lnum in log_statements.iter_global(log_marker) do
vim.api.nvim_buf_set_lines(bufnr, lnum - 1, lnum, false, {})

-- Mark buffer as modified
if not processed[bufnr] then
processed[bufnr] = true
end
Expand All @@ -40,25 +24,6 @@ local function clear_global(log_marker)
end
end

local function clear_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
-- Escape all non-word characters
if string.find(line, log_marker, 1, true) then
table.insert(lines_to_delete, i)
end
end

-- Delete lines from bottom to top
-- We don't want the line number shifting
for i = #lines_to_delete, 1, -1 do
local line_num = lines_to_delete[i]
vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, false, {})
end
end

---@param opts {global: boolean}
function M.clear(opts)
local log_marker = config.config.log_marker
Expand All @@ -71,7 +36,9 @@ function M.clear(opts)
if opts.global then
clear_global(log_marker)
else
clear_local(log_marker)
for linenr in log_statements.iter_local(log_marker) do
vim.api.nvim_buf_set_lines(0, linenr - 1, linenr, false, {})
end
end
end

Expand Down
49 changes: 49 additions & 0 deletions lua/timber/actions/comment.lua
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
50 changes: 50 additions & 0 deletions lua/timber/actions/log_statements.lua
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
Loading

0 comments on commit 9d8b973

Please sign in to comment.