From 7cefecceede85cc978c01915167a1da5cd432421 Mon Sep 17 00:00:00 2001 From: Raoul Branten Date: Tue, 30 Dec 2025 10:54:06 +0100 Subject: [PATCH] Fix autocomplete not working --- lua/config/plugin/cmp.lua | 39 ++++++------ lua/config/plugin/init.lua | 2 + lua/config/plugin/lsp.lua | 108 ++++++++++++++++++++++++++++++++++ lua/config/plugin/luasnip.lua | 2 + 4 files changed, 131 insertions(+), 20 deletions(-) create mode 100644 lua/config/plugin/lsp.lua create mode 100644 lua/config/plugin/luasnip.lua diff --git a/lua/config/plugin/cmp.lua b/lua/config/plugin/cmp.lua index ac76032..399ee0b 100644 --- a/lua/config/plugin/cmp.lua +++ b/lua/config/plugin/cmp.lua @@ -36,28 +36,27 @@ cmp.setup({ }), sources = cmp.config.sources({ - sources = cmp.config.sources({ - { name = 'nvim_lsp', priority = 1000 }, - { name = 'luasnip', priority = 750 }, - }, { - { name = 'buffer', priority = 500 }, - { name = 'path', priority = 250 }, - }), - sorting = { - comparators = { - cmp.config.compare.offset, - cmp.config.compare.exact, - 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, - }, - }, + { name = 'nvim_lsp', priority = 1000 }, + { name = 'luasnip', priority = 750 }, + }, { + { name = 'buffer', priority = 500 }, + { name = 'path', priority = 250 }, }), + sorting = { + comparators = { + cmp.config.compare.offset, + cmp.config.compare.exact, + 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, + }, + }, + formatting = { format = function(entry, vim_item) vim_item.menu = ({ diff --git a/lua/config/plugin/init.lua b/lua/config/plugin/init.lua index 11b587c..1c2a30e 100644 --- a/lua/config/plugin/init.lua +++ b/lua/config/plugin/init.lua @@ -14,5 +14,7 @@ require("config.plugin.rainbow-delimiters") require("config.plugin.indent-blankline") require("config.plugin.scrollbar") require("config.plugin.lspsaga") +require("config.plugin.lsp") +require("config.plugin.luasnip") require("config.plugin.gruvbox") require("config.plugin.cmp") diff --git a/lua/config/plugin/lsp.lua b/lua/config/plugin/lsp.lua new file mode 100644 index 0000000..b8dc210 --- /dev/null +++ b/lua/config/plugin/lsp.lua @@ -0,0 +1,108 @@ +-- LSP Configuration +local lspconfig = require('lspconfig') + +-- Setup Mason (already done via lazy.nvim, but ensure it's initialized) +require('mason').setup() + +-- Setup LSP capabilities for nvim-cmp +local capabilities = require('cmp_nvim_lsp').default_capabilities() + +-- Function to attach LSP to buffers +local on_attach = function(client, bufnr) + -- Enable completion triggered by + vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') + + -- Mappings for LSP actions + local bufopts = { noremap = true, silent = true, buffer = bufnr } + vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts) + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) + vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts) + vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts) + vim.keymap.set('n', '', vim.lsp.buf.signature_help, bufopts) + vim.keymap.set('n', 'wa', vim.lsp.buf.add_workspace_folder, bufopts) + vim.keymap.set('n', 'wr', vim.lsp.buf.remove_workspace_folder, bufopts) + vim.keymap.set('n', 'wl', function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, bufopts) + vim.keymap.set('n', 'D', vim.lsp.buf.type_definition, bufopts) + vim.keymap.set('n', 'rn', vim.lsp.buf.rename, bufopts) + vim.keymap.set('n', 'ca', vim.lsp.buf.code_action, bufopts) + vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) + vim.keymap.set('n', 'f', function() vim.lsp.buf.format { async = true } end, bufopts) +end + +-- Try to use mason-lspconfig if available, otherwise setup servers manually +local mason_lspconfig_ok, mason_lspconfig = pcall(require, 'mason-lspconfig') +if mason_lspconfig_ok then + -- Setup mason-lspconfig + mason_lspconfig.setup({ + ensure_installed = {}, -- Add LSP servers here as needed + automatic_installation = false, + }) + + -- Setup handlers if the API is available + if mason_lspconfig.setup_handlers then + mason_lspconfig.setup_handlers({ + -- Default handler for all servers + function(server_name) + lspconfig[server_name].setup({ + capabilities = capabilities, + on_attach = on_attach, + }) + end, + }) + end +end + +-- Setup LSP servers manually (uncomment as needed) +-- Install servers via :Mason command in Neovim first + +-- Lua LSP +-- lspconfig.lua_ls.setup({ +-- capabilities = capabilities, +-- on_attach = on_attach, +-- settings = { +-- Lua = { +-- runtime = { version = 'LuaJIT' }, +-- diagnostics = { globals = { 'vim' } }, +-- workspace = { library = vim.api.nvim_get_runtime_file("", true) }, +-- telemetry = { enable = false }, +-- }, +-- }, +-- }) + +-- Python LSP (uncomment when pyright is installed) +-- lspconfig.pyright.setup({ +-- capabilities = capabilities, +-- on_attach = on_attach, +-- }) + +-- Rust LSP (uncomment when rust_analyzer is installed) +-- lspconfig.rust_analyzer.setup({ +-- capabilities = capabilities, +-- on_attach = on_attach, +-- }) + +-- Diagnostic configuration +vim.diagnostic.config({ + virtual_text = true, + signs = true, + update_in_insert = false, + underline = true, + severity_sort = true, + float = { + focusable = false, + style = "minimal", + border = "rounded", + source = "always", + header = "", + prefix = "", + }, +}) + +-- Show diagnostic signs +local signs = { Error = "󰅚 ", Warn = "󰀪 ", Hint = "󰌶 ", Info = "󰋽 " } +for type, icon in pairs(signs) do + local hl = "DiagnosticSign" .. type + vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" }) +end diff --git a/lua/config/plugin/luasnip.lua b/lua/config/plugin/luasnip.lua new file mode 100644 index 0000000..d76cfbd --- /dev/null +++ b/lua/config/plugin/luasnip.lua @@ -0,0 +1,2 @@ +-- LuaSnip configuration +require("luasnip.loaders.from_vscode").lazy_load()