From 0c6dab5310f708c8557c7745eda79684a24bda28 Mon Sep 17 00:00:00 2001 From: Ellis Rahhal Date: Fri, 14 Feb 2025 16:54:21 -0800 Subject: [PATCH] refactored keybindings; added search and grep to nvim-tree --- keymaps.nix | 218 ----------------------------------------- options.nix | 2 +- plugins/bufferline.nix | 21 ++++ plugins/cmp.nix | 5 +- plugins/default.nix | 4 +- plugins/diffview.nix | 24 +++++ plugins/nvim-tree.nix | 77 ++++++++++----- plugins/telescope.nix | 169 ++++++++++++++++++++++++++++++-- plugins/treeutils.lua | 46 +++++++++ 9 files changed, 314 insertions(+), 252 deletions(-) create mode 100644 plugins/diffview.nix create mode 100644 plugins/treeutils.lua diff --git a/keymaps.nix b/keymaps.nix index 572efe0..6118d20 100644 --- a/keymaps.nix +++ b/keymaps.nix @@ -2,23 +2,6 @@ { programs.nixvim = { keymaps = [ - # ----------------------------------------------------- - # nvim-tree - # ----------------------------------------------------- - - ## Go to current buffer's file in nvim-tree - { - mode = [ "n" ]; - key = ",n"; - action = ":NvimTreeFindFile"; - } - ## Toggle nvim-tree visibility - { - mode = [ "n" ]; - key = ",m"; - action = ":NvimTreeToggle"; - } - # ----------------------------------------------------- # buffer manipulation # ----------------------------------------------------- @@ -53,207 +36,6 @@ action = ":tabnew split"; options = { noremap = true; }; } - - # ----------------------------------------------------- - # Telescope - # ----------------------------------------------------- - - ## Lists files in your current working directory, respects .gitignore - { - mode = [ "n" ]; - key = "ff"; - action = "Telescope find_files"; - options = { noremap = true; }; - } - ## Finds files by filename - { - mode = [ "n" ]; - key = ""; - action = "Telescope find_files"; - # action = "FzfLua files"; - options = { noremap = true; }; - } - # Search for a string in your current working directory and get results live as you type, respects .gitignore. (Requires ripgrep) - { - mode = [ "n" ]; - key = "fg"; - action = "Telescope live_grep"; - # action = "FzfLua live_grep"; - options = { noremap = true; }; - } - # Search file contents - { - mode = [ "n" ]; - key = ""; - action = "Telescope live_grep"; - # action = "FzfLua live_grep"; - options = { noremap = true; }; - } - # Lists open buffers in current neovim instance - { - mode = [ "n" ]; - key = "db"; - action = "Telescope buffers"; - # action = "FzfLua buffers"; - options = { noremap = true; }; - } - # Lists available help tags and opens a new window with the relevant help info on - { - mode = [ "n" ]; - key = "fh"; - action = "Telescope help_tags"; - # action = "FzfLua helptags"; - options = { noremap = true; }; - } - # Lists manpage entries, opens them in a help window on - { - mode = [ "n" ]; - key = "fm"; - action = "Telescope man_pages"; - # action = "FzfLua manpages"; - options = { noremap = true; }; - } - # Lists previously open files - { - mode = [ "n" ]; - key = "fp"; - action = "Telescope oldfiles"; - # action = "FzfLua oldfiles"; - options = { noremap = true; }; - } - # Lists previously open files, Maps to ctrl-/ - { - mode = [ "n" ]; - key = ""; - action = "Telescope oldfiles"; - # action = "FzfLua oldfiles"; - options = { noremap = true; }; - } - # Lists spelling suggestions for the current word under the cursor, replaces word with selected suggestion on - { - mode = [ "n" ]; - key = "fs"; - action = "Telescope spell_suggest"; - # action = "FzfLua spell_suggest"; - options = { noremap = true; }; - } - # Lists LSP references for iword under the cursor - { - mode = [ "n" ]; - key = "fr"; - action = "Telescope lsp_references"; - # action = "FzfLua lsp_references"; - options = { noremap = true; }; - } - # Lists LSP incoming calls for word under the cursor - { - mode = [ "n" ]; - key = "fi"; - action = "Telescope lsp_incoming_calls"; - # action = "FzfLua lsp_incoming_calls"; - options = { noremap = true; }; - } - # Lists LSP outgoing calls for word under the cursor - { - mode = [ "n" ]; - key = "fo"; - action = "Telescope lsp_outgoing_calls"; - # action = "FzfLua lsp_outgoing_calls"; - options = { noremap = true; }; - } - # Dynamically Lists LSP for all workspace symbols - { - mode = [ "n" ]; - key = "fw"; - action = "Telescope lsp_dynamic_workspace_symbols"; - # action = "FzfLua lsp_workspace_symbols"; - options = { noremap = true; }; - } - # Goto the definition of the word under the cursor, if there's only one, otherwise show all options in Telescope - { - mode = [ "n" ]; - key = "fd"; - action = "Telescope lsp_definitions"; - options = { noremap = true; }; - } - # Got to previous error - { - mode = [ "n" ]; - key = "[d"; - action = "lua vim.diagnostic.goto_prev()"; - options = { noremap = true; silent = true; }; - } - { - mode = [ "n" ]; - key = ",k"; - action = "lua vim.diagnostic.goto_prev()"; - options = { noremap = true; silent = true; }; - } - # Got to next error - { - mode = [ "n" ]; - key = "]d"; - action = "lua vim.diagnostic.goto_next()"; - options = { noremap = true; silent = true; }; - } - { - mode = [ "n" ]; - key = ",j"; - action = "lua vim.diagnostic.goto_next()"; - options = { noremap = true; silent = true; }; - } - - ## Other Telescope options: - ## git_files search only files in git, respects .gitignore - ## oldfiles previously opened files - ## command_history - ## search_history - ## man_pages - ## resume lists the results including multi-selections of the previous - ## picker - - # ----------------------------------------------------- - # Diff - # ----------------------------------------------------- - - { - mode = [ "n" ]; - key = ",d"; - ## @TODO: This doesn't work - action = '' - function() - if next(require('diffview.lib').views) == nil then - vim.cmd('DiffviewOpen origin') - else - vim.cmd('DiffviewClose') - end - end - ''; - options = { noremap = true; }; - } - - # ----------------------------------------------------- - # Bufferline - # ----------------------------------------------------- - - { - mode = [ "n" ]; - key = ""; - action = ":BufferLineCyclePrev"; - options = { noremap = true; silent = true; }; - } - { - mode = [ "n" ]; - key = ""; - action = ":BufferLineCycleNex"; - options = { noremap = true; silent = true; }; - } - { - mode = [ "n" ]; - key = ""; - action = ":bdelete!"; - options = { noremap = true; silent = true; }; - } ]; }; } diff --git a/options.nix b/options.nix index ae36237..9f09915 100644 --- a/options.nix +++ b/options.nix @@ -34,7 +34,7 @@ cursorline = true; # Highlight line cursor sits on ## @TODO Review these - completeopt = ""; # Completion options + completeopt = "menuone,noselect"; hidden = true; # Enable modified buffers in background inccommand = "nosplit"; # Show effects of a command incrementally joinspaces = false; # No double spaces with join after a dot diff --git a/plugins/bufferline.nix b/plugins/bufferline.nix index 8ad51ef..abdf0f2 100644 --- a/plugins/bufferline.nix +++ b/plugins/bufferline.nix @@ -1,6 +1,27 @@ { ... }: { programs.nixvim = { + keymaps = [ + { + mode = [ "n" ]; + key = ""; + action = ":BufferLineCyclePrev"; + options = { noremap = true; silent = true; }; + } + { + mode = [ "n" ]; + key = ""; + action = ":BufferLineCycleNex"; + options = { noremap = true; silent = true; }; + } + { + mode = [ "n" ]; + key = ""; + action = ":bdelete!"; + options = { noremap = true; silent = true; }; + } + ]; + plugins.bufferline = { enable = true; # extraOptions = { diff --git a/plugins/cmp.nix b/plugins/cmp.nix index e20bc14..c33ef46 100644 --- a/plugins/cmp.nix +++ b/plugins/cmp.nix @@ -81,8 +81,7 @@ }; completion = { - # completeopt = "menuone,noselect"; - completeopt = ""; + completeopt = "menuone,noselect"; }; autoEnableSources = true; @@ -117,7 +116,7 @@ "" = "cmp.mapping.scroll_docs(-4)"; "" = "cmp.mapping.scroll_docs(4)"; "" = "cmp.mapping.complete()"; - "" = "cmp.mapping.confirm({ select = true })"; + "" = "cmp.mapping.confirm({ select = false })"; # "" = '' # cmp.mapping(function(fallback) # -- local context = require("cmp.config.context") diff --git a/plugins/default.nix b/plugins/default.nix index fe9337a..67a8483 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -10,6 +10,7 @@ ./cmp.nix ./codecompanion.nix ./copilot.nix + ./diffview.nix ./fzf-lua.nix ./lazydev.nix ./lsp.nix @@ -21,11 +22,10 @@ programs.nixvim.plugins = { comment.enable = true; - diffview.enable = true; fugitive.enable = true; gitsigns.enable = true; indent-blankline.enable = true; - lightline.enable = true; + lightline.enable = false; lsp-signature.enable = true; # config = '' # lua << EOF diff --git a/plugins/diffview.nix b/plugins/diffview.nix new file mode 100644 index 0000000..89e79f3 --- /dev/null +++ b/plugins/diffview.nix @@ -0,0 +1,24 @@ +{ ... }: +{ + programs.nixvim = { + keymaps = [ + { + mode = [ "n" ]; + key = ",d"; + ## @TODO: This doesn't work + action = '' + function() + if next(require('diffview.lib').views) == nil then + vim.cmd('DiffviewOpen origin') + else + vim.cmd('DiffviewClose') + end + end + ''; + options = { noremap = true; }; + } + ]; + + plugins.diffview.enable = true; + }; +} diff --git a/plugins/nvim-tree.nix b/plugins/nvim-tree.nix index c5c4106..169d134 100644 --- a/plugins/nvim-tree.nix +++ b/plugins/nvim-tree.nix @@ -1,33 +1,66 @@ { ... }: { programs.nixvim = { + + extraFiles = { + "lua/treeutils.lua" = { + source = ./treeutils.lua; + }; + }; + + keymaps = [ + ## Go to current buffer's file in nvim-tree + { + mode = [ "n" ]; + key = ",n"; + action = ":NvimTreeFindFile"; + } + ## Toggle nvim-tree visibility + { + mode = [ "n" ]; + key = ",m"; + action = ":NvimTreeToggle"; + } + ## Search current path + { + mode = [ "n" ]; + key = ""; + action = "lua require('treeutils').launch_find_files()"; + options = { noremap = true; }; + } + { + mode = [ "n" ]; + key = ""; + action = "lua require('treeutils').launch_live_grep()"; + options = { noremap = true; }; + } + ]; + plugins.nvim-tree = { enable = true; - extraOptions = { - actions = { - remove_file = { - close_window = false; - }; + actions = { + removeFile = { + closeWindow = false; }; - ## Keep tree open if already open when opening a tab - tab = { - sync = { - open = true; - close = true; - }; - }; - view = { - width = 30; - }; - renderer = { - group_empty = true; - }; - git = { - enable = true; - ignore = false; - timeout = 500; + }; + ## Keep tree open if already open when opening a tab + tab = { + sync = { + open = true; + close = true; }; }; + view = { + width = 30; + }; + renderer = { + groupEmpty = true; + }; + git = { + enable = true; + ignore = false; + timeout = 500; + }; }; }; } diff --git a/plugins/telescope.nix b/plugins/telescope.nix index 0add60f..d759e23 100644 --- a/plugins/telescope.nix +++ b/plugins/telescope.nix @@ -1,6 +1,163 @@ { ... }: { programs.nixvim = { + keymaps = [ + ## Lists files in your current working directory, respects .gitignore + { + mode = [ "n" ]; + key = "ff"; + action = "Telescope find_files"; + options = { noremap = true; }; + } + ## Finds files by filename + { + mode = [ "n" ]; + key = ""; + action = "Telescope find_files"; + # action = "FzfLua files"; + options = { noremap = true; }; + } + # Search for a string in your current working directory and get results live as you type, respects .gitignore. (Requires ripgrep) + { + mode = [ "n" ]; + key = "fg"; + action = "Telescope live_grep"; + # action = "FzfLua live_grep"; + options = { noremap = true; }; + } + # Search file contents + { + mode = [ "n" ]; + key = ""; + action = "Telescope live_grep"; + # action = "FzfLua live_grep"; + options = { noremap = true; }; + } + # Lists open buffers in current neovim instance + { + mode = [ "n" ]; + key = "db"; + action = "Telescope buffers"; + # action = "FzfLua buffers"; + options = { noremap = true; }; + } + # Lists available help tags and opens a new window with the relevant help info on + { + mode = [ "n" ]; + key = "fh"; + action = "Telescope help_tags"; + # action = "FzfLua helptags"; + options = { noremap = true; }; + } + # Lists manpage entries, opens them in a help window on + { + mode = [ "n" ]; + key = "fm"; + action = "Telescope man_pages"; + # action = "FzfLua manpages"; + options = { noremap = true; }; + } + # Lists previously open files + { + mode = [ "n" ]; + key = "fp"; + action = "Telescope oldfiles"; + # action = "FzfLua oldfiles"; + options = { noremap = true; }; + } + # Lists previously open files, Maps to ctrl-/ + { + mode = [ "n" ]; + key = ""; + action = "Telescope oldfiles"; + # action = "FzfLua oldfiles"; + options = { noremap = true; }; + } + ## Binding currently used by nvim-tree + ## Lists spelling suggestions for the current word under the cursor, replaces word with selected suggestion on + # { + # mode = [ "n" ]; + # key = "fs"; + # action = "Telescope spell_suggest"; + # # action = "FzfLua spell_suggest"; + # options = { noremap = true; }; + # } + # Lists LSP references for iword under the cursor + { + mode = [ "n" ]; + key = "fr"; + action = "Telescope lsp_references"; + # action = "FzfLua lsp_references"; + options = { noremap = true; }; + } + # Lists LSP incoming calls for word under the cursor + { + mode = [ "n" ]; + key = "fi"; + action = "Telescope lsp_incoming_calls"; + # action = "FzfLua lsp_incoming_calls"; + options = { noremap = true; }; + } + # Lists LSP outgoing calls for word under the cursor + { + mode = [ "n" ]; + key = "fo"; + action = "Telescope lsp_outgoing_calls"; + # action = "FzfLua lsp_outgoing_calls"; + options = { noremap = true; }; + } + # Dynamically Lists LSP for all workspace symbols + { + mode = [ "n" ]; + key = "fw"; + action = "Telescope lsp_dynamic_workspace_symbols"; + # action = "FzfLua lsp_workspace_symbols"; + options = { noremap = true; }; + } + # Goto the definition of the word under the cursor, if there's only one, otherwise show all options in Telescope + { + mode = [ "n" ]; + key = "fd"; + action = "Telescope lsp_definitions"; + options = { noremap = true; }; + } + # Got to previous error + { + mode = [ "n" ]; + key = "[d"; + action = "lua vim.diagnostic.goto_prev()"; + options = { noremap = true; silent = true; }; + } + { + mode = [ "n" ]; + key = ",k"; + action = "lua vim.diagnostic.goto_prev()"; + options = { noremap = true; silent = true; }; + } + # Got to next error + { + mode = [ "n" ]; + key = "]d"; + action = "lua vim.diagnostic.goto_next()"; + options = { noremap = true; silent = true; }; + } + { + mode = [ "n" ]; + key = ",j"; + action = "lua vim.diagnostic.goto_next()"; + options = { noremap = true; silent = true; }; + } + + ## Other Telescope options: + ## git_files search only files in git, respects .gitignore + ## oldfiles previously opened files + ## command_history + ## search_history + ## man_pages + ## resume lists the results including multi-selections of the previous + ## picker + ]; + plugins.telescope = { enable = true; extensions.ui-select.enable = true; @@ -16,14 +173,14 @@ "" = { __raw = "require('telescope.actions').to_fuzzy_refine"; }; - # "" = { - # __raw = "require('trouble.sources.telescope').open"; - # }; + "" = { + __raw = "require('trouble.sources.telescope').open"; + }; }; n = { - # "" = { - # __raw = "require('trouble.sources.telescope').open"; - # }; + "" = { + __raw = "require('trouble.sources.telescope').open"; + }; }; }; }; diff --git a/plugins/treeutils.lua b/plugins/treeutils.lua new file mode 100644 index 0000000..89b4569 --- /dev/null +++ b/plugins/treeutils.lua @@ -0,0 +1,46 @@ +local api = require("nvim-tree.api") +local openfile = require'nvim-tree.actions.node.open-file' +local actions = require'telescope.actions' +local action_state = require'telescope.actions.state' +local M = {} + +local view_selection = function(prompt_bufnr, map) + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + local filename = selection.filename + if (filename == nil) then + filename = selection[1] + end + openfile.fn('preview', filename) + end) + return true +end + +function M.launch_live_grep(opts) + return M.launch_telescope("live_grep", opts) +end + +function M.launch_find_files(opts) + return M.launch_telescope("find_files", opts) +end + +function M.launch_telescope(func_name, opts) + local telescope_status_ok, _ = pcall(require, "telescope") + if not telescope_status_ok then + return + end + local node = api.tree.get_node_under_cursor() + local is_folder = node.fs_stat and node.fs_stat.type == 'directory' or false + local basedir = is_folder and node.absolute_path or vim.fn.fnamemodify(node.absolute_path, ":h") + if (node.name == '..' and TreeExplorer ~= nil) then + basedir = TreeExplorer.cwd + end + opts = opts or {} + opts.cwd = basedir + opts.search_dirs = { basedir } + opts.attach_mappings = view_selection + return require("telescope.builtin")[func_name](opts) +end + +return M