From e68d4e3a24d639c0ba52924fc993785261787ae1 Mon Sep 17 00:00:00 2001 From: goncrust Date: Sun, 3 Dec 2023 03:31:45 +0000 Subject: [PATCH] [NVIM] autocompletion --- .config/nvim/lua/goncrust/packer.lua | 43 +++-- .../nvim/lua/goncrust/plugins/lsp-config.lua | 151 +++++++++++++++++- .config/nvim/plugin/packer_compiled.lua | 55 +++++++ 3 files changed, 230 insertions(+), 19 deletions(-) diff --git a/.config/nvim/lua/goncrust/packer.lua b/.config/nvim/lua/goncrust/packer.lua index 8f9c3b8..f52fb9d 100644 --- a/.config/nvim/lua/goncrust/packer.lua +++ b/.config/nvim/lua/goncrust/packer.lua @@ -4,24 +4,39 @@ vim.cmd([[packadd packer.nvim]]) return require('packer').startup(function(use) - -- Packer can manage itself - use("wbthomason/packer.nvim") + -- Packer can manage itself + use("wbthomason/packer.nvim") - use("joshdick/onedark.vim") + use("joshdick/onedark.vim") - use("nvim-treesitter/nvim-treesitter", {run = ':TSUpdate'}) + use("nvim-treesitter/nvim-treesitter", { run = ':TSUpdate' }) - use("nvim-tree/nvim-tree.lua") - use("nvim-tree/nvim-web-devicons") + use("nvim-tree/nvim-tree.lua") + use("nvim-tree/nvim-web-devicons") - use("mhinz/vim-startify") + use("mhinz/vim-startify") - use("vim-airline/vim-airline") - use("vim-airline/vim-airline-themes") + use("vim-airline/vim-airline") + use("vim-airline/vim-airline-themes") - use { - "williamboman/mason.nvim", - "williamboman/mason-lspconfig.nvim", - "neovim/nvim-lspconfig" - } + use("williamboman/mason.nvim") + use("williamboman/mason-lspconfig.nvim") + use("neovim/nvim-lspconfig") + + use("hrsh7th/nvim-cmp") + use("hrsh7th/cmp-buffer") + use("hrsh7th/cmp-path") + use("hrsh7th/cmp-cmdline") + use("hrsh7th/cmp-nvim-lsp") + + use("L3MON4D3/LuaSnip") + use("saadparwaiz1/cmp_luasnip") + use("rafamadriz/friendly-snippets") + + use({ + "windwp/nvim-autopairs", + config = function() require("nvim-autopairs").setup {} end + }) + + use("hrsh7th/cmp-nvim-lsp-signature-help") end) diff --git a/.config/nvim/lua/goncrust/plugins/lsp-config.lua b/.config/nvim/lua/goncrust/plugins/lsp-config.lua index eeab3a6..09a4396 100644 --- a/.config/nvim/lua/goncrust/plugins/lsp-config.lua +++ b/.config/nvim/lua/goncrust/plugins/lsp-config.lua @@ -67,8 +67,149 @@ vim.api.nvim_create_autocmd('LspAttach', { -- format on save vim.api.nvim_create_autocmd("BufWritePre", { - buffer = buffer, - callback = function() - vim.lsp.buf.format { async = false } - end - }) + buffer = buffer, + callback = function() + vim.lsp.buf.format { async = false } + end +}) + +------------------- cmp ------------------ +local cmp_status_ok, cmp = pcall(require, "cmp") +if not cmp_status_ok then + return +end + +local snip_status_ok, luasnip = pcall(require, "luasnip") +if not snip_status_ok then + return +end + +require("luasnip/loaders/from_vscode").lazy_load() + +local check_backspace = function() + local col = vim.fn.col "." - 1 + return col == 0 or vim.fn.getline("."):sub(col, col):match "%s" +end + +local kind_icons = { + Text = "󰉿", + Method = "󰆧", + Function = "󰊕", + Constructor = "", + Field = " ", + Variable = "󰀫", + Class = "󰠱", + Interface = "", + Module = "", + Property = "󰜢", + Unit = "󰑭", + Value = "󰎠", + Enum = "", + Keyword = "󰌋", + Snippet = "", + Color = "󰏘", + File = "󰈙", + Reference = "", + Folder = "󰉋", + EnumMember = "", + Constant = "󰏿", + Struct = "", + Event = "", + Operator = "󰆕", + TypeParameter = " ", + Misc = " ", +} +-- find more here: https://www.nerdfonts.com/cheat-sheet + +cmp.setup { + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) -- For `luasnip` users. + end, + }, + mapping = { + [""] = cmp.mapping.select_prev_item(), + [""] = cmp.mapping.select_next_item(), + [""] = cmp.mapping(cmp.mapping.scroll_docs(-1), { "i", "c" }), + [""] = cmp.mapping(cmp.mapping.scroll_docs(1), { "i", "c" }), + [""] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }), + [""] = cmp.config.disable, -- Specify `cmp.config.disable` if you want to remove the default `` mapping. + [""] = cmp.mapping { + i = cmp.mapping.abort(), + c = cmp.mapping.close(), + }, + -- Accept currently selected item. If none selected, `select` first item. + -- Set `select` to `false` to only confirm explicitly selected items. + [""] = cmp.mapping.confirm { select = true }, + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expandable() then + luasnip.expand() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + elseif check_backspace() then + fallback() + else + fallback() + end + end, { + "i", + "s", + }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) + else + fallback() + end + end, { + "i", + "s", + }), + }, + formatting = { + fields = { "kind", "abbr", "menu" }, + format = function(entry, vim_item) + -- Kind icons + vim_item.kind = string.format("%s", kind_icons[vim_item.kind]) + -- vim_item.kind = string.format('%s %s', kind_icons[vim_item.kind], vim_item.kind) -- This concatonates the icons with the name of the item kind + vim_item.menu = ({ + nvim_lsp = "[LSP]", + luasnip = "[Snippet]", + buffer = "[Buffer]", + path = "[Path]", + })[entry.source.name] + return vim_item + end, + }, + sources = { + { name = "nvim_lsp" }, + { name = "luasnip" }, + { name = "buffer" }, + { name = "path" }, + { name = 'nvim_lsp_signature_help' } + }, + confirm_opts = { + behavior = cmp.ConfirmBehavior.Replace, + select = false, + }, + window = { + documentation = { + border = { "╭", "─", "╮", "│", "╯", "─", "╰", "│" }, + }, + }, + experimental = { + ghost_text = false, + native_menu = false, + }, +} + +-- autopairs integration +local cmp_autopairs = require('nvim-autopairs.completion.cmp') +cmp.event:on( + 'confirm_done', + cmp_autopairs.on_confirm_done() +) diff --git a/.config/nvim/plugin/packer_compiled.lua b/.config/nvim/plugin/packer_compiled.lua index 7e9fcf1..a8907dc 100644 --- a/.config/nvim/plugin/packer_compiled.lua +++ b/.config/nvim/plugin/packer_compiled.lua @@ -74,6 +74,46 @@ end time([[try_loadstring definition]], false) time([[Defining packer_plugins]], true) _G.packer_plugins = { + LuaSnip = { + loaded = true, + path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/LuaSnip", + url = "https://github.com/L3MON4D3/LuaSnip" + }, + ["cmp-buffer"] = { + loaded = true, + path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/cmp-buffer", + url = "https://github.com/hrsh7th/cmp-buffer" + }, + ["cmp-cmdline"] = { + loaded = true, + path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/cmp-cmdline", + url = "https://github.com/hrsh7th/cmp-cmdline" + }, + ["cmp-nvim-lsp"] = { + loaded = true, + path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/cmp-nvim-lsp", + url = "https://github.com/hrsh7th/cmp-nvim-lsp" + }, + ["cmp-nvim-lsp-signature-help"] = { + loaded = true, + path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/cmp-nvim-lsp-signature-help", + url = "https://github.com/hrsh7th/cmp-nvim-lsp-signature-help" + }, + ["cmp-path"] = { + loaded = true, + path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/cmp-path", + url = "https://github.com/hrsh7th/cmp-path" + }, + cmp_luasnip = { + loaded = true, + path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/cmp_luasnip", + url = "https://github.com/saadparwaiz1/cmp_luasnip" + }, + ["friendly-snippets"] = { + loaded = true, + path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/friendly-snippets", + url = "https://github.com/rafamadriz/friendly-snippets" + }, ["mason-lspconfig.nvim"] = { loaded = true, path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/mason-lspconfig.nvim", @@ -84,6 +124,17 @@ _G.packer_plugins = { path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/mason.nvim", url = "https://github.com/williamboman/mason.nvim" }, + ["nvim-autopairs"] = { + config = { "\27LJ\2\n@\0\0\3\0\3\0\a6\0\0\0'\2\1\0B\0\2\0029\0\2\0004\2\0\0B\0\2\1K\0\1\0\nsetup\19nvim-autopairs\frequire\0" }, + loaded = true, + path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/nvim-autopairs", + url = "https://github.com/windwp/nvim-autopairs" + }, + ["nvim-cmp"] = { + loaded = true, + path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/nvim-cmp", + url = "https://github.com/hrsh7th/nvim-cmp" + }, ["nvim-lspconfig"] = { loaded = true, path = "/home/goncrust/.local/share/nvim/site/pack/packer/start/nvim-lspconfig", @@ -132,6 +183,10 @@ _G.packer_plugins = { } time([[Defining packer_plugins]], false) +-- Config for: nvim-autopairs +time([[Config for nvim-autopairs]], true) +try_loadstring("\27LJ\2\n@\0\0\3\0\3\0\a6\0\0\0'\2\1\0B\0\2\0029\0\2\0004\2\0\0B\0\2\1K\0\1\0\nsetup\19nvim-autopairs\frequire\0", "config", "nvim-autopairs") +time([[Config for nvim-autopairs]], false) _G._packer.inside_compile = false if _G._packer.needs_bufread == true then