From 8a876daf74a7d409928f6dd6ce66b76c18308018 Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Thu, 4 Apr 2024 23:19:41 +0200 Subject: [PATCH] nvim: move some config stuff out of plugins directory --- nvim/lua/config/plugins/cmp.lua | 81 +++++++++++++++++++++++++++++++ nvim/lua/config/plugins/lsp.lua | 65 +++++++++++++++++++++++++ nvim/lua/plugins/ide/cmp.lua | 84 +-------------------------------- nvim/lua/plugins/ide/lsp.lua | 63 +------------------------ 4 files changed, 148 insertions(+), 145 deletions(-) create mode 100644 nvim/lua/config/plugins/cmp.lua create mode 100644 nvim/lua/config/plugins/lsp.lua diff --git a/nvim/lua/config/plugins/cmp.lua b/nvim/lua/config/plugins/cmp.lua new file mode 100644 index 0000000..2c16695 --- /dev/null +++ b/nvim/lua/config/plugins/cmp.lua @@ -0,0 +1,81 @@ +local icons = require('config.icons') + +return function () + local lspkind_config = { + mode = 'symbol', + preset = 'codicons', + symbol_map = icons.symbols, + maxwidth = 40, + ellipsis_char = "...", + } + local cmp = require('cmp') + + local selectPrev = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }) + local selectNext = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }) + + local borderstyle = { + border = "none", + winhighlight = 'Normal:Pmenu,FloatBorder:FloatBorder,CursorLine:PmenuSel,Search:None', + } + + return { + completion = { + completeopt = "menu,menuone,noinsert", + }, + -- enabled = function() + -- if require"cmp.config.context".in_treesitter_capture("comment")==true or require"cmp.config.context".in_syntax_group("Comment") then + -- return false + -- else + -- return true + -- end + -- end, + -- preselect = false, + view = { + entries = { name = 'custom', selection_order = 'near_cursor' }, + }, + window = { + documentation = borderstyle, + completion = borderstyle + }, + mapping = { + [""] = selectPrev, + [""] = selectPrev, + [""] = selectNext, + [""] = selectNext, + [""] = cmp.mapping.abort(), + [''] = cmp.mapping.confirm({ select = true }), + }, + snippet = { + expand = function(args) + require('luasnip').lsp_expand(args.body) + end, + }, + formatting = { + fields = { "abbr", "kind", "menu" }, + format = function(entry, vim_item) + if vim.tbl_contains({ 'path' }, entry.source.name) then + local icon, hl_group = require('nvim-web-devicons').get_icon(entry:get_completion_item().label) + if icon then + vim_item.kind = icon + vim_item.kind_hl_group = hl_group + return vim_item + end + end + + return require('lspkind').cmp_format(lspkind_config)(entry, vim_item) + end + }, + sources = { + { name = "copilot" }, + { name = 'nvim_lsp' }, + { name = 'buffer' }, + { name = 'path' }, + { name = 'luasnip' } + }, + experimental = { + ghost_text = { + hl_group = "NonText", + }, + }, + } +end diff --git a/nvim/lua/config/plugins/lsp.lua b/nvim/lua/config/plugins/lsp.lua new file mode 100644 index 0000000..3343c6b --- /dev/null +++ b/nvim/lua/config/plugins/lsp.lua @@ -0,0 +1,65 @@ +return { + mappings = { + ["rs"] = { vim.lsp.buf.rename, { desc = "Rename symbol" }}, + ["ca"] = { vim.lsp.buf.code_action, { desc = "Code action" }}, + ["ff"] = { vim.lsp.buf.format, { desc = "Format file" }}, + gd = { vim.lsp.buf.definition, { desc = "Goto definition" }}, + gi = { vim.lsp.buf.implementation, { desc = "Goto implementation"}}, + gr = { vim.lsp.buf.references, { desc = "Goto references"}}, + }, + servers = { + -- PHP + phpactor = { + settings = { + init_options = { + ["language_server_phpstan.enabled"] = true, + ["language_server_psalm.enabled"] = false, + } + } + }, + -- GO + gopls = { + -- format_on_save = true, + on_save = function () + local params = vim.lsp.util.make_range_params() + params.context = {only = {"source.organizeImports"}} + local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 1000) + for cid, res in pairs(result or {}) do + for _, r in pairs(res.result or {}) do + if r.edit then + local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16" + vim.lsp.util.apply_workspace_edit(r.edit, enc) + end + end + end + vim.lsp.buf.format({async = false}) + end, + settings = { + gopls = { + analyses = { + unusedvariable = true, + unusedwrite = true, + useany = true + }, + gofumpt = true, + }, + }, + }, + -- Lua + lua_ls = { + settings = { + Lua = { + runtime = { + version = 'LuaJIT' + }, + workspace = { + checkThirdParty = false, + library = { + vim.env.VIMRUNTIME + } + } + }, + }, + }, + }, +} diff --git a/nvim/lua/plugins/ide/cmp.lua b/nvim/lua/plugins/ide/cmp.lua index 5edf5b8..a4eefc6 100644 --- a/nvim/lua/plugins/ide/cmp.lua +++ b/nvim/lua/plugins/ide/cmp.lua @@ -1,5 +1,3 @@ -local icons = require('config.icons') - return { 'hrsh7th/nvim-cmp', version = false, @@ -11,87 +9,7 @@ return { 'L3MON4D3/LuaSnip', 'onsails/lspkind-nvim', }, - opts = function() - local lspkind_config = { - mode = 'symbol', - preset = 'codicons', - symbol_map = icons.symbols, - maxwidth = 40, - ellipsis_char = "...", - } - - local cmp = require('cmp') - vim.api.nvim_set_hl(0, "CmpGhostText", { link = "NonText", default = true }) - - local selectPrev = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }) - local selectNext = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }) - - local borderstyle = { - border = "none", - winhighlight = 'Normal:Pmenu,FloatBorder:FloatBorder,CursorLine:PmenuSel,Search:None', - } - - return { - completion = { - completeopt = "menu,menuone,noinsert", - }, - -- enabled = function() - -- if require"cmp.config.context".in_treesitter_capture("comment")==true or require"cmp.config.context".in_syntax_group("Comment") then - -- return false - -- else - -- return true - -- end - -- end, - -- preselect = false, - view = { - entries = { name = 'custom', selection_order = 'near_cursor' }, - }, - window = { - documentation = borderstyle, - completion = borderstyle - }, - mapping = { - [""] = selectPrev, - [""] = selectPrev, - [""] = selectNext, - [""] = selectNext, - [""] = cmp.mapping.abort(), - [''] = cmp.mapping.confirm({ select = true }), - }, - snippet = { - expand = function(args) - require('luasnip').lsp_expand(args.body) - end, - }, - formatting = { - fields = { "abbr", "kind", "menu" }, - format = function(entry, vim_item) - if vim.tbl_contains({ 'path' }, entry.source.name) then - local icon, hl_group = require('nvim-web-devicons').get_icon(entry:get_completion_item().label) - if icon then - vim_item.kind = icon - vim_item.kind_hl_group = hl_group - return vim_item - end - end - - return require('lspkind').cmp_format(lspkind_config)(entry, vim_item) - end - }, - sources = { - { name = "copilot" }, - { name = 'nvim_lsp' }, - { name = 'buffer' }, - { name = 'path' }, - { name = 'luasnip' } - }, - experimental = { - ghost_text = { - hl_group = "CmpGhostText", - }, - }, - } - end, + opts = require('config.plugins.cmp'), config = function(_, opts) local cmp = require('cmp') diff --git a/nvim/lua/plugins/ide/lsp.lua b/nvim/lua/plugins/ide/lsp.lua index 7b47bcc..a1f62d4 100644 --- a/nvim/lua/plugins/ide/lsp.lua +++ b/nvim/lua/plugins/ide/lsp.lua @@ -5,68 +5,7 @@ return { 'williamboman/mason-lspconfig.nvim', 'hrsh7th/cmp-nvim-lsp', }, - opts = { - mappings = { - ["rs"] = { vim.lsp.buf.rename, { desc = "Rename symbol" }}, - ["ca"] = { vim.lsp.buf.code_action, { desc = "Code action" }}, - ["ff"] = { vim.lsp.buf.format, { desc = "Format file" }}, - gd = { vim.lsp.buf.definition, { desc = "Goto definition" }}, - gi = { vim.lsp.buf.implementation, { desc = "Goto implementation"}}, - gr = { vim.lsp.buf.references, { desc = "Goto references"}}, - }, - servers = { - phpactor = { - settings = { - init_options = { - ["language_server_phpstan.enabled"] = true, - ["language_server_psalm.enabled"] = false, - } - } - }, - gopls = { - -- format_on_save = true, - on_save = function () - local params = vim.lsp.util.make_range_params() - params.context = {only = {"source.organizeImports"}} - local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction", params, 1000) - for cid, res in pairs(result or {}) do - for _, r in pairs(res.result or {}) do - if r.edit then - local enc = (vim.lsp.get_client_by_id(cid) or {}).offset_encoding or "utf-16" - vim.lsp.util.apply_workspace_edit(r.edit, enc) - end - end - end - vim.lsp.buf.format({async = false}) - end, - settings = { - gopls = { - analyses = { - unusedvariable = true, - unusedwrite = true, - useany = true - }, - gofumpt = true, - }, - }, - }, - lua_ls = { - settings = { - Lua = { - runtime = { - version = 'LuaJIT' - }, - workspace = { - checkThirdParty = false, - library = { - vim.env.VIMRUNTIME - } - } - }, - }, - }, - }, - }, + opts = require('config.plugins.lsp'), config = function (_, opts) -- Setup Mason to automatically install LSP servers require('mason').setup()