188 lines
6.2 KiB
Lua
188 lines
6.2 KiB
Lua
return {
|
|
"neovim/nvim-lspconfig",
|
|
event = { "BufReadPre", "BufNewFile" },
|
|
dependencies = {
|
|
"hrsh7th/cmp-nvim-lsp",
|
|
{ "antosha417/nvim-lsp-file-operations", config = true },
|
|
{ "folke/neodev.nvim", opts = {} },
|
|
},
|
|
config = function()
|
|
-- import lspconfig plugin
|
|
local lspconfig = require("lspconfig")
|
|
lspconfig.util.default_config = vim.tbl_extend("force", lspconfig.util.default_config, {
|
|
capabilities = vim.tbl_deep_extend(
|
|
"force",
|
|
vim.lsp.protocol.make_client_capabilities(),
|
|
require("lsp-file-operations").default_capabilities()
|
|
),
|
|
})
|
|
lspconfig.dartls.setup({})
|
|
-- import cmp-nvim-lsp plugin
|
|
local cmp_nvim_lsp = require("cmp_nvim_lsp")
|
|
|
|
local keymap = vim.keymap -- for conciseness
|
|
|
|
vim.api.nvim_create_autocmd("LspAttach", {
|
|
group = vim.api.nvim_create_augroup("UserLspConfig", {}),
|
|
callback = function(ev)
|
|
-- Buffer local mappings.
|
|
-- See `:help vim.lsp.*` for documentation on any of the below functions
|
|
local opts = { buffer = ev.buf, silent = true }
|
|
|
|
-- set keybinds
|
|
opts.desc = "Show LSP references"
|
|
keymap.set("n", "gR", "<cmd>Telescope lsp_references<CR>", opts) -- show definition, references
|
|
|
|
opts.desc = "Go to declaration"
|
|
keymap.set("n", "gD", vim.lsp.buf.declaration, opts) -- go to declaration
|
|
|
|
opts.desc = "Show LSP definitions"
|
|
keymap.set("n", "gd", "<cmd>Telescope lsp_definitions<CR>", opts) -- show lsp definitions
|
|
|
|
opts.desc = "Show LSP implementations"
|
|
keymap.set("n", "gi", "<cmd>Telescope lsp_implementations<CR>", opts) -- show lsp implementations
|
|
|
|
opts.desc = "Show LSP type definitions"
|
|
keymap.set("n", "gt", "<cmd>Telescope lsp_type_definitions<CR>", opts) -- show lsp type definitions
|
|
|
|
opts.desc = "See available code actions"
|
|
keymap.set({ "n", "v" }, "<leader>cz", vim.lsp.buf.code_action, opts) -- see available code actions, in visual mode will apply to selection
|
|
|
|
opts.desc = "Smart rename"
|
|
keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts) -- smart rename
|
|
|
|
opts.desc = "Show buffer diagnostics"
|
|
keymap.set("n", "<leader>D", "<cmd>Telescope diagnostics bufnr=0<CR>", opts) -- show diagnostics for file
|
|
|
|
opts.desc = "Show line diagnostics"
|
|
keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts) -- show diagnostics for line
|
|
|
|
opts.desc = "Go to previous diagnostic"
|
|
keymap.set("n", "[d", vim.diagnostic.goto_prev, opts) -- jump to previous diagnostic in buffer
|
|
|
|
opts.desc = "Go to next diagnostic"
|
|
keymap.set("n", "]d", vim.diagnostic.goto_next, opts) -- jump to next diagnostic in buffer
|
|
|
|
opts.desc = "Show documentation for what is under cursor"
|
|
keymap.set("n", "K", vim.lsp.buf.hover, opts) -- show documentation for what is under cursor
|
|
|
|
opts.desc = "Restart LSP"
|
|
keymap.set("n", "<leader>rs", ":LspRestart<CR>", opts) -- mapping to restart lsp if necessary
|
|
end,
|
|
})
|
|
|
|
local capabilities = vim.tbl_deep_extend(
|
|
"force",
|
|
cmp_nvim_lsp.default_capabilities(),
|
|
require("lsp-file-operations").default_capabilities()
|
|
)
|
|
local on_attach = function(client, bufnr)
|
|
-- Enable completion triggered by <C-x><C-o>
|
|
vim.bo[bufnr].omnifunc = "v:lua.vim.lsp.omnifunc"
|
|
|
|
-- Define key mappings for LSP functionality
|
|
local opts = { noremap = true, silent = true, buffer = bufnr }
|
|
vim.keymap.set("n", "gd", vim.lsp.buf.definition, opts)
|
|
vim.keymap.set("n", "K", vim.lsp.buf.hover, opts)
|
|
vim.keymap.set("n", "gi", vim.lsp.buf.implementation, opts)
|
|
vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, opts)
|
|
vim.keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts)
|
|
vim.keymap.set("n", "<leader>ca", vim.lsp.buf.code_action, opts)
|
|
vim.keymap.set("n", "gr", vim.lsp.buf.references, opts)
|
|
vim.keymap.set("n", "<leader>fr", function()
|
|
vim.lsp.buf.format({ async = true })
|
|
end, opts)
|
|
|
|
-- Additional client-specific setup
|
|
if client.server_capabilities.document_highlight then
|
|
vim.api.nvim_create_augroup("LspDocumentHighlight", { clear = true })
|
|
vim.api.nvim_create_autocmd("CursorHold", {
|
|
group = "LspDocumentHighlight",
|
|
buffer = bufnr,
|
|
callback = vim.lsp.buf.document_highlight,
|
|
})
|
|
vim.api.nvim_create_autocmd("CursorMoved", {
|
|
group = "LspDocumentHighlight",
|
|
buffer = bufnr,
|
|
callback = vim.lsp.buf.clear_references,
|
|
})
|
|
end
|
|
end
|
|
-- Change the Diagnostic symbols in the sign column (gutter)
|
|
-- (not in youtube nvim video)
|
|
local signs = { Error = " ", Warn = " ", Hint = " ", Info = " " }
|
|
for type, icon in pairs(signs) do
|
|
local hl = "DiagnosticSign" .. type
|
|
vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
|
|
end
|
|
|
|
require("mason-lspconfig").setup({
|
|
ensure_installed = {
|
|
"lua_ls",
|
|
"rust_analyzer",
|
|
"gopls",
|
|
},
|
|
handlers = {
|
|
function(server_name) -- default handler (optional)
|
|
require("lspconfig")[server_name].setup({
|
|
capabilities = capabilities,
|
|
})
|
|
end,
|
|
|
|
zls = function()
|
|
local lspconfig = require("lspconfig")
|
|
lspconfig.zls.setup({
|
|
root_dir = lspconfig.util.root_pattern(".git", "build.zig", "zls.json"),
|
|
settings = {
|
|
zls = {
|
|
enable_inlay_hints = true,
|
|
enable_snippets = true,
|
|
warn_style = true,
|
|
},
|
|
},
|
|
})
|
|
vim.g.zig_fmt_parse_errors = 0
|
|
vim.g.zig_fmt_autosave = 0
|
|
end,
|
|
["lua_ls"] = function()
|
|
local lspconfig = require("lspconfig")
|
|
lspconfig.lua_ls.setup({
|
|
capabilities = capabilities,
|
|
settings = {
|
|
Lua = {
|
|
format = {
|
|
enable = true,
|
|
-- Put format options here
|
|
-- NOTE: the value should be STRING!!
|
|
defaultConfig = {
|
|
indent_style = "space",
|
|
indent_size = "2",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
end,
|
|
["gopls"] = function()
|
|
lspconfig.gopls.setup({
|
|
on_attach = on_attach,
|
|
capabilities = capabilities,
|
|
cmd = { "gopls" },
|
|
filetypes = { "go", "gomod", "gowork", "gotmpl" },
|
|
root_dir = lspconfig.util.root_pattern("go.work", "go.mod", ".git"),
|
|
settings = {
|
|
gopls = {
|
|
completeUnimported = true,
|
|
usePlaceholder = true,
|
|
analyses = {
|
|
unusedparams = true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
end,
|
|
},
|
|
})
|
|
end,
|
|
}
|