From 8ed9f98fccf356a7ab09efdeb451c7b93e7b1dbc Mon Sep 17 00:00:00 2001 From: c-my Date: Wed, 26 Apr 2023 00:05:45 +0800 Subject: [PATCH] lsp: use new config structure --- lua/plugins/lsp.lua | 104 +++++++++++++++++++----------------- lua/plugins/lsp/lua.lua | 18 +++++++ lua/plugins/lsp/pyright.lua | 7 +++ lua/plugins/lsp/texlab.lua | 22 ++++++++ 4 files changed, 103 insertions(+), 48 deletions(-) create mode 100644 lua/plugins/lsp/lua.lua create mode 100644 lua/plugins/lsp/pyright.lua create mode 100644 lua/plugins/lsp/texlab.lua diff --git a/lua/plugins/lsp.lua b/lua/plugins/lsp.lua index 5827a09..03a2d56 100644 --- a/lua/plugins/lsp.lua +++ b/lua/plugins/lsp.lua @@ -10,44 +10,48 @@ return { ensure_installed = { "jdtls", "omnisharp", "pyright", "lua_ls" }, }, config = function(_, opts) + -- Global mappings. + -- See `:help vim.diagnostic.*` for documentation on any of the below functions + vim.keymap.set("n", "e", vim.diagnostic.open_float) + vim.keymap.set("n", "[d", vim.diagnostic.goto_prev) + vim.keymap.set("n", "]d", vim.diagnostic.goto_next) + vim.keymap.set("n", "q", vim.diagnostic.setloclist) + + -- Use LspAttach autocommand to only map the following keys + -- after the language server attaches to the current buffer + vim.api.nvim_create_autocmd("LspAttach", { + group = vim.api.nvim_create_augroup("UserLspConfig", {}), + callback = function(ev) + -- Enable completion triggered by + vim.bo[ev.buf].omnifunc = "v:lua.vim.lsp.omnifunc" + + -- Buffer local mappings. + -- See `:help vim.lsp.*` for documentation on any of the below functions + -- stylua: ignore start + vim.keymap.set("n", "gD", vim.lsp.buf.declaration, { desc = "Goto Declaration", buffer = ev.buf }) + vim.keymap.set("n", "gd", "Telescope lsp_definitions theme=get_cursor", { desc = "Goto Definition", buffer = ev.buf }) + vim.keymap.set("n", "gr", "Telescope lsp_references theme=get_cursor", { desc = "Goto References", buffer = ev.buf }) + vim.keymap.set("n", "gi", "Telescope lsp_implementation theme=get_cursor", { desc = "Goto Implementation", buffer = ev.buf }) + vim.keymap.set("n", "K", vim.lsp.buf.hover, { desc = "Hover", buffer = ev.buf }) + vim.keymap.set("n", "", vim.lsp.buf.signature_help, { desc = "Signature Help", buffer = ev.buf }) + vim.keymap.set("n", "wa", vim.lsp.buf.add_workspace_folder, { buffer = ev.buf }) + vim.keymap.set("n", "wr", vim.lsp.buf.remove_workspace_folder, { buffer = ev.buf }) + vim.keymap.set("n", "wl", function() + print(vim.inspect(vim.lsp.buf.list_workspace_folders())) + end, { buffer = ev.buf }) + vim.keymap.set("n", "D", "Telescope lsp_type_definitions theme=get_cursor", { buffer = ev.buf }) + vim.keymap.set("n", "rn", vim.lsp.buf.rename, { buffer = ev.buf }) + vim.keymap.set({ "n", "v" }, "ca", vim.lsp.buf.code_action, { desc = "Code Action", buffer = ev.buf }) + vim.keymap.set("n", "f", vim.lsp.buf.format, { desc = "Format Document", buffer = ev.buf }) + -- stylua ignore end + end, + }) local on_attach = function(client, bufnr) -- Avoiding LSP formatting conflicts local null_ls_table = { pyright = true, lua_ls = true } if null_ls_table[client.name] then client.server_capabilities.documentFormattingProvider = false -- 0.8 and later end - -- Enable completion triggered by - vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc") - - -- Mappings. - -- See `:help vim.lsp.*` for documentation on any of the below functions - local bufopts = { - noremap = true, - silent = true, - buffer = bufnr, - } - local function map(mode, lhs, rhs, opts) - opts = opts or {} - opts.noremap = true - opts.silent = true - opts.buffer = bufnr - vim.keymap.set(mode, lhs, rhs, opts) - end - map("n", "gD", vim.lsp.buf.declaration, { desc = "Goto Declaration" }) - map("n", "gd", "Telescope lsp_definitions", { desc = "Goto Definition" }) - map("n", "gr", "Telescope lsp_references", { desc = "Godo References" }) - map("n", "gi", "Telescope lsp_implementation", { desc = "Goto Implementation" }) - map("n", "K", vim.lsp.buf.hover, { desc = "Hover" }) - map("n", "", vim.lsp.buf.signature_help, { desc = "Signature Help" }) - 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) - map("n", "ca", vim.lsp.buf.code_action, { desc = "Code Action" }) - map("n", "fm", vim.lsp.buf.format, { desc = "Format Document" }) end local lspconfig = require("lspconfig") local capabilities = require("cmp_nvim_lsp").default_capabilities() @@ -60,23 +64,27 @@ return { capabilities = capabilities, }) end, - - ["lua_ls"] = function() - lspconfig.lua_ls.setup({ - on_attach = on_attach, + ["pyright"] = function() + local pyright_config = require("plugins.lsp.pyright") + lspconfig.pyright.setup({ capabilities = capabilities, - settings = { - Lua = { - diagnostics = { - globals = { "vim" }, - }, - workspace = { - library = { - [vim.fn.stdpath("config") .. "/lua"] = true, - }, - }, - }, - }, + on_attach = pyright_config.on_attach, + }) + end, + ["lua_ls"] = function() + local lua_config = require("plugins.lsp.lua") + lspconfig.lua_ls.setup({ + on_attach = lua_config.on_attach, + capabilities = capabilities, + settings = lua_config.settings, + }) + end, + ["texlab"] = function() + local texlab_config = require("plugins.lsp.texlab") + lspconfig.texlab.setup({ + on_attach = texlab_config.on_attach, + capabilities = capabilities, + settings = texlab_config.settings, }) end, }) diff --git a/lua/plugins/lsp/lua.lua b/lua/plugins/lsp/lua.lua new file mode 100644 index 0000000..e9007f2 --- /dev/null +++ b/lua/plugins/lsp/lua.lua @@ -0,0 +1,18 @@ +local M = { + on_attach = function(client, bufnr) + client.server_capabilities.documentFormattingProvider = false + end, + settings = { + Lua = { + diagnostics = { + globals = { "vim" }, + }, + workspace = { + library = { + [vim.fn.stdpath("config") .. "/lua"] = true, + }, + }, + }, + }, +} +return M diff --git a/lua/plugins/lsp/pyright.lua b/lua/plugins/lsp/pyright.lua new file mode 100644 index 0000000..2182930 --- /dev/null +++ b/lua/plugins/lsp/pyright.lua @@ -0,0 +1,7 @@ +local M = { + on_attach = function(client, bufnr) + -- Adopting Black formater in python + client.server_capabilities.documentFormattingProvider = false -- 0.8 and later + end, +} +return M diff --git a/lua/plugins/lsp/texlab.lua b/lua/plugins/lsp/texlab.lua new file mode 100644 index 0000000..32eb72f --- /dev/null +++ b/lua/plugins/lsp/texlab.lua @@ -0,0 +1,22 @@ +local M = { + + on_attach = function(client, bufnr) + vim.keymap.set("n", "lb", "TexlabBuild", { desc = "Texlab Build", buffer = bufnr }) + vim.keymap.set("n", "lf", "TexlabForward", { desc = "Texlab Forward", buffer = bufnr }) + end, + settings = { + texlab = { + build = { + executable = "latexmk", + args = { "-interaction=nonstopmode", "-synctex=1" }, + onSave = true, + forwardSearchAfter = false, + }, + forwardSearch = { + executable = "SumatraPDF.exe", + args = { "-reuse-instance", "%p", "-forward-search", "%f", "%l" }, + }, + }, + }, +} +return M