From 58a73588b60762f86995d78e2fda0f362bcbdddd Mon Sep 17 00:00:00 2001 From: hwu Date: Tue, 2 Sep 2025 19:54:22 -0400 Subject: [PATCH] add blink cmp --- init.lua | 4 +- lua/custom/plugins/cmp.lua | 167 +++++++++++------------ lua/custom/plugins/lspconfig.lua | 197 +++------------------------- lua/custom/plugins/pairs.lua | 10 +- lua/custom/plugins/snacks.lua | 2 +- lua/kickstart/plugins/autopairs.lua | 16 --- lua/myconfigs.lua | 2 + 7 files changed, 99 insertions(+), 299 deletions(-) delete mode 100644 lua/kickstart/plugins/autopairs.lua diff --git a/init.lua b/init.lua index 2c81f7eb..81c154bb 100644 --- a/init.lua +++ b/init.lua @@ -318,13 +318,15 @@ require('lazy').setup({ -- Document existing key chains spec = { { 'c', group = '[C]ode', mode = { 'n', 'x' } }, + { 'b', group = '[B]uffer' }, { 'd', group = '[D]ocument' }, { 'r', group = '[R]ename' }, { 's', group = '[S]earch' }, { 'f', group = '[F]ormat' }, + { 'g', group = '[G]it' }, { 'w', group = '[W]orkspace' }, { 't', group = '[T]oggle' }, - { 'h', group = 'Git [H]unk', mode = { 'n', 'v' } }, + { 'u', group = '[U]I' }, { 'y', group = '[Y]ank' }, }, triggers = { diff --git a/lua/custom/plugins/cmp.lua b/lua/custom/plugins/cmp.lua index c23f4f0d..c8ff94ca 100644 --- a/lua/custom/plugins/cmp.lua +++ b/lua/custom/plugins/cmp.lua @@ -1,103 +1,90 @@ return { - 'hrsh7th/nvim-cmp', - event = 'InsertEnter', + 'saghen/blink.cmp', + -- optional: provides snippets for the snippet source dependencies = { - 'hrsh7th/cmp-buffer', -- source for text in buffer - 'hrsh7th/cmp-path', -- source for file system paths + { 'rafamadriz/friendly-snippets' }, { 'L3MON4D3/LuaSnip', - -- follow latest release. - version = 'v2.*', -- Replace by the latest released major (first number of latest release) - -- install jsregexp (optional!). - build = 'make install_jsregexp', + version = 'v2.*', + config = function() + -- load snippets from path/of/your/nvim/config/my-cool-snippets + require('luasnip.loaders.from_vscode').lazy_load() + require('luasnip.loaders.from_vscode').lazy_load { paths = vim.fn.stdpath 'config' .. '/mysnippets/' } + end, }, - 'saadparwaiz1/cmp_luasnip', -- for autocompletion - 'rafamadriz/friendly-snippets', -- useful snippets - 'onsails/lspkind.nvim', -- vs-code like pictograms }, - config = function() - local cmp = require 'cmp' - local luasnip = require 'luasnip' + -- use a release tag to download pre-built binaries + version = '1.*', + -- AND/OR build from source, requires nightly: https://rust-lang.github.io/rustup/concepts/channels.html#working-with-nightly-rust + -- build = 'cargo build --release', + -- If you use nix, you can build from source using latest nightly rust with: + -- build = 'nix run .#build-plugin', - local lspkind = require 'lspkind' - - -- loads vscode style snippets from installed plugins (e.g. friendly-snippets) - require('luasnip.loaders.from_vscode').lazy_load {} - - -- https://github.com/L3MON4D3/LuaSnip?tab=readme-ov-file - for _, ft_path in ipairs(vim.api.nvim_get_runtime_file('mysnippets/*.lua', true)) do - loadfile(ft_path)() - end - - cmp.setup { - preselect = cmp.PreselectMode.None, - completion = { - completeopt = 'menu,menuone,noinsert,noselect', - }, - experimental = { ghost_text = true }, - snippet = { -- configure how nvim-cmp interacts with snippet engine - expand = function(args) - luasnip.lsp_expand(args.body) - end, - }, - mapping = cmp.mapping.preset.insert { - [''] = cmp.mapping.abort(), -- close completion window - [''] = cmp.mapping.confirm { select = true }, - - [''] = cmp.mapping.select_next_item(), - [''] = cmp.mapping.select_prev_item(), - -- [''] = cmp.mapping.select_next_item { behavior = cmp.SelectBehavior.Select }, - -- [''] = cmp.mapping.select_prev_item { behavior = cmp.SelectBehavior.Select }, - - -- Scroll the documentation window [b]ack / [f]orward - [''] = cmp.mapping.scroll_docs(-4), - [''] = cmp.mapping.scroll_docs(4), - - [''] = cmp.mapping(function(fallback) - if cmp.visible() then - cmp.confirm { select = true } - elseif luasnip.expand_or_locally_jumpable() then - luasnip.expand_or_jump() + ---@module 'blink.cmp' + ---@type blink.cmp.Config + opts = { + -- 'default' (recommended) for mappings similar to built-in completions (C-y to accept) + -- 'super-tab' for mappings similar to vscode (tab to accept) + -- 'enter' for enter to accept + -- 'none' for no mappings + -- + -- All presets have the following mappings: + -- C-space: Open menu or open docs if already open + -- C-n/C-p or Up/Down: Select next/previous item + -- C-e: Hide menu + -- C-k: Toggle signature help (if signature.enabled = true) + -- + -- See :h blink-cmp-config-keymap for defining your own keymap + keymap = { + preset = 'default', + [''] = { + function(cmp) + if cmp.snippet_active() then + return cmp.accept() else - cmp.complete() + return cmp.select_and_accept() end - end, { 'i', 's' }), + end, + 'snippet_forward', + }, + [''] = { 'snippet_backward', 'fallback' }, + }, - [''] = 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' }), + appearance = { + -- 'mono' (default) for 'Nerd Font Mono' or 'normal' for 'Nerd Font' + -- Adjusts spacing to ensure icons are aligned + nerd_font_variant = 'mono', + }, + + signature = { enabled = true }, + completion = { + -- menu = { border = 'single' }, + documentation = { + auto_show = true, + auto_show_delay_ms = 500, }, - -- sources for autocompletion - sources = cmp.config.sources { - { name = 'nvim_lsp' }, - { name = 'luasnip' }, -- snippets - { name = 'buffer' }, -- text within current buffer - { name = 'path' }, -- file system paths - }, - -- sorting = { - -- comparators = { - -- cmp.config.compare.score, - -- cmp.config.compare.recently_used, - -- }, - -- }, - -- configure lspkind for vs-code like pictograms in completion menu - formatting = { - expandable_indicator = true, - fields = { cmp.ItemField.Abbr, cmp.ItemField.Kind }, - format = lspkind.cmp_format { - maxwidth = 50, - ellipsis_char = '...', - }, - }, - } - end, + accept = { auto_brackets = { enabled = true } }, + list = { selection = { preselect = false, auto_insert = false } }, + -- Display a preview of the selected item on the current line + ghost_text = { enabled = true }, + }, + + -- ensure you have the `snippets` source (enabled by default) + -- snippets = { preset = 'default' }, + snippets = { preset = 'luasnip' }, + -- Default list of enabled providers defined so that you can extend it + -- elsewhere in your config, without redefining it, due to `opts_extend` + sources = { + default = { 'lsp', 'path', 'snippets', 'buffer' }, + }, + + -- (Default) Rust fuzzy matcher for typo resistance and significantly better performance + -- You may use a lua implementation instead by using `implementation = "lua"` or fallback to the lua implementation, + -- when the Rust fuzzy matcher is not available, by using `implementation = "prefer_rust"` + -- + -- See the fuzzy documentation for more information + fuzzy = { implementation = 'prefer_rust_with_warning' }, + }, + opts_extend = { 'sources.default' }, } diff --git a/lua/custom/plugins/lspconfig.lua b/lua/custom/plugins/lspconfig.lua index 42e0beb4..dac48560 100644 --- a/lua/custom/plugins/lspconfig.lua +++ b/lua/custom/plugins/lspconfig.lua @@ -1,142 +1,9 @@ return { - -- Main LSP Configuration 'neovim/nvim-lspconfig', - dependencies = { - -- Automatically install LSPs and related tools to stdpath for Neovim - { 'williamboman/mason.nvim', config = true }, -- NOTE: Must be loaded before dependants - 'williamboman/mason-lspconfig.nvim', - 'WhoIsSethDaniel/mason-tool-installer.nvim', - - -- Useful status updates for LSP. - -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})` - { 'j-hui/fidget.nvim', opts = {} }, - - -- Allows extra capabilities provided by nvim-cmp - 'hrsh7th/cmp-nvim-lsp', - }, - config = function() - -- Brief aside: **What is LSP?** - -- - -- LSP is an initialism you've probably heard, but might not understand what it is. - -- - -- LSP stands for Language Server Protocol. It's a protocol that helps editors - -- and language tooling communicate in a standardized fashion. - -- - -- In general, you have a "server" which is some tool built to understand a particular - -- language (such as `gopls`, `lua_ls`, `rust_analyzer`, etc.). These Language Servers - -- (sometimes called LSP servers, but that's kind of like ATM Machine) are standalone - -- processes that communicate with some "client" - in this case, Neovim! - -- - -- LSP provides Neovim with features like: - -- - Go to definition - -- - Find references - -- - Autocompletion - -- - Symbol Search - -- - and more! - -- - -- Thus, Language Servers are external tools that must be installed separately from - -- Neovim. This is where `mason` and related plugins come into play. - -- - -- If you're wondering about lsp vs treesitter, you can check out the wonderfully - -- and elegantly composed help section, `:help lsp-vs-treesitter` - - -- This function gets run when an LSP attaches to a particular buffer. - -- That is to say, every time a new file is opened that is associated with - -- an lsp (for example, opening `main.rs` is associated with `rust_analyzer`) this - -- function will be executed to configure the current buffer - vim.api.nvim_create_autocmd('LspAttach', { - group = vim.api.nvim_create_augroup('kickstart-lsp-attach', { clear = true }), - callback = function(event) - -- NOTE: Remember that Lua is a real programming language, and as such it is possible - -- to define small helper and utility functions so you don't have to repeat yourself. - -- - -- In this case, we create a function that lets us more easily define mappings specific - -- for LSP related items. It sets the mode, buffer and description for us each time. - local map = function(keys, func, desc, mode) - mode = mode or 'n' - vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = 'LSP: ' .. desc }) - end - - -- Rename the variable under your cursor. - -- Most Language Servers support renaming across files, etc. - map('rn', vim.lsp.buf.rename, '[R]e[n]ame') - - -- Execute a code action, usually your cursor needs to be on top of an error - -- or a suggestion from your LSP for this to activate. - map('ca', vim.lsp.buf.code_action, '[C]ode [A]ction', { 'n', 'x' }) - - -- WARN: This is not Goto Definition, this is Goto Declaration. - -- For example, in C this would take you to the header. - map('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration') - - -- The following two autocommands are used to highlight references of the - -- word under your cursor when your cursor rests there for a little while. - -- See `:help CursorHold` for information about when this is executed - -- - -- When you move your cursor, the highlights will be cleared (the second autocommand). - local client = vim.lsp.get_client_by_id(event.data.client_id) - if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_documentHighlight) then - local highlight_augroup = vim.api.nvim_create_augroup('kickstart-lsp-highlight', { clear = false }) - vim.api.nvim_create_autocmd({ 'CursorHold', 'CursorHoldI' }, { - buffer = event.buf, - group = highlight_augroup, - callback = vim.lsp.buf.document_highlight, - }) - - vim.api.nvim_create_autocmd({ 'CursorMoved', 'CursorMovedI' }, { - buffer = event.buf, - group = highlight_augroup, - callback = vim.lsp.buf.clear_references, - }) - - vim.api.nvim_create_autocmd('LspDetach', { - group = vim.api.nvim_create_augroup('kickstart-lsp-detach', { clear = true }), - callback = function(event2) - vim.lsp.buf.clear_references() - vim.api.nvim_clear_autocmds { group = 'kickstart-lsp-highlight', buffer = event2.buf } - end, - }) - end - - -- The following code creates a keymap to toggle inlay hints in your - -- code, if the language server you are using supports them - -- - -- This may be unwanted, since they displace some of your code - if client and client.supports_method(vim.lsp.protocol.Methods.textDocument_inlayHint) then - map('th', function() - vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled { bufnr = event.buf }) - end, '[T]oggle Inlay [H]ints') - end - end, - }) - - -- LSP servers and clients are able to communicate to each other what features they support. - -- By default, Neovim doesn't support everything that is in the LSP specification. - -- When you add nvim-cmp, luasnip, etc. Neovim now has *more* capabilities. - -- So, we create new capabilities with nvim cmp, and then broadcast that to the servers. - local capabilities = vim.lsp.protocol.make_client_capabilities() - capabilities = vim.tbl_deep_extend('force', capabilities, require('cmp_nvim_lsp').default_capabilities()) - - -- Enable the following language servers - -- Feel free to add/remove any LSPs that you want here. They will automatically be installed. - -- - -- Add any additional override configuration in the following tables. Available keys are: - -- - cmd (table): Override the default command used to start the server - -- - filetypes (table): Override the default list of associated filetypes for the server - -- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features. - -- - settings (table): Override the default settings passed when initializing the server. - -- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/ - local servers = { - -- clangd = {}, - -- gopls = {}, - -- pyright = {}, - -- rust_analyzer = {}, - -- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs - -- - -- Some languages (like typescript) have entire language plugins that can be useful: - -- https://github.com/pmizio/typescript-tools.nvim - -- - -- But for many setups, the LSP (`ts_ls`) will work just fine + dependencies = { 'saghen/blink.cmp' }, + -- example using `opts` for defining servers + opts = { + servers = { ts_ls = {}, -- html = { filetypes = { 'html', 'twig', 'hbs' } }, @@ -146,50 +13,16 @@ return { sqlls = {}, jsonls = {}, yamlls = {}, - - lua_ls = { - -- cmd = {...}, - -- filetypes = { ...}, - -- capabilities = {}, - settings = { - Lua = { - completion = { - callSnippet = 'Replace', - }, - -- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings - -- diagnostics = { disable = { 'missing-fields' } }, - }, - }, - }, - } - - -- Ensure the servers and tools above are installed - -- To check the current status of installed tools and/or manually install - -- other tools, you can run - -- :Mason - -- - -- You can press `g?` for help in this menu. - require('mason').setup() - - -- You can add other tools here that you want Mason to install - -- for you, so that they are available from within Neovim. - local ensure_installed = vim.tbl_keys(servers or {}) - vim.list_extend(ensure_installed, { - 'stylua', -- Used to format Lua code - }) - require('mason-tool-installer').setup { ensure_installed = ensure_installed } - - require('mason-lspconfig').setup { - handlers = { - function(server_name) - local server = servers[server_name] or {} - -- This handles overriding only values explicitly passed - -- by the server configuration above. Useful when disabling - -- certain features of an LSP (for example, turning off formatting for ts_ls) - server.capabilities = vim.tbl_deep_extend('force', {}, capabilities, server.capabilities or {}) - require('lspconfig')[server_name].setup(server) - end, - }, - } + lua_ls = {}, + }, + }, + config = function(_, opts) + local lspconfig = require 'lspconfig' + for server, config in pairs(opts.servers) do + -- passing config.capabilities to blink.cmp merges with the capabilities in your + -- `opts[server].capabilities, if you've defined it + config.capabilities = require('blink.cmp').get_lsp_capabilities(config.capabilities) + lspconfig[server].setup(config) + end end, } diff --git a/lua/custom/plugins/pairs.lua b/lua/custom/plugins/pairs.lua index 85202cae..4c76ed99 100644 --- a/lua/custom/plugins/pairs.lua +++ b/lua/custom/plugins/pairs.lua @@ -1,13 +1,5 @@ return { 'windwp/nvim-autopairs', event = 'InsertEnter', - -- config = true, - -- use opts = {} for passing setup options - -- this is equalent to setup({}) function - config = function() - require('nvim-autopairs').setup {} - local cmp_autopairs = require 'nvim-autopairs.completion.cmp' - local cmp = require 'cmp' - cmp.event:on('confirm_done', cmp_autopairs.on_confirm_done()) - end, + config = true, } diff --git a/lua/custom/plugins/snacks.lua b/lua/custom/plugins/snacks.lua index 0c3bad73..b6fe962e 100644 --- a/lua/custom/plugins/snacks.lua +++ b/lua/custom/plugins/snacks.lua @@ -99,7 +99,7 @@ return { desc = 'Find Config File', }, { - 'ff', + 'fF', function() Snacks.picker.files() end, diff --git a/lua/kickstart/plugins/autopairs.lua b/lua/kickstart/plugins/autopairs.lua deleted file mode 100644 index 87a7e5ff..00000000 --- a/lua/kickstart/plugins/autopairs.lua +++ /dev/null @@ -1,16 +0,0 @@ --- autopairs --- https://github.com/windwp/nvim-autopairs - -return { - 'windwp/nvim-autopairs', - event = 'InsertEnter', - -- Optional dependency - dependencies = { 'hrsh7th/nvim-cmp' }, - config = function() - require('nvim-autopairs').setup {} - -- If you want to automatically add `(` after selecting a function or method - local cmp_autopairs = require 'nvim-autopairs.completion.cmp' - local cmp = require 'cmp' - cmp.event:on('confirm_done', cmp_autopairs.on_confirm_done()) - end, -} diff --git a/lua/myconfigs.lua b/lua/myconfigs.lua index 8551e098..28944f33 100644 --- a/lua/myconfigs.lua +++ b/lua/myconfigs.lua @@ -43,6 +43,8 @@ vim.keymap.set('n', 'w', ':w', { noremap = true, desc = 'Save f vim.keymap.set('i', '', vim.lsp.buf.signature_help, { noremap = true, desc = 'Signature Help' }) vim.keymap.set('n', 'ff', vim.lsp.buf.format, { noremap = true, desc = 'LSP format' }) +vim.keymap.set('n', 'rn', vim.lsp.buf.rename, { noremap = true, desc = 'Rename' }) +vim.keymap.set('n', 'ca', vim.lsp.buf.code_action, { noremap = true, desc = 'Code Action' }) vim.api.nvim_set_keymap('n', 'yr', ":let @+=expand('%:.%:t')", { noremap = true, silent = true, desc = 'Copy Relative Path' }) vim.api.nvim_set_keymap('n', 'ya', ":let @+=expand('%:P%:t')", { noremap = true, silent = true, desc = 'Copy Absolute Path' })