From c5d1b0ac8f2f03a4b4aa566b36f123b673b7af54 Mon Sep 17 00:00:00 2001 From: Nikhil Date: Sat, 23 Aug 2025 22:06:02 +0530 Subject: [PATCH] removed dupication and refactored my keymaps --- lua/core/options.lua | 2 +- lua/plugins/editor.lua | 119 +++++++++++++++++++++++++++++++++++++---- lua/plugins/git.lua | 79 ++++++++++++++------------- lua/plugins/lsp.lua | 95 -------------------------------- lua/plugins/tools.lua | 71 +++++++++++++++++++++--- 5 files changed, 217 insertions(+), 149 deletions(-) diff --git a/lua/core/options.lua b/lua/core/options.lua index abff30d9..fc1289f6 100644 --- a/lua/core/options.lua +++ b/lua/core/options.lua @@ -1,5 +1,5 @@ -- Set to true if you have a Nerd Font installed and selected in the terminal -vim.g.have_nerd_font = false +vim.g.have_nerd_font = true -- [[ Setting options ]] -- See `:help vim.opt` diff --git a/lua/plugins/editor.lua b/lua/plugins/editor.lua index 5f7cf0bd..32c84885 100644 --- a/lua/plugins/editor.lua +++ b/lua/plugins/editor.lua @@ -1,15 +1,19 @@ --- [plugins/editing.lua] +-- [plugins/editor.lua] return { - - - { -- Autoformat + -- -- Autoformat: `conform.nvim` + -- This plugin provides a non-LSP based way to format your code. + { 'stevearc/conform.nvim', + -- Only load this plugin right before a buffer is written. event = { 'BufWritePre' }, + -- Add the `ConformInfo` command for debugging. cmd = { 'ConformInfo' }, + -- Set up keybinds for manual formatting. keys = { { + -- The leader key followed by 'f' to format the current buffer. 'f', function() require('conform').format { async = true, lsp_format = 'fallback' } @@ -19,18 +23,115 @@ return { }, }, opts = { + -- Disable notifications on formatting errors to avoid pop-ups. notify_on_error = false, + -- This function runs before saving a file to check if it should be formatted. format_on_save = function(bufnr) + -- A list of filetypes where auto-formatting on save is disabled. local disable_filetypes = { c = true, cpp = true } + + -- Determine if LSP formatting should be used. + local lsp_format_opt if disable_filetypes[vim.bo[bufnr].filetype] then + -- If the filetype is in the disabled list, don't use LSP formatting. + lsp_format_opt = 'never' + else + -- Otherwise, use LSP formatting as a fallback. + lsp_format_opt = 'fallback' + end + + return { + timeout_ms = 500, + lsp_format = lsp_format_opt, + } + end, + -- Map filetypes to specific formatters. + formatters_by_ft = { + lua = { 'stylua' }, + }, + }, + }, + + -- -- Autocompletion: `nvim-cmp` and its dependencies + -- This is the core of the autocompletion system. + { + 'hrsh7th/nvim-cmp', + -- Only load this plugin when entering insert mode. + event = 'InsertEnter', + dependencies = { + -- `LuaSnip` is the snippet engine. + { + 'L3MON4D3/LuaSnip', + -- This build command ensures the `jsregexp` dependency is installed if needed. + build = (function() + if vim.fn.has 'win32' == 1 or vim.fn.executable 'make' == 0 then return end - return { timeout_ms = 500, lsp_format = 'fallback' } - end, - formatters_by_ft = { - lua = { 'stylua' }, - }, + return 'make install_jsregexp' + end)(), + }, + -- `cmp_luasnip` connects the snippet engine to the autocompletion. + 'saadparwaiz1/cmp_luasnip', + -- `cmp-nvim-lsp` provides completion items from the language server. + 'hrsh7th/cmp-nvim-lsp', + -- `cmp-path` provides completion for file paths. + 'hrsh7th/cmp-path', + -- `lazydev` is used for runtime completion of `lazy.nvim` plugin names. + 'folke/lazydev.nvim', }, + config = function() + -- Get local references to the required modules. + local cmp = require 'cmp' + local luasnip = require 'luasnip' + luasnip.config.setup {} + + -- Set up the `nvim-cmp` plugin. + cmp.setup { + -- Define how snippets are expanded. + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + -- Configure the completion menu. + completion = { completeopt = 'menu,menuone,noinsert' }, + -- Set up key mappings for various completion actions. + mapping = cmp.mapping.preset.insert { + -- Navigate the completion menu. + [''] = cmp.mapping.select_next_item(), + [''] = cmp.mapping.select_prev_item(), + -- Scroll documentation. + [''] = cmp.mapping.scroll_docs(-4), + [''] = cmp.mapping.scroll_docs(4), + -- Confirm the selected completion item. + [''] = cmp.mapping.confirm { select = true }, + -- Manually trigger completion. + [''] = cmp.mapping.complete {}, + -- Jump to the next part of a snippet. + [''] = cmp.mapping(function() + if luasnip.expand_or_locally_jumpable() then + luasnip.expand_or_jump() + end + end, { 'i', 's' }), + -- Jump to the previous part of a snippet. + [''] = cmp.mapping(function() + if luasnip.locally_jumpable(-1) then + luasnip.jump(-1) + end + end, { 'i', 's' }), + }, + -- Define the sources for completion items and their priority. + sources = { + { + name = 'lazydev', + group_index = 0, + }, + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + { name = 'path' }, + }, + } + end, }, { -- Linting diff --git a/lua/plugins/git.lua b/lua/plugins/git.lua index 426546eb..bfbcf7ea 100644 --- a/lua/plugins/git.lua +++ b/lua/plugins/git.lua @@ -1,18 +1,26 @@ ---[plugins/git.lua] - +-- [plugins/git.lua] return { - { -- Adds git related signs to the gutter, as well as utilities for managing changes - 'lewis6991/gitsigns.nvim', + { -- Adds git related signs + keymaps + "lewis6991/gitsigns.nvim", + event = "BufReadPre", -- load only when a file is opened opts = { - -- Basic options for the signs in the gutter signs = { - add = { text = '+' }, - change = { text = '~' }, - delete = { text = '_' }, - topdelete = { text = '‾' }, - changedelete = { text = '~' }, + add = { text = "+" }, + change = { text = "~" }, + delete = { text = "_" }, + topdelete = { text = "‾" }, + changedelete = { text = "~" }, + }, + current_line_blame = true, -- shows git blame at end of line + current_line_blame_opts = { + virt_text = true, + virt_text_pos = "eol", -- "eol" | "overlay" | "right_align" + delay = 500, + ignore_whitespace = false, }, - -- All keymaps are now defined in the on_attach function + current_line_blame_formatter = ", - ", + + -- Keymaps on attach on_attach = function(bufnr) local gs = package.loaded.gitsigns local function map(mode, l, r, desc) @@ -20,42 +28,39 @@ return { end -- Navigation - map('n', ']c', gs.next_hunk, 'Next Hunk') - map('n', '[c', gs.prev_hunk, 'Prev Hunk') + map("n", "]c", gs.next_hunk, "Next Hunk") + map("n", "[c", gs.prev_hunk, "Prev Hunk") -- Actions - map({ 'n', 'v' }, 'hs', gs.stage_hunk, 'Stage Hunk') - map({ 'n', 'v' }, 'hr', gs.reset_hunk, 'Reset Hunk') - map('n', 'hS', gs.stage_buffer, 'Stage Buffer') - map('n', 'hu', gs.undo_stage_hunk, 'Undo Stage Hunk') - map('n', 'hR', gs.reset_buffer, 'Reset Buffer') - map('n', 'hp', gs.preview_hunk, 'Preview Hunk') - map('n', 'hb', function() gs.blame_line { full = true } end, 'Blame Line') - map('n', 'tb', gs.toggle_current_line_blame, 'Toggle Blame Line') - map('n', 'hd', gs.diffthis, 'Diff This') - map('n', 'hD', function() gs.diffthis '~' end, 'Diff This ~') - map('n', 'td', gs.toggle_deleted, 'Toggle Deleted') + map({ "n", "v" }, "hs", gs.stage_hunk, "Stage Hunk") + map({ "n", "v" }, "hr", gs.reset_hunk, "Reset Hunk") + map("n", "hS", gs.stage_buffer, "Stage Buffer") + map("n", "hu", gs.undo_stage_hunk, "Undo Stage Hunk") + map("n", "hR", gs.reset_buffer, "Reset Buffer") + map("n", "hp", gs.preview_hunk, "Preview Hunk") + map("n", "hb", function() gs.blame_line { full = true } end, "Blame Line") + map("n", "tb", gs.toggle_current_line_blame, "Toggle Blame Line") + map("n", "hd", gs.diffthis, "Diff This") + map("n", "hD", function() gs.diffthis("~") end, "Diff This ~") + map("n", "td", gs.toggle_deleted, "Toggle Deleted") -- Text object - map({ 'o', 'x' }, 'ih', ':Gitsigns select_hunk', 'Select Hunk') + map({ "o", "x" }, "ih", ":Gitsigns select_hunk", "Select Hunk") end, }, }, { -- Git wrapper - 'tpope/vim-fugitive', - cmd = { "Git", "G" }, -- lazy load on :Git + "tpope/vim-fugitive", + cmd = { "Git", "G" }, -- lazy load only on :Git commands }, - require('gitsigns').setup { - current_line_blame = true, -- shows git blame at end of line - current_line_blame_opts = { - virt_text = true, - virt_text_pos = 'eol', -- 'eol' | 'overlay' | 'right_align' - delay = 500, - ignore_whitespace = false, + { -- Lazygit UI (optional) + "kdheepak/lazygit.nvim", + cmd = "LazyGit", + dependencies = { "nvim-lua/plenary.nvim" }, + keys = { + { "lg", "LazyGit", desc = "Open LazyGit" }, }, - current_line_blame_formatter = ', - ', - } - + }, } diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 5df1cd5e..45a015cb 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -162,99 +162,4 @@ return { } end, }, - - { -- Autoformat - 'stevearc/conform.nvim', - event = { 'BufWritePre' }, - cmd = { 'ConformInfo' }, - keys = { - { - 'f', - function() - require('conform').format { async = true, lsp_format = 'fallback' } - end, - mode = '', - desc = '[F]ormat buffer', - }, - }, - opts = { - notify_on_error = false, - format_on_save = function(bufnr) - local disable_filetypes = { c = true, cpp = true } - local lsp_format_opt - if disable_filetypes[vim.bo[bufnr].filetype] then - lsp_format_opt = 'never' - else - lsp_format_opt = 'fallback' - end - return { - timeout_ms = 500, - lsp_format = lsp_format_opt, - } - end, - formatters_by_ft = { - lua = { 'stylua' }, - }, - }, - }, - - { -- Autocompletion - 'hrsh7th/nvim-cmp', - event = 'InsertEnter', - dependencies = { - { - 'L3MON4D3/LuaSnip', - build = (function() - if vim.fn.has 'win32' == 1 or vim.fn.executable 'make' == 0 then - return - end - return 'make install_jsregexp' - end)(), - }, - 'saadparwaiz1/cmp_luasnip', - 'hrsh7th/cmp-nvim-lsp', - 'hrsh7th/cmp-path', - }, - config = function() - local cmp = require 'cmp' - local luasnip = require 'luasnip' - luasnip.config.setup {} - - cmp.setup { - snippet = { - expand = function(args) - luasnip.lsp_expand(args.body) - end, - }, - completion = { completeopt = 'menu,menuone,noinsert' }, - mapping = cmp.mapping.preset.insert { - [''] = cmp.mapping.select_next_item(), - [''] = cmp.mapping.select_prev_item(), - [''] = cmp.mapping.scroll_docs(-4), - [''] = cmp.mapping.scroll_docs(4), - [''] = cmp.mapping.confirm { select = true }, - [''] = cmp.mapping.complete {}, - [''] = cmp.mapping(function() - if luasnip.expand_or_locally_jumpable() then - luasnip.expand_or_jump() - end - end, { 'i', 's' }), - [''] = cmp.mapping(function() - if luasnip.locally_jumpable(-1) then - luasnip.jump(-1) - end - end, { 'i', 's' }), - }, - sources = { - { - name = 'lazydev', - group_index = 0, - }, - { name = 'nvim_lsp' }, - { name = 'luasnip' }, - { name = 'path' }, - }, - } - end, - }, } diff --git a/lua/plugins/tools.lua b/lua/plugins/tools.lua index 432286ac..dfd48571 100644 --- a/lua/plugins/tools.lua +++ b/lua/plugins/tools.lua @@ -70,6 +70,57 @@ return { }, }, + -- neo-tree.lua + 'nvim-neo-tree/neo-tree.nvim', + branch = 'v3.x', + dependencies = { + 'nvim-lua/plenary.nvim', + 'nvim-tree/nvim-web-devicons', -- not strictly required, but recommended + 'MunifTanjim/nui.nvim', + }, + cmd = 'Neotree', + keys = { + { + 'e', + function() + require('neo-tree.command').execute { toggle = true, dir = vim.loop.cwd() } + end, + desc = 'Explorer NeoTree (Current Dir)', + }, + { + 'E', + function() + require('neo-tree.command').execute { toggle = true, dir = vim.fn.stdpath 'config' } + end, + desc = 'Explorer NeoTree (Config Dir)', + }, + }, + opts = { + close_if_last_window = true, -- Close Neo-tree if it is the last window left + popup_border_style = 'rounded', + filesystem = { + filtered_items = { + visible = true, + hide_dotfiles = false, + hide_gitignored = true, + }, + follow_current_file = { + enabled = true, -- This will find and focus the file in the tree when you open a buffer. + }, + hijack_netrw_behavior = 'open_current', -- Or 'disabled' if you prefer to keep netrw for directories. + }, + window = { + mappings = { + [''] = 'none', -- Disable space for opening folders, allowing for custom mappings + ['l'] = 'open', + ['h'] = 'close_node', + ['S'] = 'open_split', + ['s'] = 'open_vsplit', + }, + }, + }, + + { -- Oil.nvim: directory navigation as buffers 'stevearc/oil.nvim', opts = {}, @@ -97,17 +148,23 @@ return { }, { -- Snacks.nvim: utilities (scratch, terminal, etc.) - 'folke/snacks.nvim', - config = function() - require("snacks").setup({ + "folke/snacks.nvim", + event = "VeryLazy", + opts = { terminal = { win = { style = "float" } }, - scratch = true, - }) - vim.keymap.set("n", "tt", function() require("snacks.terminal").toggle() end, { desc = "Toggle terminal" }) - vim.keymap.set("n", "ss", function() require("snacks.scratch").open() end, { desc = "Open scratch buffer" }) + scratch = { enabled = true }, -- ✅ must be a table + }, + config = function(_, opts) + local snacks = require("snacks") + snacks.setup(opts) + + -- Keymaps + vim.keymap.set("n", "tt", function() snacks.terminal.toggle() end, { desc = "Toggle terminal" }) + vim.keymap.set("n", "sc", function() snacks.scratch.open() end, { desc = "Open [s]cratch buffer" }) end, }, + { -- Debugging (DAP) 'mfussenegger/nvim-dap', dependencies = {