Skip to content

Commit

Permalink
feat(search): add search feature powered by telescope.nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
Goose97 committed Jan 15, 2025
1 parent ad99ad6 commit eaf3143
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 12 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ https://github.com/user-attachments/assets/6bbcb1ab-45a0-45f3-a03a-1d0780219362

- [Neovim 0.10+](https://github.com/neovim/neovim/releases)
- [Recommended] [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter): to support languages, users need to install appropriate Treesitter parsers. `nvim-treesitter` provides an easy interface to manage them.
- [Optional] [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim): required for log statements search feature.

## Installation

Expand Down Expand Up @@ -322,7 +323,7 @@ Out of the box, `timber.nvim` provides [default templates](https://github.com/Go
<details>
<summary><strong>Clear/comment log statements</strong></summary>

To use this feature, you need to configure `log_marker`. The default is 🪵. This `log_marker` will be search/grep to find the log statement lines. Make sure to include it in your log templates. A convenient way to do it is using the `%log_marker` placeholder:
To use these features, you need to configure `log_marker`. The default is 🪵. This `log_marker` will be search/grep to find the log statement lines. Make sure to include it in your log templates. A convenient way to do it is using the `%log_marker` placeholder:

```lua

Expand Down Expand Up @@ -358,6 +359,16 @@ vim.o.grepprg = "grep --line-number --with-filename -R --exclude-dir=.git" -- Us

</details>

<details>
<summary><strong>Search log statements</strong></summary>

Similar to clear/comment log statements feature, you need to configure `log_marker`. `timber.nvim` uses `telescope.nvim` to search log statements:

```lua
require("timber.actions").search_log_statements()
```
</details>

<details>
<summary><strong>Capture log output</strong></summary>

Expand Down
6 changes: 6 additions & 0 deletions doc/timber.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Users can invoke actions via the API. Action APIs are defined in the
:h timber.actions.add_log_targets_to_batch
:h timber.actions.clear_log_statements
:h timber.actions.toggle_comment_log_statements
:h timber.actions.search_log_statements


actions.insert_log({opts}) *timber.actions.insert_log()*
Expand Down Expand Up @@ -270,6 +271,11 @@ actions.toggle_comment_log_statements({opts}) *timber.actions.toggle_comment_l
or just the current one (false).
Default: false


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

Search log statements in all files using |telescope.nvim|.

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

Expand Down
4 changes: 4 additions & 0 deletions lua/timber/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,10 @@ function M.toggle_comment_log_statements(opts)
require("timber.actions.comment").toggle_comment(opts)
end

function M.search_log_statements()
require("timber.actions.search").search()
end

function M.setup()
treesitter.setup()
end
Expand Down
20 changes: 20 additions & 0 deletions lua/timber/actions/search.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local M = {}

local config = require("timber.config")
local utils = require("timber.utils")

function M.search()
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

require("telescope.builtin").grep_string({
search = log_marker,
prompt_title = "Log Statements (timber.nvim)",
})
end

return M
19 changes: 8 additions & 11 deletions tests/minimal_init.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
local plenary_dir = "./vendor/plenary.nvim"

if vim.fn.isdirectory(plenary_dir) == 0 then
vim.fn.system({ "git", "clone", "https://github.com/nvim-lua/plenary.nvim", plenary_dir })
end

local nvim_treesitter_dir = "./vendor/nvim-treesitter"
if vim.fn.isdirectory(nvim_treesitter_dir) == 0 then
vim.fn.system({ "git", "clone", "https://github.com/nvim-treesitter/nvim-treesitter.git", nvim_treesitter_dir })
local function install_dep(dir, path)
if vim.fn.isdirectory(dir) == 0 then
vim.fn.system({ "git", "clone", path, dir })
end
vim.opt.rtp:append(dir)
end

vim.opt.rtp:append(".")
vim.opt.rtp:append(plenary_dir)
vim.opt.rtp:append(nvim_treesitter_dir)
install_dep("./vendor/plenary.nvim", "https://github.com/nvim-lua/plenary.nvim.git")
install_dep("./vendor/nvim-treesitter", "https://github.com/nvim-treesitter/nvim-treesitter.git")
install_dep("./vendor/telescope.nvim", "https://github.com/nvim-telescope/telescope.nvim.git")

-- Setup grepprg for global clear and comment tests
vim.o.grepprg = "grep --line-number --with-filename -R --exclude-dir=.git"
Expand Down
15 changes: 15 additions & 0 deletions tests/timber/actions/timber_actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1995,3 +1995,18 @@ describe("timber.actions.toggle_comment_log_statements", function()
end)
end)
end)

describe("timber.actions.search_log_statements", function()
it("calls telescope.builtin.grep_string with the log_marker", function()
timber.setup({ log_marker = "foo" })

local telescope = require("telescope.builtin")
local telescope_spy = spy.on(telescope, "grep_string")

actions.search_log_statements()
vim.cmd("close!")

assert.spy(telescope_spy).was_called(1)
assert.spy(telescope_spy).was_called_with({ search = "foo", prompt_title = "Log Statements (timber.nvim)" })
end)
end)

0 comments on commit eaf3143

Please sign in to comment.