From f2792252017b3362274aed489284cd092e8c9235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Hamal=20Dvo=C5=99=C3=A1k?= Date: Thu, 24 Apr 2025 11:38:26 +0200 Subject: [PATCH] Migrate to lspconfig --- .gitmodules | 3 + bundle/nvim-lspconfig | 1 + lua/config/format.lua | 14 ---- lua/config/init.lua | 3 - lua/config/lsp/clangd.lua | 29 -------- lua/config/lsp/init.lua | 68 ----------------- lua/config/lsp/pylsp.lua | 43 ----------- lua/config/lsp/pyright.lua | 28 ------- lua/config/lsp/ruff.lua | 28 ------- lua/config/lsp/rust_analyzer.lua | 44 ----------- lua/config/lsp/typescript.lua | 27 ------- lua/config/lsp/vls.lua | 41 ----------- lua/config/tab.lua | 14 ---- lua/init.lua | 122 ++++++++++++++++++++++++++++--- 14 files changed, 116 insertions(+), 349 deletions(-) create mode 160000 bundle/nvim-lspconfig delete mode 100644 lua/config/format.lua delete mode 100644 lua/config/init.lua delete mode 100644 lua/config/lsp/clangd.lua delete mode 100644 lua/config/lsp/init.lua delete mode 100644 lua/config/lsp/pylsp.lua delete mode 100644 lua/config/lsp/pyright.lua delete mode 100644 lua/config/lsp/ruff.lua delete mode 100644 lua/config/lsp/rust_analyzer.lua delete mode 100644 lua/config/lsp/typescript.lua delete mode 100644 lua/config/lsp/vls.lua delete mode 100644 lua/config/tab.lua diff --git a/.gitmodules b/.gitmodules index ab5c1c9..caba269 100644 --- a/.gitmodules +++ b/.gitmodules @@ -19,3 +19,6 @@ [submodule "bundle/nvim-format-buffer"] path = bundle/nvim-format-buffer url = https://github.com/acro5piano/nvim-format-buffer.git +[submodule "bundle/nvim-lspconfig"] + path = bundle/nvim-lspconfig + url = https://github.com/neovim/nvim-lspconfig.git diff --git a/bundle/nvim-lspconfig b/bundle/nvim-lspconfig new file mode 160000 index 0000000..8b0f47d --- /dev/null +++ b/bundle/nvim-lspconfig @@ -0,0 +1 @@ +Subproject commit 8b0f47d851ee5343d38fe194a06ad16b9b9bd086 diff --git a/lua/config/format.lua b/lua/config/format.lua deleted file mode 100644 index 0ee6aef..0000000 --- a/lua/config/format.lua +++ /dev/null @@ -1,14 +0,0 @@ -require('nvim-format-buffer').setup({ - verbose = false, - format_rules = { - { pattern = { '*.c', '*.h' }, command = 'clang-format' }, - { - pattern = { '*.js', '*.mjs', '*.ts', '*.mts', '*.cjs', '*.tsx', '*.vue', '*.css' }, - command = function() - return 'prettier --stdin-filepath ' .. vim.api.nvim_buf_get_name(0) - end, - }, - { pattern = { "*.sql" }, command = "pg_format --spaces=2 --wrap-limit=1000 --no-rcfile" }, - { pattern = { "*.py", "*.pyi" }, command = "ruff check --select I --fix --silent - | ruff format -" } - }, -}) diff --git a/lua/config/init.lua b/lua/config/init.lua deleted file mode 100644 index d67c8ab..0000000 --- a/lua/config/init.lua +++ /dev/null @@ -1,3 +0,0 @@ -require("config.tab") -require("config.format") -require("config.lsp.init") diff --git a/lua/config/lsp/clangd.lua b/lua/config/lsp/clangd.lua deleted file mode 100644 index 175038e..0000000 --- a/lua/config/lsp/clangd.lua +++ /dev/null @@ -1,29 +0,0 @@ -local bin_name = "clangd" -local cmd = { - bin_name, - "--background-index", - "--suggest-missing-includes", - "--enable-config", - "--query-driver=/usr/lib64/ccache/*,/usr/bin/*", -} - -local config = vim.fs.find(".clangd", { - upward = true, - stop = vim.loop.os_homedir(), - path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)), -}) - -if next(config) then - vim.api.nvim_create_autocmd("FileType", { - pattern = "c,cpp", - callback = function() - vim.lsp.start({ - name = "clangd", - cmd = cmd, - root_dir = config[1], - settings = { - }, - }) - end, - }) -end diff --git a/lua/config/lsp/init.lua b/lua/config/lsp/init.lua deleted file mode 100644 index 7eb779a..0000000 --- a/lua/config/lsp/init.lua +++ /dev/null @@ -1,68 +0,0 @@ -require('vim.lsp.log').set_format_func(vim.inspect) --- vim.lsp.set_log_level("debug") - -require("config.lsp.pyright") -require("config.lsp.ruff") --- require("config.lsp.pylsp") --- require("config.lsp.typescript") -require("config.lsp.clangd") -require("config.lsp.vls") -require("config.lsp.rust_analyzer") - -local api = vim.api -local opts = { noremap = true, silent = true } -local keymap = vim.keymap.set - -keymap("n", "[d", "lua vim.diagnostic.jump({count = -1, float = true})", opts) -keymap("n", "]d", "lua vim.diagnostic.jump({count = 1, float = true})", opts) -keymap("n", "[e", "lua vim.diagnostic.jump({count = -1, severity = vim.diagnostic.severity.ERROR, float = true})", opts) -keymap("n", "]e", "lua vim.diagnostic.jump({count = -1, severity = vim.diagnostic.severity.ERROR, float = true})", opts) - -keymap("n", "gd", "lua vim.lsp.buf.definition()", opts) -keymap("n", "gD", "lua vim.lsp.buf.declaration()", opts) -keymap("n", "gh", "lua vim.lsp.buf.signature_help()", opts) -keymap("n", "gI", "lua vim.lsp.buf.implementation()", opts) -keymap("n", "gt", "lua vim.lsp.buf.type_definition()", opts) - -keymap("n", "ca", "lua require('fzf-lua').lsp_code_actions()", opts) - -keymap("n", "gl", "lua vim.diagnostic.reset()", opts) - --- Automatic completion triggering --- vim.api.nvim_create_autocmd('LspAttach', { --- callback = function(ev) --- local client = vim.lsp.get_client_by_id(ev.data.client_id) --- if client:supports_method('textDocument/completion') then --- vim.lsp.completion.enable(true, client.id, ev.buf, { autotrigger = true }) --- end --- end, --- }) - -vim.api.nvim_create_autocmd('LspAttach', { - callback = function(ev) - local client = vim.lsp.get_client_by_id(ev.data.client_id) - if client:supports_method('textDocument/completion') then - vim.lsp.completion.enable(true, client.id, ev.buf, { autotrigger = false }) - end - end, -}) - --- Old on-attach --- vim.api.nvim_create_autocmd('LspAttach', { --- callback = function(ev) --- local client = vim.lsp.get_client_by_id(ev.data.client_id) --- --- if client.server_capabilities.completionProvider then --- vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc" --- vim.bo[ev.buf].completefunc = "v:lua.vim.lsp.omnifunc" --- end --- --- if client.server_capabilities.definitionProvider then --- vim.bo[ev.buf].tagfunc = "v:lua.vim.lsp.tagfunc" --- end --- --- if client.server_capabilities.documentFormattingProvider then --- vim.bo[ev.buf].formatexpr = "v:lua.vim.lsp.formatexpr()" --- end --- end, --- }) diff --git a/lua/config/lsp/pylsp.lua b/lua/config/lsp/pylsp.lua deleted file mode 100644 index ade7a16..0000000 --- a/lua/config/lsp/pylsp.lua +++ /dev/null @@ -1,43 +0,0 @@ -local root_files = { - "pyproject.toml", - "setup.py", - "setup.cfg", - "requirements.txt", - "Pipfile", - "pyrightconfig.json", - ".git", -} - -local root_dir = vim.fs.dirname(vim.fs.find(root_files, { upward = true })[1]) - -vim.api.nvim_create_autocmd("FileType", { - pattern = "python", - callback = function() - vim.lsp.start({ - name = "pylsp", - cmd = { "pylsp", "--log-config", "/tmp/logging.json", }, - root_dir = root_dir, - settings = { - pylsp = { - plugins = { - pyflakes = { - enabled = false, - }, - pycodestyle = { - enabled = false, - }, - mccabe = { - enabled = false, - }, - flake8 = { - enabled = true, - }, - pylsp_mypy = { - enabled = true, - }, - }, - }, - }, - }) - end, -}) diff --git a/lua/config/lsp/pyright.lua b/lua/config/lsp/pyright.lua deleted file mode 100644 index 26b059d..0000000 --- a/lua/config/lsp/pyright.lua +++ /dev/null @@ -1,28 +0,0 @@ -local bin_name = "pyright-langserver" -local cmd = { bin_name, "--stdio", } - -local root_files = { - "pyproject.toml", - "requirements.txt", - ".git", -} - -vim.api.nvim_create_autocmd("FileType", { - pattern = "python", - callback = function() - vim.lsp.start({ - name = "pyright", - cmd = cmd, - root_dir = vim.fs.dirname(vim.fs.find(root_files, { upward = true })[1]), - settings = { - python = { - analysis = { - autoSearchPaths = true, - useLibraryCodeForTypes = true, - diagnosticMode = "workspace", - }, - }, - }, - }) - end, -}) diff --git a/lua/config/lsp/ruff.lua b/lua/config/lsp/ruff.lua deleted file mode 100644 index bea8661..0000000 --- a/lua/config/lsp/ruff.lua +++ /dev/null @@ -1,28 +0,0 @@ -local bin_name = "ruff" -local cmd = { bin_name, "server", } - -local root_files = { - "pyproject.toml", - "requirements.txt", - ".git", -} - -vim.api.nvim_create_autocmd("FileType", { - pattern = "python", - callback = function() - vim.lsp.start({ - name = "ruff-server", - cmd = cmd, - root_dir = vim.fs.dirname(vim.fs.find(root_files, { upward = true })[1]), - settings = { - }, - }) - - -- vim.api.nvim_create_autocmd("BufWritePre", { - -- pattern = { "*.py", "*.pyi" }, - -- callback = function() - -- vim.lsp.buf.format { timeout_ms = 2000 } - -- end, - -- }) - end, -}) diff --git a/lua/config/lsp/rust_analyzer.lua b/lua/config/lsp/rust_analyzer.lua deleted file mode 100644 index 6af765d..0000000 --- a/lua/config/lsp/rust_analyzer.lua +++ /dev/null @@ -1,44 +0,0 @@ -local bin_name = "rust-analyzer" -local cmd = { bin_name } - -local config = vim.fs.find("Cargo.toml", { - upward = true, - stop = vim.loop.os_homedir(), - path = vim.fs.dirname(vim.api.nvim_buf_get_name(0)), -}) - -if next(config) then - vim.api.nvim_create_autocmd("FileType", { - pattern = "rust", - callback = function() - vim.lsp.start({ - name = "rust-analyzer", - cmd = cmd, - root_dir = config[1], - settings = { - ["rust-analyzer"] = { - checkOnSave = { - command = "clippy", - enable = true - }, - diagnostics = { - enable = false, - }, - check = { - command = "check", - extraArgs = {"--all-features"} - }, - }, - }, - capabilities = vim.lsp.protocol.make_client_capabilities(), - }) - end, - }) - - vim.api.nvim_create_autocmd("BufWritePre", { - pattern = "*.rs", - callback = function() - vim.lsp.buf.format({ timeout_ms = 2000 }) - end, - }) -end diff --git a/lua/config/lsp/typescript.lua b/lua/config/lsp/typescript.lua deleted file mode 100644 index cbd65a2..0000000 --- a/lua/config/lsp/typescript.lua +++ /dev/null @@ -1,27 +0,0 @@ -local bin_name = "typescript-language-server" -local cmd = { bin_name, "--stdio" } - -local root_files = { - "package.json", - ".git", -} - -vim.api.nvim_create_autocmd("FileType", { - pattern = "typescript,javascript,javascriptreact,typescriptreact", - callback = function() - vim.lsp.start({ - name = "typescript-language-server", - cmd = cmd, - root_dir = vim.fs.dirname(vim.fs.find(root_files, { upward = true })[1]), - settings = { - init_options = { - completionDisableFilterText = true, - hostInfo = 'neovim', - preferences = { - includeCompletionsForModuleExports = false - }, - }, - }, - }) - end, -}) diff --git a/lua/config/lsp/vls.lua b/lua/config/lsp/vls.lua deleted file mode 100644 index dbf02b4..0000000 --- a/lua/config/lsp/vls.lua +++ /dev/null @@ -1,41 +0,0 @@ -local root_files = { - "package.json", - ".git", -} - -local root_dir = vim.fs.dirname(vim.fs.find(root_files, { upward = true })[1]) - -vim.api.nvim_create_autocmd("FileType", { - pattern = "typescript,javascript,javascriptreact,typescriptreact,vue", - callback = function() --- vim.lsp.start({ --- name = "typescript-language-server", --- cmd = { "typescript-language-server", "--stdio" }, --- root_dir = root_dir, --- settings = { --- init_options = { --- completionDisableFilterText = true, --- plugins = { --- name = "@vue/typescript-plugin", --- location = "/home/mordae/.config/yarn/global/node_modules/@vue/language-server", --- languages = { "vue" } --- }, --- }, --- }, --- }) - - vim.lsp.start({ - name = "volar", - cmd = { "vue-language-server", "--stdio" }, - root_dir = root_dir, - init_options = { - typescript = { - tsdk = "/home/mordae/.config/yarn/global/node_modules/typescript/lib", - }, - vue = { - hybridMode = false, - }, - }, - }) - end, -}) diff --git a/lua/config/tab.lua b/lua/config/tab.lua deleted file mode 100644 index 3d1cb16..0000000 --- a/lua/config/tab.lua +++ /dev/null @@ -1,14 +0,0 @@ -local function check_back_space() - local col = vim.fn.col('.') - 1 - return col <= 0 or vim.fn.getline('.'):sub(col, col):match('%s') -end - -function tab_complete() - if check_back_space() then - return vim.api.nvim_replace_termcodes('', true, false, true) - end - return vim.api.nvim_replace_termcodes('', true, false, true) -end - -vim.api.nvim_set_keymap('i', '', 'v:lua.tab_complete()', { expr = true, noremap = true }) -vim.api.nvim_set_keymap('i', '', '', { noremap = true }) diff --git a/lua/init.lua b/lua/init.lua index 514fa1b..cc686a5 100644 --- a/lua/init.lua +++ b/lua/init.lua @@ -1,7 +1,7 @@ -- Work around https://github.com/neovim/neovim/issues/21856 vim.api.nvim_create_autocmd({ "VimLeave" }, { callback = function() - vim.fn.jobstart('/bin/true', {detach=true}) + vim.fn.jobstart("/bin/true", { detach = true }) end, }) @@ -17,8 +17,8 @@ vim.diagnostic.config({ -- Open in floating window float = { - source = 'always', - border = 'shadow', + source = "always", + border = "shadow", }, }) @@ -48,13 +48,14 @@ require("fzf-lua").setup { }, } -vim.keymap.set('n', '', 'lua require("fzf-lua").files()', { silent = true }) -vim.keymap.set('n', '', 'lua require("fzf-lua").git_status()', { silent = true }) -vim.keymap.set('n', '', 'lua require("fzf-lua").live_grep()', { silent = true }) -vim.keymap.set('n', '', 'lua require("fzf-lua").buffers()', { silent = true }) -vim.keymap.set('n', '', 'lua require("fzf-lua").builtin()', { silent = true }) +vim.keymap.set("n", "", "lua require('fzf-lua').files()", { noremap = true, silent = true }) +vim.keymap.set("n", "", "lua require('fzf-lua').git_status()", { noremap = true, silent = true }) +vim.keymap.set("n", "", "lua require('fzf-lua').live_grep()", { noremap = true, silent = true }) +vim.keymap.set("n", "", "lua require('fzf-lua').buffers()", { noremap = true, silent = true }) +vim.keymap.set("n", "", "lua require('fzf-lua').builtin()", { noremap = true, silent = true }) +vim.keymap.set("n", "ca", "lua require('fzf-lua').lsp_code_actions()", { noremap = true, silent = true }) -require('nvim-treesitter.configs').setup { +require("nvim-treesitter.configs").setup { highlight = { enable = true, additional_vim_regex_highlighting = false, @@ -65,4 +66,105 @@ require('nvim-treesitter.configs').setup { } } -require("config.init") +require("nvim-format-buffer").setup({ + verbose = false, + format_rules = { + { pattern = { "*.c", "*.h" }, command = "clang-format" }, + { + pattern = { "*.js", "*.mjs", "*.ts", "*.mts", "*.cjs", "*.tsx", "*.vue", "*.css" }, + command = function() + return "prettier --stdin-filepath " .. vim.api.nvim_buf_get_name(0) + end, + }, + { pattern = { "*.sql" }, command = "pg_format --spaces=2 --wrap-limit=1000 --no-rcfile" }, + { pattern = { "*.py", "*.pyi" }, command = "ruff check --select I --fix --silent - | ruff format -" } + }, +}) + +require("vim.lsp.log").set_format_func(vim.inspect) +-- vim.lsp.set_log_level("debug") + +vim.keymap.set("n", "[d", "lua vim.diagnostic.jump({count = -1, float = true})", { noremap = true, silent = true }) +vim.keymap.set("n", "]d", "lua vim.diagnostic.jump({count = 1, float = true})", { noremap = true, silent = true }) +vim.keymap.set("n", "[e", "lua vim.diagnostic.jump({count = -1, severity = vim.diagnostic.severity.ERROR, float = true})", { noremap = true, silent = true }) +vim.keymap.set("n", "]e", "lua vim.diagnostic.jump({count = -1, severity = vim.diagnostic.severity.ERROR, float = true})", { noremap = true, silent = true }) + +vim.keymap.set("n", "gd", "lua vim.lsp.buf.definition()", { noremap = true, silent = true }) +vim.keymap.set("n", "gD", "lua vim.lsp.buf.declaration()", { noremap = true, silent = true }) +vim.keymap.set("n", "gh", "lua vim.lsp.buf.signature_help()", { noremap = true, silent = true }) +vim.keymap.set("n", "gI", "lua vim.lsp.buf.implementation()", { noremap = true, silent = true }) +vim.keymap.set("n", "gt", "lua vim.lsp.buf.type_definition()", { noremap = true, silent = true }) + +vim.keymap.set("n", "gl", "lua vim.diagnostic.reset()", { noremap = true, silent = true }) + +vim.lsp.enable("pyright") +vim.lsp.config("pyright", {}) + +vim.lsp.enable("ts_ls") +vim.lsp.config("ts_ls", { + init_options = { + plugins = { + { + name = "@vue/typescript-plugin", + location = "/home/mordae/.config/yarn/global/node_modules/@vue/typescript-plugin", + languages = { "javascript", "typescript", "vue" }, + }, + }, + }, + filetypes = { + "javascript", + "typescript", + "vue", + }, +}) + +vim.lsp.enable("volar") +vim.lsp.config("volar", { + init_options = { + typescript = { + tsdk = "/home/mordae/.config/yarn/global/node_modules/typescript/lib", + } + } +}) + +vim.lsp.enable("clangd") +vim.lsp.config("clangd", { + cmd = { + "clangd", + "--background-index", + "--suggest-missing-includes", + "--enable-config", + "--query-driver=/usr/lib64/ccache/*,/usr/bin/*", + } +}) + +vim.lsp.enable("ruff") +vim.lsp.config("ruff", {}) + +vim.lsp.enable("rust_analyzer") +vim.lsp.config("rust_analyzer", {}) + +-- Automatic completion triggering +-- vim.api.nvim_create_autocmd("LspAttach", { +-- callback = function(ev) +-- local client = vim.lsp.get_client_by_id(ev.data.client_id) +-- if client:supports_method("textDocument/completion") then +-- vim.lsp.completion.enable(true, client.id, ev.buf, { autotrigger = true }) +-- end +-- end, +-- }) + +local function check_back_space() + local col = vim.fn.col('.') - 1 + return col <= 0 or vim.fn.getline('.'):sub(col, col):match('%s') +end + +function tab_complete() + if check_back_space() then + return vim.api.nvim_replace_termcodes('', true, false, true) + end + return vim.api.nvim_replace_termcodes('', true, false, true) +end + +vim.api.nvim_set_keymap('i', '', 'v:lua.tab_complete()', { expr = true, noremap = true }) +vim.api.nvim_set_keymap('i', '', '', { noremap = true })