config-neovim/lua/plugins/50-lsp.lua
2024-09-07 22:51:37 +08:00

153 lines
5.9 KiB
Lua

function setup_keymap_lsp()
-- -- 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)
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
local opts = { buffer = ev.buf }
vim.keymap.set("n", "gt", vim.lsp.buf.type_definition, opts)
vim.keymap.set("n", "gD", vim.lsp.buf.declaration, opts)
vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, opts)
vim.keymap.set("n", "<leader>wa", vim.lsp.buf.add_workspace_folder, opts)
vim.keymap.set("n", "<leader>wr", vim.lsp.buf.remove_workspace_folder, opts)
vim.keymap.set("n", "<leader>wl", function()
print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
end, opts)
vim.keymap.set("n", "gR", vim.lsp.buf.rename, opts)
vim.keymap.set({ "n", "v" }, "ga", vim.lsp.buf.code_action, opts)
vim.keymap.set("n", "g=", function()
vim.lsp.buf.format { async = true }
end, opts)
end,
})
end
local lua_ls_opts = {
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
},
diagnostics = {
-- Get the language server to recognize the `vim` global
globals = { "vim" },
},
workspace = {
-- Make the server aware of Neovim runtime files
library = vim.api.nvim_get_runtime_file("", true),
},
-- Do not send telemetry data containing a randomized but unique identifier
telemetry = {
enable = false,
},
},
},
}
return {
{
"neovim/nvim-lspconfig",
dependencies = {
"hrsh7th/nvim-cmp",
"hrsh7th/cmp-nvim-lsp",
"hrsh7th/cmp-buffer",
"hrsh7th/cmp-path",
"hrsh7th/cmp-cmdline",
"L3MON4D3/LuaSnip",
"williamboman/mason.nvim",
"williamboman/mason-lspconfig.nvim",
"mfussenegger/nvim-lint",
"mhartington/formatter.nvim",
},
event = { "BufReadPre", "BufNewFile", "VeryLazy" },
cmd = { "LspInfo", "LspInstall", "LspUninstall" },
config = function (_, opts)
local mason = require("mason")
local mason_lsp = require("mason-lspconfig")
local luasnip = require("luasnip")
local cmp = require("cmp")
mason.setup()
mason_lsp.setup()
mason_lsp.setup_handlers {
function (server_name) -- default handler (optional)
require("lspconfig")[server_name].setup {}
end,
["lua_ls"] = function ()
require("lspconfig")["lua_ls"].setup(lua_ls_opts)
end
}
cmp.setup({
snippet = {
expand = function(args)
luasnip.lsp_expand(args.body)
end,
},
mapping = cmp.mapping.preset.insert({
["<C-b>"] = cmp.mapping.scroll_docs(-4),
["<C-f>"] = cmp.mapping.scroll_docs(4),
["<C-Space>"] = cmp.mapping.complete(),
["<C-e>"] = cmp.mapping.abort(),
["<tab>"] = cmp.mapping.confirm({ select = true }),
}),
sources = cmp.config.sources({
{ name = "nvim_lsp" },
{ name = "buffer" },
{ name = "luasnip" },
}),
})
cmp.setup.cmdline(":", {
mapping = cmp.mapping.preset.cmdline(),
sources = cmp.config.sources({
{ name = "path" }
}, {
{ name = "cmdline" }
})
})
setup_keymap_lsp()
end,
keys = {
{ "gs", "<cmd>LspStart<cr>", mode = "n" },
{ "gS", "<cmd>LspStop<cr>", mode = "n" },
{ "gI", "<cmd>LspInfo<cr>", mode = "n" },
{ "ge", vim.diagnostic.open_float, mode = "n" },
{ "gp", vim.diagnostic.goto_prev, mode = "n" },
{ "gn", vim.diagnostic.goto_next, mode = "n" },
{ "gq", vim.diagnostic.setloclist, mode = "n" }
},
},
{
"folke/trouble.nvim",
dependencies = { "nvim-tree/nvim-web-devicons" },
event = "VeryLazy",
keys = {
{ "<leader>xx", "<cmd>TroubleToggle<cr>", mode = "n" },
{ "<leader>xw", "<cmd>TroubleToggle workspace_diagnostics<cr>", mode = "n" },
{ "<leader>xd", "<cmd>TroubleToggle document_diagnostics<cr>", mode = "n" },
{ "<leader>xq", "<cmd>TroubleToggle quickfix<cr>", mode = "n" },
{ "<leader>xl", "<cmd>TroubleToggle loclist<cr>", mode = "n" },
{ "<leader>xr", "<cmd>TroubleToggle lsp_references<cr>", mode = "n" },
},
opts = { },
},
}