Skip to content

Commit 8100fe5

Browse files
authored
feat(grug-far-nvim)!: add more features and align with recommended spectre.nvim mappings (#1212)
1 parent 408111c commit 8100fe5

File tree

1 file changed

+95
-8
lines changed
  • lua/astrocommunity/search/grug-far-nvim

1 file changed

+95
-8
lines changed

lua/astrocommunity/search/grug-far-nvim/init.lua

Lines changed: 95 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
local default_opts = { instanceName = "main", transient = true }
2+
local function grug_far_open(opts)
3+
local grug_far = require "grug-far"
4+
opts = require("astrocore").extend_tbl(default_opts, opts)
5+
if not grug_far.has_instance(opts.instanceName) then
6+
grug_far.open(opts)
7+
else
8+
grug_far.open_instance(opts.instanceName)
9+
if opts.prefills then grug_far.update_instance_prefills(opts.instanceName, opts.prefills, false) end
10+
end
11+
end
12+
113
---@type LazySpec
214
return {
315
"MagicDuck/grug-far.nvim",
@@ -12,17 +24,51 @@ return {
1224
"AstroNvim/astrocore",
1325
---@param opts AstroCoreOpts
1426
opts = function(_, opts)
15-
if not opts.mappings then opts.mappings = require("astrocore").empty_map_table() end
16-
local maps, prefix = opts.mappings, "<Leader>r"
27+
local maps, prefix = opts.mappings, "<Leader>s"
1728

18-
maps.n[prefix] = {
19-
function() require("grug-far").open { transient = true } end,
20-
desc = require("astroui").get_icon("GrugFar", 1, true) .. "Search and Replace",
29+
maps.n[prefix] = { desc = require("astroui").get_icon("GrugFar", 1, true) .. "Search/Replace" }
30+
maps.n[prefix .. "s"] = {
31+
function() grug_far_open() end,
32+
desc = "Search/Replace workspace",
33+
}
34+
maps.n[prefix .. "e"] = {
35+
function()
36+
local ext = require("astrocore.buffer").is_valid() and vim.fn.expand "%:e" or ""
37+
grug_far_open {
38+
prefills = { filesFilter = ext ~= "" and "*." .. ext or nil },
39+
}
40+
end,
41+
desc = "Search/Replace filetype",
42+
}
43+
maps.n[prefix .. "f"] = {
44+
function()
45+
local filter = require("astrocore.buffer").is_valid() and vim.fn.expand "%" or nil
46+
grug_far_open { prefills = { paths = filter } }
47+
end,
48+
desc = "Search/Replace file",
49+
}
50+
maps.n[prefix .. "w"] = {
51+
function()
52+
local current_word = vim.fn.expand "<cword>"
53+
if current_word ~= "" then
54+
grug_far_open {
55+
startCursorRow = 4,
56+
prefills = { search = vim.fn.expand "<cword>" },
57+
}
58+
else
59+
vim.notify("No word under cursor", vim.log.levels.WARN, { title = "Grug-far" })
60+
end
61+
end,
62+
desc = "Replace current word",
2163
}
22-
2364
maps.x[prefix] = {
24-
function() require("grug-far").open { transient = true, startCursorRow = 4 } end,
25-
desc = require("astroui").get_icon("GrugFar", 1, true) .. "Search and Replace (current word)",
65+
function()
66+
local grug_far = require "grug-far"
67+
local grug_opts = require("astrocore").extend_tbl(default_opts, { startCursorRow = 4 })
68+
if grug_far.has_instance(grug_opts.instanceName) then grug_far.close_instance(grug_opts.instanceName) end
69+
grug_far.with_visual_selection(grug_opts)
70+
end,
71+
desc = "Replace selection",
2672
}
2773
end,
2874
},
@@ -42,6 +88,47 @@ return {
4288
---@type CatppuccinOptions
4389
opts = { integrations = { grug_far = true } },
4490
},
91+
{
92+
"folke/which-key.nvim",
93+
optional = true,
94+
opts = function(_, opts)
95+
if not opts.disable then opts.disable = {} end
96+
opts.disable.ft = require("astrocore").list_insert_unique(opts.disable.ft, { "grug-far" })
97+
end,
98+
},
99+
{
100+
"nvim-neo-tree/neo-tree.nvim",
101+
optional = true,
102+
opts = {
103+
commands = {
104+
grug_far_replace = function(state)
105+
local node = state.tree:get_node()
106+
grug_far_open {
107+
prefills = {
108+
path = node.type == "directory" and node:get_id() or vim.fn.fnamemodify(node:get_id(), ":h"),
109+
},
110+
}
111+
end,
112+
},
113+
window = {
114+
mappings = {
115+
gS = "grug_far_replace",
116+
},
117+
},
118+
},
119+
},
120+
{
121+
"stevearc/oil.nvim",
122+
optional = true,
123+
opts = {
124+
keymaps = {
125+
gS = {
126+
function() grug_far_open { prefills = { path = require("oil").get_current_dir() } } end,
127+
desc = "Search/Replace in directory",
128+
},
129+
},
130+
},
131+
},
45132
},
46133
---@param opts GrugFarOptionsOverride
47134
opts = function(_, opts)

0 commit comments

Comments
 (0)