diff options
| author | Ibrahim Muftee <ibrahim@muftee.net> | 2024-05-10 09:24:37 -0500 |
|---|---|---|
| committer | Ibrahim Muftee <ibrahim@muftee.net> | 2026-07-01 00:23:31 -0400 |
| commit | e456adf0c9c6ef27f42919b027d3aecb01755b0e (patch) | |
| tree | 291f7b01499be06bbc3fbb56095ac383d0b26240 | |
| parent | ef171af0a70c6adcf19cef150e3f74d13cba4207 (diff) | |
feat(nvim): add copilot to nvim-cmp
additionally move nvim-cmp plugin to its own file
| -rw-r--r-- | .chezmoi.toml.tmpl | 3 | ||||
| -rw-r--r-- | dot_config/nvim/lua/plugins/lsp-zero.lua | 36 | ||||
| -rw-r--r-- | dot_config/nvim/lua/plugins/nvim-cmp.lua.tmpl | 178 |
3 files changed, 185 insertions, 32 deletions
diff --git a/.chezmoi.toml.tmpl b/.chezmoi.toml.tmpl index f9a8b7a..5f8c013 100644 --- a/.chezmoi.toml.tmpl +++ b/.chezmoi.toml.tmpl @@ -15,6 +15,8 @@ {{ $git_email := promptStringOnce . "git_email" "Email address for Git global config" -}} {{ $git_name := promptStringOnce . "git_name" "Display name for Git global config" -}} +{{ $enable_nvim_copilot := promptBoolOnce . "enable_nvim_copilot" "Enable features related to GitHub Copilot" -}} + {{ if eq .chezmoi.os "windows" -}} [cd] command = "pwsh" @@ -26,6 +28,7 @@ [data] git_email = {{ $git_email | quote }} git_name = {{ $git_name | quote }} + enable_nvim_copilot = {{ $enable_nvim_copilot }} [diff] exclude = ["scripts"] diff --git a/dot_config/nvim/lua/plugins/lsp-zero.lua b/dot_config/nvim/lua/plugins/lsp-zero.lua index a234493..67ea808 100644 --- a/dot_config/nvim/lua/plugins/lsp-zero.lua +++ b/dot_config/nvim/lua/plugins/lsp-zero.lua @@ -18,44 +18,16 @@ return { config = true, }, - -- Autocompletion - { - 'hrsh7th/nvim-cmp', - event = 'InsertEnter', - dependencies = { - { 'L3MON4D3/LuaSnip' }, - }, - config = function() - -- Here is where you configure the autocompletion settings. - local lsp_zero = require('lsp-zero') - lsp_zero.extend_cmp() - - -- And you can configure cmp even more, if you want to. - local cmp = require('cmp') - local cmp_action = lsp_zero.cmp_action() - - cmp.setup({ - formatting = lsp_zero.cmp_format({details = true}), - mapping = cmp.mapping.preset.insert({ - ['<C-Space>'] = cmp.mapping.complete(), - ['<C-u>'] = cmp.mapping.scroll_docs(-8), - ['<C-d>'] = cmp.mapping.scroll_docs(8), - -- ['<C-f>'] = cmp.mapping.luasnip_jump_forward(), - -- ['<C-b>'] = cmp.mapping.luasnip_jump_backward(), - }) - }) - end - }, -- LSP { 'neovim/nvim-lspconfig', - cmd = {'LspInfo', 'LspInstall', 'LspStart'}, - event = {'BufReadPre', 'BufNewFile'}, + cmd = { 'LspInfo', 'LspInstall', 'LspStart' }, + event = { 'BufReadPre', 'BufNewFile' }, dependencies = { { 'hrsh7th/cmp-nvim-lsp' }, { 'williamboman/mason-lspconfig.nvim' }, - { "j-hui/fidget.nvim", tag = "legacy", opts = {} }, + { "j-hui/fidget.nvim", tag = "legacy", opts = {} }, }, config = function() -- This is where all the LSP shenanigans will live @@ -67,7 +39,7 @@ return { lsp_zero.on_attach(function(client, bufnr) -- see :help lsp_zero_keybindings -- to learn the available actions - lsp_zero.default_keymaps({buffer = bufnr}) + lsp_zero.default_keymaps({ buffer = bufnr }) end) require('mason-lspconfig').setup({ diff --git a/dot_config/nvim/lua/plugins/nvim-cmp.lua.tmpl b/dot_config/nvim/lua/plugins/nvim-cmp.lua.tmpl new file mode 100644 index 0000000..60c77d3 --- /dev/null +++ b/dot_config/nvim/lua/plugins/nvim-cmp.lua.tmpl @@ -0,0 +1,178 @@ +-- {{/* +-- vim:ft=lua +-- This file supports Go's text/template language. +-- +-- My work role allows me to use an enterprise subscription to GitHub +-- Copilot, but I don't pay to use this personally. +-- +-- Since copilot.lua is integrated tightly into nvim-cmp, using a +-- Chezmoi template is the easiest way to manage this machine-specific +-- difference. +-- +-- Unfortunately, it's tough to get an editor to treat this as both +-- a Go template and as a Lua file. I compromise by calling it a Lua +-- file and wrapping template directives in Lua comments. This has the +-- side effect of adding a few extra random dash characters to the +-- rendered file, but I don't mind. +-- */}} + +-- Optional features: +-- * GitHub Copilot: {{ .enable_nvim_copilot }} +-- +-- To adjust optional features, see the Chezmoi config at {{ .chezmoi.configFile }} + +return { + "hrsh7th/nvim-cmp", + version = false, -- last release is way too old + event = { "InsertEnter", "CmdlineEnter" }, + dependencies = { + "L3MON4D3/LuaSnip", -- Snippet engine + "rafamadriz/friendly-snippets", -- Useful snippets in VSCode snippet format + "onsails/lspkind.nvim", -- Pretty formatting in the completion menu + + -- Completion sources + "hrsh7th/cmp-nvim-lsp", + "hrsh7th/cmp-nvim-lsp-signature-help", + "hrsh7th/cmp-buffer", + "hrsh7th/cmp-path", + "saadparwaiz1/cmp_luasnip", + + -- {{ if eq .enable_nvim_copilot true -}} + -- GitHub Copilot + "zbirenbaum/copilot.lua", + "zbirenbaum/copilot-cmp", + -- {{- end }} + }, + config = function() + local cmp = require("cmp") + local luasnip = require("luasnip") + + -- Load any VSCode-style snippets from other installed plugins + require("luasnip.loaders.from_vscode").lazy_load() + + -- {{ if eq .enable_nvim_copilot true -}} + -- Init Copilot + require("copilot").setup({ + suggestion = { enabled = false }, + panel = { enabled = false }, + }) + require("copilot_cmp").setup() + -- {{ end }} + + cmp.setup({ + completion = { + -- See :help completeopt + completeopt = "menu,menuone,noinsert,preview", + }, + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + ["<C-n>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }), + ["<C-p>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }), + ["<C-b>"] = cmp.mapping.scroll_docs(-4), + ["<C-f>"] = cmp.mapping.scroll_docs(4), + + -- Ctrl+Space does not currently work in Neovim core on Windows + -- https://github.com/neovim/neovim/issues/8435 + -- ["<C-Space>"] = cmp.mapping.complete(), + ["<C-c>"] = cmp.mapping.complete(), + + ["<C-e>"] = cmp.mapping.abort(), + ["<C-y>"] = cmp.mapping.confirm({ + -- Accept currently selected item + behavior = cmp.ConfirmBehavior.Insert, + select = true, + }), + ["<C-S-y>"] = cmp.mapping.confirm({ + -- Accept currently selected item, replacing stuff as needed + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }), + ["<CR>"] = cmp.mapping({ + -- https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#safely-select-entries-with-cr + i = function(fallback) + -- In insert mode, only use Enter to complete something if cmp + -- is active and the user has explicitly selected an item + if cmp.visible() and cmp.get_active_entry() then + cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false }) + else + fallback() + end + end, + s = cmp.mapping.confirm({ select = true }), + c = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }), + }), + }), + sorting = { + priority_weight = 2, + comparators = { + cmp.config.compare.exact, + + -- {{ if eq .enable_nvim_copilot true -}} + -- Custom comparitor from copilot_cmp + require("copilot_cmp.comparators").prioritize, + -- {{ end -}} + + -- Mostly default sort order from cmp + cmp.config.compare.offset, + -- cmp.config.compare.scopes, --this is commented in nvim-cmp too + cmp.config.compare.score, + cmp.config.compare.recently_used, + cmp.config.compare.locality, + cmp.config.compare.kind, + cmp.config.compare.sort_text, + cmp.config.compare.length, + cmp.config.compare.order, + }, + }, + sources = { + -- LSP results should be first priority + { name = "nvim_lsp", group_index = 1 }, + + { name = "nvim_lsp_signature_help", group_index = 2 }, + { name = "luasnip", group_index = 2 }, + -- {{ if eq .enable_nvim_copilot true }} + { name = "copilot", group_index = 2 }, + -- {{ end }} + { name = "path", group_index = 2 }, + { name = "buffer", group_index = 2, keyword_length = 5 }, + }, + -- sources = cmp.config.sources({ + -- { name = "nvim_lsp_signature_help" }, + -- { name = "nvim_lsp" }, + -- { name = "luasnip" }, + -- }, { + -- { name = "path" }, + -- { name = "buffer", keyword_length = 5 }, + -- }), + formatting = { + format = require("lspkind").cmp_format({ + mode = "symbol_text", + -- maxwidth = 50, + maxwidth = function() + return math.floor(0.45 * vim.o.columns) + end, + ellipsis_char = "...", + show_labelDetails = true, + symbol_map = { + -- {{ if eq .enable_nvim_copilot true }} + Copilot = " ", + -- {{ end }} + }, + }), + }, + experimental = { + ghost_text = { + hl_group = "LspCodeLens", + }, + }, + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered(), + }, + }) + end, +} |
