From e7cddfde33e2b1c8ae5d8a36344f27bc2acc1074 Mon Sep 17 00:00:00 2001 From: punkfairie Date: Wed, 1 Jan 2025 18:56:17 -0800 Subject: [PATCH] feat: nvim-lspconfig -> vim.lsp.config() --- nix/neovim-overlay.nix | 3 - nvim/init.lua | 1 + nvim/lsp/lua-language-server.lua | 38 ++++ nvim/lua/lsp.lua | 183 +++++++++++++++++++ nvim/lua/plugins/lsp/init.lua | 5 - nvim/lua/plugins/lsp/nvim-lspconfig.lua | 227 ------------------------ 6 files changed, 222 insertions(+), 235 deletions(-) create mode 100644 nvim/lsp/lua-language-server.lua create mode 100644 nvim/lua/lsp.lua delete mode 100644 nvim/lua/plugins/lsp/init.lua delete mode 100644 nvim/lua/plugins/lsp/nvim-lspconfig.lua diff --git a/nix/neovim-overlay.nix b/nix/neovim-overlay.nix index 00b6e37..98d2646 100644 --- a/nix/neovim-overlay.nix +++ b/nix/neovim-overlay.nix @@ -91,9 +91,6 @@ with final.pkgs.lib; let ] )) - # LSP - nvim-lspconfig - # Coding blink-cmp luasnip diff --git a/nvim/init.lua b/nvim/init.lua index af49fcd..4c18dd7 100644 --- a/nvim/init.lua +++ b/nvim/init.lua @@ -12,6 +12,7 @@ require('keymaps') require('autocmds') require('snacks-nvim') +require('lsp') require('lz.n').load('plugins') diff --git a/nvim/lsp/lua-language-server.lua b/nvim/lsp/lua-language-server.lua new file mode 100644 index 0000000..071b543 --- /dev/null +++ b/nvim/lsp/lua-language-server.lua @@ -0,0 +1,38 @@ +return { + filetypes = { 'lua' }, + cmd = { 'lua-language-server' }, + root_markers = { + '.luarc.json', + '.luarc.jsonc', + '.luacheckrc', + '.stylua.toml', + 'stylua.toml', + 'selene.toml', + 'selene.yml', + }, + + settings = { + Lua = { + workspace = { + checkThirdParty = false, + }, + codeLens = { + enable = true, + }, + completion = { + callSnippet = 'Replace', + }, + doc = { + privateName = { '^_' }, + }, + hint = { + enable = true, + setType = false, + paramType = true, + paramName = 'Disable', + semicolon = 'Disable', + arrayIndex = 'Disable', + }, + }, + }, +} diff --git a/nvim/lua/lsp.lua b/nvim/lua/lsp.lua new file mode 100644 index 0000000..8c7f8f2 --- /dev/null +++ b/nvim/lua/lsp.lua @@ -0,0 +1,183 @@ +local i = require('icons') + +-- Diagnostics -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +---@type vim.diagnostic.Opts +local diagnosticConfig = { + underline = true, + update_in_insert = true, + virtual_text = { + spacing = 4, + source = 'if_many', + prefix = function(diagnostic) + for d, icon in pairs(i.diagnostics) do + if diagnostic.severity == vim.diagnostic.severity[d:upper()] then + return icon + end + end + + return '' + end, + }, + severity_sort = true, + signs = { + text = { + [vim.diagnostic.severity.ERROR] = i.diagnostics.Error, + [vim.diagnostic.severity.WARN] = i.diagnostics.Warn, + [vim.diagnostic.severity.HINT] = i.diagnostics.Hint, + [vim.diagnostic.severity.INFO] = i.diagnostics.Info, + }, + }, +} + +for severity, icon in pairs(diagnosticConfig.signs.text) do + local name = + vim.diagnostic.severity[severity]:lower():gsub('^%l', string.upper) + name = 'DiagnosticSign' .. name + + vim.fn.sign_define(name, { text = icon, texthl = name, numhl = '' }) +end + +vim.diagnostic.config(diagnosticConfig) + +-- Codelens -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +vim.api.nvim_create_autocmd('User', { + pattern = 'LspSupportsMethod', + callback = function(args) + local client = vim.lsp.get_client_by_id(args.data.client_id) + local buffer = args.data.buffer + + if client and args.data.method == 'textDocument/codeLens' then + vim.lsp.codelens.refresh() + + vim.api.nvim_create_autocmd({ 'BufEnter', 'CursorHold', 'InsertLeave' }, { + buffer = buffer, + callback = vim.lsp.codelens.refresh, + }) + end + end, +}) + +-- Server Setup -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +local has_blink, blink = pcall(require, 'blink.cmp') +local capabilities = vim.tbl_deep_extend( + 'force', + {}, + vim.lsp.protocol.make_client_capabilities(), + has_blink and blink.get_lsp_capabilities() or {}, + { + workspace = { + fileOperations = { + didRename = true, + willRename = true, + }, + }, + } +) + +vim.lsp.config('*', { + root_markers = { '.git' }, + capabilities = capabilities, +}) + +vim.lsp.enable({ 'lua-language-server' }) + +-- Keymaps -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- +vim.api.nvim_create_autocmd('LspAttach', { + callback = function(args) + local client = vim.lsp.get_client_by_id(args.data.client_id) + + if not client then + return + end + + local set = vim.keymap.set + + set('n', 'gD', vim.lsp.buf.declaration, { desc = 'declaration' }) + set('n', 'gI', vim.lsp.buf.implementation, { desc = 'implementation' }) + + set( + 'n', + 'gr', + vim.lsp.buf.references, + { desc = 'references', nowait = true } + ) + + set('n', 'gy', vim.lsp.buf.type_definition, { desc = 'type definition' }) + + set('n', 'K', function() + return vim.lsp.buf.hover() + end, { desc = 'hover' }) + + if client.supports_method('textDocument/codeAction') then + set( + { 'n', 'v' }, + 'ca', + vim.lsp.buf.code_action, + { desc = 'code action' } + ) + end + + if client.supports_method('textDocument/codeLens') then + set( + { 'n', 'v' }, + 'cc', + vim.lsp.codelens.run, + { desc = 'run codelens' } + ) + + set( + 'n', + 'cC', + vim.lsp.codelens.refresh, + { desc = 'refresh & display codelens' } + ) + end + + if client.supports_method('textDocument/definition') then + set('n', 'gd', vim.lsp.buf.definition, { desc = 'definition' }) + end + + if client.supports_method('textDocument/documentHighlight') then + if Snacks.words.is_enabled() then + set('n', '', function() + Snacks.words.jump(vim.v.count1, true) + end, { desc = 'next reference' }) + + set('n', '', function() + Snacks.words.jump(-vim.v.count1, true) + end, { desc = 'prev reference' }) + + set('n', ']]', function() + Snacks.words.jump(vim.v.count1) + end, { desc = 'next reference' }) + + set('n', '[[', function() + Snacks.words.jump(-vim.v.count1) + end, { desc = 'prev reference' }) + end + end + + if client.supports_method('textDocument/rename') then + set('n', 'cr', vim.lsp.buf.rename, { desc = 'rename' }) + end + + if client.supports_method('textDocument/signatureHelp') then + set('i', '', function() + return vim.lsp.buf.signature_help() + end, { desc = 'signature help' }) + + set('n', 'gK', function() + vim.lsp.buf.signature_help() + end, { desc = 'signature help' }) + end + + if + client.supports_method('workspace/didRenameFiles') + or client.supports_method('workspace/willRenameFiles') + then + set('n', 'cR', function() + Snacks.rename.rename_file() + end, { desc = 'rename file' }) + end + end, +}) diff --git a/nvim/lua/plugins/lsp/init.lua b/nvim/lua/plugins/lsp/init.lua deleted file mode 100644 index 3b5d41b..0000000 --- a/nvim/lua/plugins/lsp/init.lua +++ /dev/null @@ -1,5 +0,0 @@ -local req = MarleyVim.local_require('plugins.lsp') - -return { - req('nvim-lspconfig'), -} diff --git a/nvim/lua/plugins/lsp/nvim-lspconfig.lua b/nvim/lua/plugins/lsp/nvim-lspconfig.lua deleted file mode 100644 index 079b00b..0000000 --- a/nvim/lua/plugins/lsp/nvim-lspconfig.lua +++ /dev/null @@ -1,227 +0,0 @@ -return { - 'nvim-lspconfig', - event = { 'BufRead', 'BufWinEnter', 'BufNewFile' }, - cmd = { 'LspStart', 'LspRestart', 'LspStop', 'LspLog' }, - after = function() - local i = require('icons') - - -- Diagnostics -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ---@type vim.diagnostic.Opts - local diagnosticConfig = { - underline = true, - update_in_insert = true, - virtual_text = { - spacing = 4, - source = 'if_many', - prefix = function(diagnostic) - for d, icon in pairs(i.diagnostics) do - if diagnostic.severity == vim.diagnostic.severity[d:upper()] then - return icon - end - end - - return '' - end, - }, - severity_sort = true, - signs = { - text = { - [vim.diagnostic.severity.ERROR] = i.diagnostics.Error, - [vim.diagnostic.severity.WARN] = i.diagnostics.Warn, - [vim.diagnostic.severity.HINT] = i.diagnostics.Hint, - [vim.diagnostic.severity.INFO] = i.diagnostics.Info, - }, - }, - } - - for severity, icon in pairs(diagnosticConfig.signs.text) do - local name = - vim.diagnostic.severity[severity]:lower():gsub('^%l', string.upper) - name = 'DiagnosticSign' .. name - - vim.fn.sign_define(name, { text = icon, texthl = name, numhl = '' }) - end - - vim.diagnostic.config(diagnosticConfig) - - -- Codelens -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - vim.api.nvim_create_autocmd('User', { - pattern = 'LspSupportsMethod', - callback = function(args) - local client = vim.lsp.get_client_by_id(args.data.client_id) - local buffer = args.data.buffer - - if client and args.data.method == 'textDocument/codeLens' then - vim.lsp.codelens.refresh() - - vim.api.nvim_create_autocmd( - { 'BufEnter', 'CursorHold', 'InsertLeave' }, - { - buffer = buffer, - callback = vim.lsp.codelens.refresh, - } - ) - end - end, - }) - - -- Server Setup -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - local has_blink, blink = pcall(require, 'blink.cmp') - local capabilities = vim.tbl_deep_extend( - 'force', - {}, - vim.lsp.protocol.make_client_capabilities(), - has_blink and blink.get_lsp_capabilities() or {}, - { - workspace = { - fileOperations = { - didRename = true, - willRename = true, - }, - }, - } - ) - - local lspconfig = require('lspconfig') - - lspconfig.util.default_config = vim.tbl_extend( - 'force', - lspconfig.util.default_config, - { capabilities = capabilities } - ) - - lspconfig.lua_ls.setup({ - settings = { - Lua = { - workspace = { - checkThirdParty = false, - }, - codeLens = { - enable = true, - }, - completion = { - callSnippet = 'Replace', - }, - doc = { - privateName = { '^_' }, - }, - hint = { - enable = true, - setType = false, - paramType = true, - paramName = 'Disable', - semicolon = 'Disable', - arrayIndex = 'Disable', - }, - }, - }, - }) - - -- Keymaps -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - vim.api.nvim_create_autocmd('LspAttach', { - callback = function(args) - local client = vim.lsp.get_client_by_id(args.data.client_id) - - if not client then - return - end - - local set = vim.keymap.set - - set('n', 'cl', 'LspInfo', { desc = 'lsp info' }) - set('n', 'gD', vim.lsp.buf.declaration, { desc = 'declaration' }) - set('n', 'gI', vim.lsp.buf.implementation, { desc = 'implementation' }) - - set( - 'n', - 'gr', - vim.lsp.buf.references, - { desc = 'references', nowait = true } - ) - - set( - 'n', - 'gy', - vim.lsp.buf.type_definition, - { desc = 'type definition' } - ) - - set('n', 'K', function() - return vim.lsp.buf.hover() - end, { desc = 'hover' }) - - if client.supports_method('textDocument/codeAction') then - set( - { 'n', 'v' }, - 'ca', - vim.lsp.buf.code_action, - { desc = 'code action' } - ) - end - - if client.supports_method('textDocument/codeLens') then - set( - { 'n', 'v' }, - 'cc', - vim.lsp.codelens.run, - { desc = 'run codelens' } - ) - - set( - 'n', - 'cC', - vim.lsp.codelens.refresh, - { desc = 'refresh & display codelens' } - ) - end - - if client.supports_method('textDocument/definition') then - set('n', 'gd', vim.lsp.buf.definition, { desc = 'definition' }) - end - - if client.supports_method('textDocument/documentHighlight') then - if Snacks.words.is_enabled() then - set('n', '', function() - Snacks.words.jump(vim.v.count1, true) - end, { desc = 'next reference' }) - - set('n', '', function() - Snacks.words.jump(-vim.v.count1, true) - end, { desc = 'prev reference' }) - - set('n', ']]', function() - Snacks.words.jump(vim.v.count1) - end, { desc = 'next reference' }) - - set('n', '[[', function() - Snacks.words.jump(-vim.v.count1) - end, { desc = 'prev reference' }) - end - end - - if client.supports_method('textDocument/rename') then - set('n', 'cr', vim.lsp.buf.rename, { desc = 'rename' }) - end - - if client.supports_method('textDocument/signatureHelp') then - set('i', '', function() - return vim.lsp.buf.signature_help() - end, { desc = 'signature help' }) - - set('n', 'gK', function() - vim.lsp.buf.signature_help() - end, { desc = 'signature help' }) - end - - if - client.supports_method('workspace/didRenameFiles') - or client.supports_method('workspace/willRenameFiles') - then - set('n', 'cR', function() - Snacks.rename.rename_file() - end, { desc = 'rename file' }) - end - end, - }) - end, -}