summaryrefslogtreecommitdiff
path: root/lua/autocomplete/autocomplete.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/autocomplete/autocomplete.lua')
-rw-r--r--lua/autocomplete/autocomplete.lua169
1 files changed, 88 insertions, 81 deletions
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,
+},
+}