From c474210010bc69d90e39333b7dbbaa9e1c7caed7 Mon Sep 17 00:00:00 2001 From: punkfairie Date: Sat, 4 Jan 2025 20:54:00 -0800 Subject: [PATCH] feat: project.nvim --- nix/neovim-overlay.nix | 1 + nvim/lua/plugins/editor/fzf-lua.lua | 87 ++++++++++++++++++++++++++ nvim/lua/plugins/util/init.lua | 1 + nvim/lua/plugins/util/project-nvim.lua | 19 ++++++ 4 files changed, 108 insertions(+) create mode 100644 nvim/lua/plugins/util/project-nvim.lua diff --git a/nix/neovim-overlay.nix b/nix/neovim-overlay.nix index b25248d..ddfcc76 100644 --- a/nix/neovim-overlay.nix +++ b/nix/neovim-overlay.nix @@ -35,6 +35,7 @@ with final.pkgs.lib; let nui-nvim persistence-nvim mini-hipatterns + project-nvim # Colorscheme rose-pine diff --git a/nvim/lua/plugins/editor/fzf-lua.lua b/nvim/lua/plugins/editor/fzf-lua.lua index 34dd56a..0d9ea72 100644 --- a/nvim/lua/plugins/editor/fzf-lua.lua +++ b/nvim/lua/plugins/editor/fzf-lua.lua @@ -8,6 +8,92 @@ local function pick(picker, opts) end end +local projectPick = nil + +projectPick = function() + local has_project, _ = pcall(require, 'project_nvim') + if not has_project then + return + end + + local fzf_lua = require('fzf-lua') + local project = require('project_nvim.project') + local history = require('project_nvim.utils.history') + local results = history.get_recent_projects() + local utils = require('fzf-lua.utils') + + local function hl_validate(hl) + return not utils.is_hl_cleared(hl) and hl or nil + end + + local function ansi_from_hl(hl, s) + return utils.ansi_from_hl(hl_validate(hl), s) + end + + local opts = { + fzf_opts = { + ['--header'] = string.format( + ':: <%s> to %s | <%s> to %s | <%s> to %s | <%s> to %s | <%s> to %s', + ansi_from_hl('FzfLuaHeaderBind', 'ctrl-t'), + ansi_from_hl('FzfLuaHeaderText', 'tabedit'), + ansi_from_hl('FzfLuaHeaderBind', 'ctrl-s'), + ansi_from_hl('FzfLuaHeaderText', 'live_grep'), + ansi_from_hl('FzfLuaHeaderBind', 'ctrl-r'), + ansi_from_hl('FzfLuaHeaderText', 'oldfiles'), + ansi_from_hl('FzfLuaHeaderBind', 'ctrl-w'), + ansi_from_hl('FzfLuaHeaderText', 'change_dir'), + ansi_from_hl('FzfLuaHeaderBind', 'ctrl-d'), + ansi_from_hl('FzfLuaHeaderText', 'delete') + ), + }, + fzf_colors = true, + actions = { + ['default'] = { + function(selected) + fzf_lua.files({ cwd = selected[1] }) + end, + }, + ['ctrl-t'] = { + function(selected) + vim.cmd('tabedit') + fzf_lua.files({ cwd = selected[1] }) + end, + }, + ['ctrl-s'] = { + function(selected) + fzf_lua.live_grep({ cwd = selected[1] }) + end, + }, + ['ctrl-r'] = { + function(selected) + fzf_lua.oldfiles({ cwd = selected[1] }) + end, + }, + ['ctrl-w'] = { + function(selected) + local path = selected[1] + local ok = project.set_pwd(path) + if ok then + vim.api.nvim_win_close(0, false) + vim.notify('Change project dir to ' .. path, 'info') + end + end, + }, + ['ctrl-d'] = function(selected) + local path = selected[1] + local choice = + vim.fn.confirm("Delete '" .. path .. "' project? ", '&Yes\n&No') + if choice == 1 then + history.delete_project({ value = path }) + end + projectPick() + end, + }, + } + + fzf_lua.fzf_exec(results, opts) +end + return { 'fzf-lua', cmd = 'FzfLua', @@ -25,6 +111,7 @@ return { desc = 'find files (cwd)', }, { 'fg', pick('git_files'), desc = 'find files (git)' }, + { 'fp', projectPick, desc = 'projects' }, { 'fr', pick('oldfiles'), desc = 'recent' }, { 'fR', diff --git a/nvim/lua/plugins/util/init.lua b/nvim/lua/plugins/util/init.lua index 70eff0f..29da99a 100644 --- a/nvim/lua/plugins/util/init.lua +++ b/nvim/lua/plugins/util/init.lua @@ -5,4 +5,5 @@ return { req('nui-nvim'), req('persistance-nvim'), req('plenary'), + req('project-nvim'), } diff --git a/nvim/lua/plugins/util/project-nvim.lua b/nvim/lua/plugins/util/project-nvim.lua new file mode 100644 index 0000000..4e42c80 --- /dev/null +++ b/nvim/lua/plugins/util/project-nvim.lua @@ -0,0 +1,19 @@ +return { + 'project.nvim', + event = { 'DeferredUIEnter' }, + after = function() + require('project_nvim').setup({ + manual_mode = true, + }) + + local history = require('project_nvim.utils.history') + history.delete_project = function(project) + for k, v in pairs(history.recent_projects) do + if v == project.value then + history.recent_projects[k] = nil + return + end + end + end + end, +}