From 3e85812630c0158d338dfae8c77a32ce61a9cf1e Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 30 Oct 2025 16:58:06 +0100 Subject: [PATCH] fix(nvim): fix autocompletion --- nvim/lua/olinpin/plugins/completion.lua | 58 ++++++++++++++++ nvim/lua/olinpin/plugins/mason.lua | 89 ++++++++++++++----------- 2 files changed, 109 insertions(+), 38 deletions(-) create mode 100644 nvim/lua/olinpin/plugins/completion.lua diff --git a/nvim/lua/olinpin/plugins/completion.lua b/nvim/lua/olinpin/plugins/completion.lua new file mode 100644 index 0000000..11190be --- /dev/null +++ b/nvim/lua/olinpin/plugins/completion.lua @@ -0,0 +1,58 @@ +return { + { + "hrsh7th/nvim-cmp", + event = "InsertEnter", + dependencies = { + "hrsh7th/cmp-nvim-lsp", + "hrsh7th/cmp-buffer", + "hrsh7th/cmp-path", + "L3MON4D3/LuaSnip", + "saadparwaiz1/cmp_luasnip", + }, + config = function() + local cmp = require("cmp") + local luasnip = require("luasnip") + + cmp.setup({ + snippet = { + expand = function(args) + luasnip.lsp_expand(args.body) + end, + }, + mapping = cmp.mapping.preset.insert({ + [""] = cmp.mapping.scroll_docs(-4), + [""] = cmp.mapping.scroll_docs(4), + [""] = cmp.mapping.complete(), + [""] = cmp.mapping.abort(), + [""] = cmp.mapping.confirm({ select = true }), + [""] = cmp.mapping(function(fallback) + if cmp.visible() then + cmp.select_next_item() + elseif luasnip.expand_or_jumpable() then + luasnip.expand_or_jump() + 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" }), + }), + sources = cmp.config.sources({ + { name = "nvim_lsp" }, + { name = "luasnip" }, + }, { + { name = "buffer" }, + { name = "path" }, + }), + }) + end, + }, +} + diff --git a/nvim/lua/olinpin/plugins/mason.lua b/nvim/lua/olinpin/plugins/mason.lua index 685ebeb..ac14c8a 100644 --- a/nvim/lua/olinpin/plugins/mason.lua +++ b/nvim/lua/olinpin/plugins/mason.lua @@ -27,10 +27,10 @@ local PACKAGES = { -- Lint "eslint-lsp", "pylint", - "jq", - "php-cs-fixer", - "intelephense", - "prettier", + "jq", + "php-cs-fixer", + "intelephense", + "prettier", } local function install(pack, version) @@ -117,30 +117,30 @@ end return { { "williamboman/mason.nvim", - init = function() - -- Do not crowd home directory with NPM cache folder - vim.env.npm_config_cache = vim.env.HOME .. "/.cache/npm" - end, - opts = { - ui = { - border = "rounded", - height = 0.85, - width = 0.8, + init = function() + -- Do not crowd home directory with NPM cache folder + vim.env.npm_config_cache = vim.env.HOME .. "/.cache/npm" + end, + opts = { + ui = { + border = "rounded", + height = 0.85, + width = 0.8, + }, }, - }, - config = function(_, opts) - require("mason").setup(opts) + config = function(_, opts) + require("mason").setup(opts) - -- Filter out disabled packages - local packages = {} - for _, package in ipairs(PACKAGES) do - table.insert(packages, package) - end + -- Filter out disabled packages + local packages = {} + for _, package in ipairs(PACKAGES) do + table.insert(packages, package) + end - vim.defer_fn(function() - syncPackages(packages) - end, 3000) - end, + vim.defer_fn(function() + syncPackages(packages) + end, 3000) + end, event = { "VeryLazy" }, }, @@ -148,6 +148,8 @@ return { "williamboman/mason-lspconfig.nvim", dependencies = { "williamboman/mason.nvim" }, config = function() + local capabilities = require("cmp_nvim_lsp").default_capabilities() + local on_attach = function(client, bufnr) local opts = { buffer = bufnr, silent = true } @@ -165,12 +167,13 @@ return { end -- Configure LSP servers using new vim.lsp.config API - vim.lsp.config('lua_ls', { + vim.lsp.config("lua_ls", { on_attach = on_attach, + capabilities = capabilities, settings = { Lua = { - runtime = { version = 'LuaJIT' }, - diagnostics = { globals = { 'vim' } }, + runtime = { version = "LuaJIT" }, + diagnostics = { globals = { "vim" } }, workspace = { library = vim.api.nvim_get_runtime_file("", true), checkThirdParty = false, @@ -180,19 +183,29 @@ return { }, }) - vim.lsp.config('ts_ls', { on_attach = on_attach }) - vim.lsp.config('pyright', { on_attach = on_attach }) - vim.lsp.config('html', { on_attach = on_attach }) - vim.lsp.config('cssls', { on_attach = on_attach }) - vim.lsp.config('jsonls', { on_attach = on_attach }) - vim.lsp.config('yamlls', { on_attach = on_attach }) - vim.lsp.config('intelephense', { on_attach = on_attach }) - vim.lsp.config('eslint', { on_attach = on_attach }) - + vim.lsp.config("ts_ls", { on_attach = on_attach, capabilities = capabilities }) + vim.lsp.config("pyright", { on_attach = on_attach, capabilities = capabilities }) + vim.lsp.config("html", { on_attach = on_attach, capabilities = capabilities }) + vim.lsp.config("cssls", { on_attach = on_attach, capabilities = capabilities }) + vim.lsp.config("jsonls", { on_attach = on_attach, capabilities = capabilities }) + vim.lsp.config("yamlls", { on_attach = on_attach, capabilities = capabilities }) + vim.lsp.config("intelephense", { + on_attach = on_attach, + capabilities = capabilities, + settings = { + intelephense = { + format = { + enable = true, + }, + }, + }, + }) + vim.lsp.config("eslint", { on_attach = on_attach, capabilities = capabilities }) + require("mason-lspconfig").setup({ ensure_installed = { "lua_ls", - "ts_ls", + "ts_ls", "pyright", "html", "cssls",