diff options
| author | Aotrix <[email protected]> | 2024-02-24 16:52:50 +0100 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2024-02-24 16:52:50 +0100 |
| commit | 847e9d0fe4c28ae59cd3d5b4e07ee30d558e6048 (patch) | |
| tree | d6b18ebc786b2794bf9bf1410ec151a6e591b828 | |
| parent | 3bd0fd13998b49cc564e57fb8fd10b11b1a20af6 (diff) | |
added autocomplete
| -rw-r--r-- | init.lua | 12 | ||||
| -rw-r--r-- | lazy-lock.json | 3 | ||||
| -rw-r--r-- | lua/autocomplete/autocomplete.lua | 169 |
3 files changed, 96 insertions, 88 deletions
@@ -36,12 +36,12 @@ require('lazy').setup({ 'williamboman/mason-lspconfig.nvim' , -- Optional -- Autocompletion - 'hrsh7th/nvim-cmp' , -- Required - 'hrsh7th/cmp-nvim-lsp' , -- Required - 'hrsh7th/cmp-buffer' , -- Optional - 'hrsh7th/cmp-path' , -- Optional - 'saadparwaiz1/cmp_luasnip' , -- Optional - 'hrsh7th/cmp-nvim-lua' , -- Optional + 'hrsh7th/nvim-cmp', -- Required + 'hrsh7th/cmp-nvim-lsp', -- Required + 'hrsh7th/cmp-buffer', -- Optional + 'hrsh7th/cmp-path', -- Optional + 'saadparwaiz1/cmp_luasnip', -- Optional + 'hrsh7th/cmp-nvim-lua', -- Optional -- Snippets 'L3MON4D3/LuaSnip' , -- Required diff --git a/lazy-lock.json b/lazy-lock.json index 09d589f..ae7bca3 100644 --- a/lazy-lock.json +++ b/lazy-lock.json @@ -8,7 +8,8 @@ "friendly-snippets": { "branch": "main", "commit": "5cc1f45c6aac699ad008fb85f6ae03236062667d" }, "harpoon": { "branch": "master", "commit": "ccae1b9bec717ae284906b0bf83d720e59d12b91" }, "lazy.nvim": { "branch": "main", "commit": "28126922c9b54e35a192ac415788f202c3944c9f" }, - "lsp-zero.nvim": { "branch": "v2.x", "commit": "9a686513eaaa13d737d0fec8956a18268ead8b29" }, + "lsp-zero.nvim": { "branch": "v3.x", "commit": "9a686513eaaa13d737d0fec8956a18268ead8b29" }, + "lspkind.nvim": { "branch": "master", "commit": "1735dd5a5054c1fb7feaf8e8658dbab925f4f0cf" }, "lualine.nvim": { "branch": "master", "commit": "7d131a8d3ba5016229e8a1d08bf8782acea98852" }, "mason-lspconfig.nvim": { "branch": "main", "commit": "0954d7730e749d606ddf8d7ae8846848be435d53" }, "mason.nvim": { "branch": "main", "commit": "c43eeb5614a09dc17c03a7fb49de2e05de203924" }, diff --git a/lua/autocomplete/autocomplete.lua b/lua/autocomplete/autocomplete.lua index ce4f77a..70131bf 100644 --- a/lua/autocomplete/autocomplete.lua +++ b/lua/autocomplete/autocomplete.lua @@ -1,101 +1,108 @@ +-- Add additional capabilities supported by nvim-cmp +local capabilities = require('cmp_nvim_lsp').default_capabilities() + local lspconfig = require('lspconfig') -lspconfig.pyright.setup{} -lspconfig.clangd.setup{} -require("mason").setup() -require("mason-lspconfig").setup() -local lsp_capabilities = require('cmp_nvim_lsp').default_capabilities() -lspconfig.pyright.setup({ - capabilities = lsp_capabilities, -}) -vim.opt.completeopt = {'menu', 'menuone', 'noselect'} -local cmp = require('cmp') -local luasnip = require('luasnip') +-- Enable some language servers with the additional completion capabilities offered by nvim-cmp +local servers = { 'clangd', 'rust_analyzer', 'pyright', 'tsserver' } +for _, lsp in ipairs(servers) do + lspconfig[lsp].setup { + -- on_attach = my_custom_on_attach, + capabilities = capabilities, + } +end -local select_opts = {behavior = cmp.SelectBehavior.Select} +-- luasnip setup +local luasnip = require 'luasnip' -cmp.setup({ +-- nvim-cmp setup +local cmp = require 'cmp' +cmp.setup { snippet = { expand = function(args) luasnip.lsp_expand(args.body) - end - }, - sources = { - {name = 'path'}, - {name = 'nvim_lsp', keyword_length = 1}, - {name = 'buffer', keyword_length = 3}, - {name = 'luasnip', keyword_length = 2}, - }, - window = { - completion = cmp.config.window.bordered(), - documentation = cmp.config.window.bordered() - }, - formatting = { - fields = {'menu', 'abbr', 'kind'}, - format = function(entry, item) - local menu_icon = { - nvim_lsp = 'λ', - luasnip = '⋗', - buffer = 'Ω', - path = '🖫', - } - - item.menu = menu_icon[entry.source.name] - return item end, }, - performance = { - max_view_entries = 7 - }, - mapping = { - ['<Up>'] = cmp.mapping.select_prev_item(select_opts), - ['<Down>'] = cmp.mapping.select_next_item(select_opts), - - ['<C-p>'] = cmp.mapping.select_prev_item(select_opts), - ['<C-n>'] = cmp.mapping.select_next_item(select_opts), - - ['<C-u>'] = cmp.mapping.scroll_docs(-4), - ['<C-d>'] = cmp.mapping.scroll_docs(4), - - ['<C-e>'] = cmp.mapping.abort(), - ['<C-y>'] = cmp.mapping.confirm({select = true}), - ['<CR>'] = cmp.mapping.confirm({select = false}), - - ['<C-f>'] = cmp.mapping(function(fallback) - if luasnip.jumpable(1) then - luasnip.jump(1) - else - fallback() - end - end, {'i', 's'}), - - ['<C-b>'] = cmp.mapping(function(fallback) - if luasnip.jumpable(-1) then - luasnip.jump(-1) - else - fallback() - end - end, {'i', 's'}), - + mapping = cmp.mapping.preset.insert({ + ['<C-d>'] = cmp.mapping.scroll_docs(-4), + ['<C-f>'] = cmp.mapping.scroll_docs(4), + ['<C-Space>'] = cmp.mapping.complete(), + ['<CR>'] = cmp.mapping.confirm { + behavior = cmp.ConfirmBehavior.Replace, + select = true, + }, ['<Tab>'] = cmp.mapping(function(fallback) - local col = vim.fn.col('.') - 1 - if cmp.visible() then - cmp.select_next_item(select_opts) - elseif col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') then - fallback() + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() else - cmp.complete() + fallback() end - end, {'i', 's'}), - + end, { 'i', 's' }), ['<S-Tab>'] = cmp.mapping(function(fallback) if cmp.visible() then - cmp.select_prev_item(select_opts) + cmp.select_prev_item() + elseif luasnip.jumpable(-1) then + luasnip.jump(-1) else fallback() end - end, {'i', 's'}), + end, { 'i', 's' }), + }), + sources = cmp.config.sources({ + { name = 'nvim_lsp' }, + { name = 'luasnip' }, + }), + window = { + completion = cmp.config.window.bordered(), + documentation = cmp.config.window.bordered() }, -}) + formatting = { + fields = { "abbr", "menu", "kind" }, + format = function(entry, item) + -- Define menu shorthand for different completion sources. + local menu_icon = { + nvim_lsp = "LSP", + nvim_lua = "LUA", + luasnip = "SNP", + buffer = "BUFF", + path = "PATH", + } + -- Set the menu "icon" to the shorthand for each completion source. + item.menu = menu_icon[entry.source.name] + + -- Set the fixed width of the completion menu to 60 characters. + -- fixed_width = 20 + + -- Set 'fixed_width' to false if not provided. + fixed_width = fixed_width or false + + -- Get the completion entry text shown in the completion window. + local content = item.abbr + -- Set the fixed completion window width. + if fixed_width then + vim.o.pumwidth = fixed_width + end + + -- Get the width of the current window. + local win_width = vim.api.nvim_win_get_width(0) + + -- Set the max content width based on either: 'fixed_width' + -- or a percentage of the window width, in this case 20%. + -- We subtract 10 from 'fixed_width' to leave room for 'kind' fields. + local max_content_width = fixed_width and fixed_width - 10 or math.floor(win_width * 0.2) + + -- Truncate the completion entry text if it's longer than the + -- max content width. We subtract 3 from the max content width + -- to account for the "..." that will be appended to it. + if #content > max_content_width then + item.abbr = vim.fn.strcharpart(content, 0, max_content_width - 3) .. "..." + else + item.abbr = content .. (" "):rep(max_content_width - #content) + end + return item + end, +}, +} |
