Skip to content

Commit

Permalink
feat: add .cursor()
Browse files Browse the repository at this point in the history
  • Loading branch information
gorillamoe committed Aug 2, 2024
1 parent 421ed79 commit ddc07e5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ require('lazy').setup({
{
'mistweaverco/tafuta.nvim',
-- Make sure this matches the command you want to use and the command pass to setup
-- as user_command_prompt and user_command_cursor
-- e.g. if you want to use `:Rg` then the cmd should be `Rg`
-- If you don't want to use a command, you can omit this option completely
cmd = { "Tf" },
cmd = { "Tf", "Tfc" },
config = function()
-- Setup is required, even if you don't pass any options
require('tafuta').setup({
-- The user command to run the search e.g. `:Tf <query>`
-- Default: "Tf", but it can be anything you want.
-- If you don't want a command, you can set it to `nil`
user_command = "Tf",
user_command_prompt = "Tf",
user_command_cursor = "Tfc",
-- rg options, a lua table of options to pass to rg,
-- e.g. { "--hidden", "--no-ignore" }
-- Default: nil
Expand All @@ -65,6 +67,8 @@ require('lazy').setup({

## Usage

Search for text in your project via the command:

```
:Tf <search-term>
```
Expand All @@ -77,3 +81,15 @@ require('tafuta').run("[search term here, can be regex too]")

If you omit the search term,
it will prompt you for one (via `input()`).

You can also search for the word under the cursor via:

```
:Tfc
```

or via calling a lua function:

```lua
require('tafuta').cursor()
```
6 changes: 5 additions & 1 deletion lua/tafuta/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ local M = {}

M.defaults = {
-- The user command to run the search e.g. `:Tf <query>`
user_command = "Tf",
user_command_prompt = "Tf",
-- The user command to search for the word under the cursor e.g. `:Tfc`
user_command_cursor = "Tfc",
-- The ripgrep options to use when searching, e.g. `{"--hidden", "--no-ignore-vcs"}`
rg_options = nil,
}

M.options = {}
Expand Down
2 changes: 1 addition & 1 deletion lua/tafuta/globals/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local M = {}

M.VERSION = "1.0.0"
M.VERSION = "1.1.0"

return M
38 changes: 35 additions & 3 deletions lua/tafuta/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ local async_run = vim.schedule_wrap(function(res)
end
end)

local function get_word_under_cursor()
local _, col = unpack(vim.api.nvim_win_get_cursor(0))
local line = vim.api.nvim_get_current_line()
if col == #line then
col = col - 1
end
local start_col, end_col = line:find("%w+", col + 1)
if start_col and end_col then
return line:sub(start_col, end_col)
else
return nil
end
end

local search = function(search_query)
local search_command = { "rg", "--vimgrep" }
local rg_options = CONFIG.get().rg_options
Expand All @@ -38,9 +52,10 @@ end

M.setup = function(config)
CONFIG.setup(config)
local user_command = CONFIG.get().user_command
if user_command ~= nil then
vim.api.nvim_create_user_command(user_command, function(a)
local user_command_prompt = CONFIG.get().user_command_prompt
local user_command_cursor = CONFIG.get().user_command_cursor
if user_command_prompt ~= nil then
vim.api.nvim_create_user_command(user_command_prompt, function(a)
if a.args == "" then
vim.notify("❌ No search query provided", vim.log.levels.INFO, { title = "Tafuta" })
return
Expand All @@ -51,6 +66,14 @@ M.setup = function(config)
desc = "Tf, blazingly fast ⚑ search πŸ” using ripgrep πŸ¦€",
})
end
if user_command_cursor ~= nil then
vim.api.nvim_create_user_command(user_command_cursor, function()
M.cursor()
end, {
nargs = 0,
desc = "Search for the word under the cursor",
})
end
end

M.run = function(search_query)
Expand All @@ -64,6 +87,15 @@ M.run = function(search_query)
search(search_query)
end

M.cursor = function()
local word = get_word_under_cursor()
if word == nil then
vim.notify("❌ no word under cursor", vim.log.levels.INFO, { title = "Tafuta" })
return
end
search(word)
end

M.version = function()
local neovim_version = vim.fn.execute("version")
vim.notify(
Expand Down

0 comments on commit ddc07e5

Please sign in to comment.