return { -- auto completion { "hrsh7th/nvim-cmp", version = false, -- last release is way too old event = "InsertEnter", dependencies = { "hrsh7th/cmp-nvim-lsp", "hrsh7th/cmp-buffer", "hrsh7th/cmp-path", }, -- Not all LSP servers add brackets when completing a function. -- To better deal with this, LazyVim adds a custom option to cmp, -- that you can configure. For example: -- -- ```lua -- opts = { -- auto_brackets = { "python" } -- } -- ``` opts = function() vim.api.nvim_set_hl(0, "CmpGhostText", { link = "Comment", default = true }) local cmp = require("cmp") local defaults = require("cmp.config.default")() return { auto_brackets = {}, -- configure any filetype to auto add brackets completion = { completeopt = "menu,menuone,noinsert", }, mapping = cmp.mapping.preset.insert({ [""] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }), [""] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }), [""] = cmp.mapping.scroll_docs(-4), [""] = cmp.mapping.scroll_docs(4), [""] = cmp.mapping.complete(), [""] = cmp.mapping.abort(), [""] = cmp.mapping.confirm({ select = true }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. [""] = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true, }), -- Accept currently selected item. Set `select` to `false` to only confirm explicitly selected items. [""] = function(fallback) cmp.abort() fallback() end, }), sources = cmp.config.sources({ { name = "nvim_lsp" }, { name = "path" }, }, { { name = "buffer" }, }), formatting = { format = function(_, item) local icons = require("lazyvim.config").icons.kinds if icons[item.kind] then item.kind = icons[item.kind] .. item.kind end return item end, }, experimental = { ghost_text = { hl_group = "CmpGhostText", }, }, sorting = defaults.sorting, } end, ---@param opts cmp.ConfigSchema | {auto_brackets?: string[]} config = function(_, opts) for _, source in ipairs(opts.sources) do source.group_index = source.group_index or 1 end local cmp = require("cmp") local Kind = cmp.lsp.CompletionItemKind cmp.setup(opts) cmp.event:on("confirm_done", function(event) if not vim.tbl_contains(opts.auto_brackets or {}, vim.bo.filetype) then return end local entry = event.entry local item = entry:get_completion_item() if vim.tbl_contains({ Kind.Function, Kind.Method }, item.kind) and item.insertTextFormat ~= 2 then local cursor = vim.api.nvim_win_get_cursor(0) local prev_char = vim.api.nvim_buf_get_text(0, cursor[1] - 1, cursor[2], cursor[1] - 1, cursor[2] + 1, {})[1] if prev_char ~= "(" and prev_char ~= ")" then local keys = vim.api.nvim_replace_termcodes("()", false, false, true) vim.api.nvim_feedkeys(keys, "i", true) end end end) end, }, -- snippets vim.snippet and { "nvim-cmp", dependencies = { { "rafamadriz/friendly-snippets" }, { "garymjr/nvim-snippets", opts = { friendly_snippets = true } }, }, opts = function(_, opts) opts.snippet = { expand = function(args) vim.snippet.expand(args.body) end, } table.insert(opts.sources, { name = "snippets" }) end, keys = { { "", function() if vim.snippet.active({ direction = 1 }) then vim.schedule(function() vim.snippet.jump(1) end) return end return "" end, expr = true, silent = true, mode = "i", }, { "", function() vim.schedule(function() vim.snippet.jump(1) end) end, silent = true, mode = "s", }, { "", function() if vim.snippet.active({ direction = -1 }) then vim.schedule(function() vim.snippet.jump(-1) end) return end return "" end, expr = true, silent = true, mode = { "i", "s" }, }, }, } or { import = "lazyvim.plugins.extras.coding.luasnip", enabled = vim.fn.has("nvim-0.10") == 0 }, -- auto pairs { "echasnovski/mini.pairs", event = "VeryLazy", opts = { mappings = { ["`"] = { action = "closeopen", pair = "``", neigh_pattern = "[^\\`].", register = { cr = false } }, }, }, keys = { { "up", function() vim.g.minipairs_disable = not vim.g.minipairs_disable if vim.g.minipairs_disable then LazyVim.warn("Disabled auto pairs", { title = "Option" }) else LazyVim.info("Enabled auto pairs", { title = "Option" }) end end, desc = "Toggle Auto Pairs", }, }, }, -- Fast and feature-rich surround actions. For text that includes -- surrounding characters like brackets or quotes, this allows you -- to select the text inside, change or modify the surrounding characters, -- and more. { "echasnovski/mini.surround", keys = function(_, keys) -- Populate the keys based on the user's options local plugin = require("lazy.core.config").spec.plugins["mini.surround"] local opts = require("lazy.core.plugin").values(plugin, "opts", false) local mappings = { { opts.mappings.add, desc = "Add Surrounding", mode = { "n", "v" } }, { opts.mappings.delete, desc = "Delete Surrounding" }, { opts.mappings.find, desc = "Find Right Surrounding" }, { opts.mappings.find_left, desc = "Find Left Surrounding" }, { opts.mappings.highlight, desc = "Highlight Surrounding" }, { opts.mappings.replace, desc = "Replace Surrounding" }, { opts.mappings.update_n_lines, desc = "Update `MiniSurround.config.n_lines`" }, } mappings = vim.tbl_filter(function(m) return m[1] and #m[1] > 0 end, mappings) return vim.list_extend(mappings, keys) end, opts = { mappings = { add = "gsa", -- Add surrounding in Normal and Visual modes delete = "gsd", -- Delete surrounding find = "gsf", -- Find surrounding (to the right) find_left = "gsF", -- Find surrounding (to the left) highlight = "gsh", -- Highlight surrounding replace = "gsr", -- Replace surrounding update_n_lines = "gsn", -- Update `n_lines` }, }, }, -- comments { "JoosepAlviste/nvim-ts-context-commentstring", lazy = true, opts = { enable_autocmd = false, }, }, { import = "lazyvim.plugins.extras.coding.mini-comment", enabled = function() if vim.fn.has("nvim-0.10") == 1 then -- Majestically override the native `get_commentstring` function. vim.schedule(function() LazyVim.inject.set_upvalue( LazyVim.inject.get_upvalue(require("vim._comment").textobject, "get_comment_parts"), "get_commentstring", function() return require("ts_context_commentstring.internal").calculate_commentstring() or vim.bo.commentstring end ) end) else return true end end, }, -- Better text-objects { "echasnovski/mini.ai", -- keys = { -- { "a", mode = { "x", "o" } }, -- { "i", mode = { "x", "o" } }, -- }, event = "VeryLazy", opts = function() local ai = require("mini.ai") return { n_lines = 500, custom_textobjects = { o = ai.gen_spec.treesitter({ a = { "@block.outer", "@conditional.outer", "@loop.outer" }, i = { "@block.inner", "@conditional.inner", "@loop.inner" }, }, {}), f = ai.gen_spec.treesitter({ a = "@function.outer", i = "@function.inner" }, {}), c = ai.gen_spec.treesitter({ a = "@class.outer", i = "@class.inner" }, {}), t = { "<([%p%w]-)%f[^<%w][^<>]->.-", "^<.->().*()$" }, d = { "%f[%d]%d+" }, -- digits e = { -- Word with case { "%u[%l%d]+%f[^%l%d]", "%f[%S][%l%d]+%f[^%l%d]", "%f[%P][%l%d]+%f[^%l%d]", "^[%l%d]+%f[^%l%d]", }, "^().*()$", }, g = function() -- Whole buffer, similar to `gg` and 'G' motion local from = { line = 1, col = 1 } local to = { line = vim.fn.line("$"), col = math.max(vim.fn.getline("$"):len(), 1), } return { from = from, to = to } end, u = ai.gen_spec.function_call(), -- u for "Usage" U = ai.gen_spec.function_call({ name_pattern = "[%w_]" }), -- without dot in function name }, } end, config = function(_, opts) require("mini.ai").setup(opts) -- register all text objects with which-key LazyVim.on_load("which-key.nvim", function() ---@type table local i = { [" "] = "Whitespace", ['"'] = 'Balanced "', ["'"] = "Balanced '", ["`"] = "Balanced `", ["("] = "Balanced (", [")"] = "Balanced ) including white-space", [">"] = "Balanced > including white-space", [""] = "Balanced <", ["]"] = "Balanced ] including white-space", ["["] = "Balanced [", ["}"] = "Balanced } including white-space", ["{"] = "Balanced {", ["?"] = "User Prompt", _ = "Underscore", a = "Argument", b = "Balanced ), ], }", c = "Class", d = "Digit(s)", e = "Word in CamelCase & snake_case", f = "Function", g = "Entire file", o = "Block, conditional, loop", q = "Quote `, \", '", t = "Tag", u = "Use/call function & method", U = "Use/call without dot in name", } local a = vim.deepcopy(i) for k, v in pairs(a) do a[k] = v:gsub(" including.*", "") end local ic = vim.deepcopy(i) local ac = vim.deepcopy(a) for key, name in pairs({ n = "Next", l = "Last" }) do i[key] = vim.tbl_extend("force", { name = "Inside " .. name .. " textobject" }, ic) a[key] = vim.tbl_extend("force", { name = "Around " .. name .. " textobject" }, ac) end require("which-key").register({ mode = { "o", "x" }, i = i, a = a, }) end) end, }, }