nvim: add keys to search Go and Odin stdlib declarations

This commit is contained in:
2026-03-11 23:20:37 +01:00
parent 0d65fc0d2a
commit 389dbe5c9d

View File

@@ -20,6 +20,45 @@ if ok then
}
})
fzf.register_ui_select()
local odin_pkg = "~/cloned/Odin"
local function go_root()
return vim.fn.trim(vim.fn.system("go env GOROOT"))
end
local go_root_pkg = nil
local function go_mod()
return vim.fs.joinpath(vim.fn.trim(vim.fn.system("go env GOPATH")), "pkg/mod")
end
local go_mod_pkg = nil
local function go_grep(cwd)
local input = vim.fn.input("Go grep declarations> ")
if #input == 0 then
return
end
local search1 = "^(func|type) .*(?<= )(?=[A-Z])[a-zA-Z0-9_]*" .. input .. "[a-zA-Z0-9_]*( |\\()"
local search2 = "^[\\t]+(?=[A-Z])[a-zA-Z0-9_]*" .. input .. "[a-zA-Z0-9_]* +="
local search = "(" .. search1 .. "|" .. search2 .. ")"
fzf.grep({
cwd = cwd,
cmd = "rg -P -g '*.go'",
search = search,
no_esc = true,
profile = 'ivy',
})
end
local function odin_grep(cwd)
local input = vim.fn.input("Odin grep declarations> ")
if #input == 0 then
return
end
local search = "^[ \\t]*[a-zA-Z0-9_]*" .. input .. "[a-zA-Z0-9_]* +::"
fzf.grep({
cwd = cwd,
cmd = "rg -g '*.odin'",
search = search,
no_esc = true,
profile = 'ivy',
})
end
local set = vim.keymap.set
set("n", "<leader>ba", fzf.buffers, { desc = "FzfLua buffers" })
set("n", "<leader>bh", fzf.history, { desc = "FzfLua history" })
@@ -109,6 +148,68 @@ if ok then
set("n", "grs", fzf.lsp_type_sub, { desc = "Outgouing calls" })
set("n", "grp", fzf.lsp_type_super, { desc = "Outgouing calls" })
set("n", "grt", fzf.lsp_typedefs, { desc = "Goto Type Definition" })
set("n", "<leader>Gf", function()
fzf.files({ cwd = go_root(), cmd = "fd --type f -e go" })
end, { desc = "FzfLua Go files" })
set("n", "<leader>GF", function()
fzf.files({ cwd = go_mod(), cmd = "fd --type f -e go" })
end, { desc = "FzfLua Go mod files" })
set("n", "<leader>Gs", function()
go_grep(go_root())
end, { desc = "FzfLua Go grep declarations" })
set("n", "<leader>GS", function()
go_grep(go_mod())
end, { desc = "FzfLua Go grep mod declarations" })
set("n", "<leader>Gm", function()
go_grep(go_mod_pkg or go_mod())
end, { desc = "FzfLua Go grep mod declarations" })
set("n", "<leader>GM", function()
fzf.zoxide({
cmd = "fd '' --type d " .. vim.fn.shellescape(go_mod()) .. [[ | awk '{print "\t"$1}']],
actions = {
["enter"] = function(a)
go_mod_pkg = vim.fn.trim(a[1])
go_grep(go_mod_pkg)
end
}
})
end, { desc = "FzfLua Go select package and grep package declarations" })
set("n", "<leader>Gp", function()
go_grep(go_root_pkg or go_root())
end, { desc = "FzfLua Go grep package declarations" })
set("n", "<leader>GP", function()
fzf.zoxide({
cmd = "fd '' --type d " .. vim.fn.shellescape(go_root()) .. [[ | awk '{print "\t"$1}']],
actions = {
["enter"] = function(a)
go_root_pkg = vim.fn.trim(a[1])
go_grep(go_root_pkg)
end
}
})
end, { desc = "FzfLua Go select package and grep package declarations" })
set("n", "<leader>of", function()
fzf.files({ cwd = "~/cloned/Odin", cmd = "fd --type f -e odin" })
end, { desc = "FzfLua Odin files" })
set("n", "<leader>os", function()
odin_grep("~/cloned/Odin")
end, { desc = "FzfLua Odin grep declarations" })
set("n", "<leader>op", function()
odin_grep(odin_pkg)
end, { desc = "FzfLua Odin grep package declarations" })
set("n", "<leader>oP", function()
fzf.zoxide({
cmd = [[fd '' --type d ~/cloned/Odin | awk '{print "\t"$1}']],
actions = {
["enter"] = function(a)
odin_pkg = vim.fn.trim(a[1])
odin_grep(odin_pkg)
end
}
})
end, { desc = "FzfLua Odin select package and grep package declarations" })
else
print("plugin fzf-lua missing")
end