lsp: use new config structure

This commit is contained in:
2023-04-26 00:05:45 +08:00
parent 45b2a50dcf
commit 8ed9f98fcc
4 changed files with 103 additions and 48 deletions
+56 -48
View File
@@ -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", "<Leader>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", "<Leader>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 <c-x><c-o>
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", "<Cmd>Telescope lsp_definitions theme=get_cursor<CR>", { desc = "Goto Definition", buffer = ev.buf })
vim.keymap.set("n", "gr", "<Cmd>Telescope lsp_references theme=get_cursor<CR>", { desc = "Goto References", buffer = ev.buf })
vim.keymap.set("n", "gi", "<Cmd>Telescope lsp_implementation theme=get_cursor<CR>", { desc = "Goto Implementation", buffer = ev.buf })
vim.keymap.set("n", "K", vim.lsp.buf.hover, { desc = "Hover", buffer = ev.buf })
vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, { desc = "Signature Help", buffer = ev.buf })
vim.keymap.set("n", "<Leader>wa", vim.lsp.buf.add_workspace_folder, { buffer = ev.buf })
vim.keymap.set("n", "<Leader>wr", vim.lsp.buf.remove_workspace_folder, { buffer = ev.buf })
vim.keymap.set("n", "<Leader>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, { buffer = ev.buf })
vim.keymap.set("n", "<Leader>D", "<Cmd>Telescope lsp_type_definitions theme=get_cursor<CR>", { buffer = ev.buf })
vim.keymap.set("n", "<Leader>rn", vim.lsp.buf.rename, { buffer = ev.buf })
vim.keymap.set({ "n", "v" }, "<Leader>ca", vim.lsp.buf.code_action, { desc = "Code Action", buffer = ev.buf })
vim.keymap.set("n", "<Leader>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 <c-x><c-o>
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", "<Cmd>Telescope lsp_definitions<CR>", { desc = "Goto Definition" })
map("n", "gr", "<Cmd>Telescope lsp_references<CR>", { desc = "Godo References" })
map("n", "gi", "<Cmd>Telescope lsp_implementation<CR>", { desc = "Goto Implementation" })
map("n", "K", vim.lsp.buf.hover, { desc = "Hover" })
map("n", "<C-k>", vim.lsp.buf.signature_help, { desc = "Signature Help" })
vim.keymap.set("n", "<Leader>wa", vim.lsp.buf.add_workspace_folder, bufopts)
vim.keymap.set("n", "<Leader>wr", vim.lsp.buf.remove_workspace_folder, bufopts)
vim.keymap.set("n", "<Leader>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, bufopts)
vim.keymap.set("n", "<Leader>D", vim.lsp.buf.type_definition, bufopts)
vim.keymap.set("n", "<Leader>rn", vim.lsp.buf.rename, bufopts)
map("n", "<Leader>ca", vim.lsp.buf.code_action, { desc = "Code Action" })
map("n", "<Leader>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,
})
+18
View File
@@ -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
+7
View File
@@ -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
+22
View File
@@ -0,0 +1,22 @@
local M = {
on_attach = function(client, bufnr)
vim.keymap.set("n", "<Leader>lb", "<Cmd>TexlabBuild<CR>", { desc = "Texlab Build", buffer = bufnr })
vim.keymap.set("n", "<Leader>lf", "<Cmd>TexlabForward<CR>", { 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