diff --git a/README.md b/README.md index 57897cc..1ec8225 100644 --- a/README.md +++ b/README.md @@ -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 ` -- 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 @@ -65,6 +67,8 @@ require('lazy').setup({ ## Usage +Search for text in your project via the command: + ``` :Tf ``` @@ -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() +``` diff --git a/lua/tafuta/config/init.lua b/lua/tafuta/config/init.lua index 6d32fff..8db482b 100644 --- a/lua/tafuta/config/init.lua +++ b/lua/tafuta/config/init.lua @@ -2,7 +2,11 @@ local M = {} M.defaults = { -- The user command to run the search e.g. `:Tf ` - 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 = {} diff --git a/lua/tafuta/globals/init.lua b/lua/tafuta/globals/init.lua index 43d4a18..e4eb604 100644 --- a/lua/tafuta/globals/init.lua +++ b/lua/tafuta/globals/init.lua @@ -1,5 +1,5 @@ local M = {} -M.VERSION = "1.0.0" +M.VERSION = "1.1.0" return M diff --git a/lua/tafuta/init.lua b/lua/tafuta/init.lua index 9461473..d7f5771 100644 --- a/lua/tafuta/init.lua +++ b/lua/tafuta/init.lua @@ -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 @@ -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 @@ -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) @@ -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(