160 lines
4.3 KiB
Lua
160 lines
4.3 KiB
Lua
local ok, lspconfig = pcall(require, "lspconfig")
|
|
|
|
if not ok then
|
|
return
|
|
end
|
|
|
|
local border = {
|
|
{ "┌", "FloatBorder" },
|
|
{ "─", "FloatBorder" },
|
|
{ "┐", "FloatBorder" },
|
|
{ "│", "FloatBorder" },
|
|
{ "┘", "FloatBorder" },
|
|
{ "─", "FloatBorder" },
|
|
{ "└", "FloatBorder" },
|
|
{ "│", "FloatBorder" },
|
|
}
|
|
|
|
local handlers = {
|
|
["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = border }),
|
|
["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = border }),
|
|
}
|
|
|
|
local capabilities = {}
|
|
|
|
local has_cmp, cmp_lsp = pcall(require, "cmp_nvim_lsp")
|
|
if has_cmp then
|
|
capabilities = cmp_lsp.default_capabilities()
|
|
end
|
|
|
|
local has_lsp_status, lsp_status = pcall(require, "lsp_status")
|
|
if has_lsp_status then
|
|
capabilities = vim.tbl_extend("keep", capabilities, lsp_status.capabilities)
|
|
end
|
|
|
|
local format_augroup = setmetatable({}, {
|
|
__index = function(self, id)
|
|
local group = vim.api.nvim_create_augroup(string.format("lsp-format-%s", id), { clear = true })
|
|
rawset(self, id, group)
|
|
return group
|
|
end,
|
|
})
|
|
|
|
local format_saving = {}
|
|
|
|
local format_handler = function(err, result, ctx)
|
|
if not result then
|
|
return
|
|
end
|
|
|
|
local client = vim.lsp.get_client_by_id(ctx.client_id)
|
|
vim.lsp.util.apply_text_edits(result, ctx.bufnr, client.offset_encoding)
|
|
|
|
if ctx.bufnr == vim.api.nvim_get_current_buf() then
|
|
format_saving[ctx.bufnr] = true
|
|
vim.cmd [[update]]
|
|
format_saving[ctx.bufnr] = false
|
|
end
|
|
end
|
|
|
|
local on_attach = function(client, bufnr)
|
|
require("my.keymaps").lsp_keymaps(client, bufnr)
|
|
|
|
if client.server_capabilities.completionProvider then
|
|
vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")
|
|
end
|
|
|
|
if client.server_capabilities.definitionProvider then
|
|
vim.api.nvim_buf_set_option(bufnr, "tagfunc", "v:lua.vim.lsp.tagfunc")
|
|
end
|
|
|
|
-- Highlight on cursor hold
|
|
if client.server_capabilities.documentHighlightProvider then
|
|
vim.api.nvim_create_autocmd({ "CursorHold" }, {
|
|
callback = vim.lsp.buf.document_highlight,
|
|
buffer = bufnr,
|
|
})
|
|
vim.api.nvim_create_autocmd({ "CursorHoldI" }, {
|
|
callback = vim.lsp.buf.signature_help,
|
|
buffer = bufnr,
|
|
})
|
|
vim.api.nvim_create_autocmd({ "CursorMoved" }, {
|
|
callback = vim.lsp.buf.clear_references,
|
|
buffer = bufnr,
|
|
})
|
|
end
|
|
|
|
-- Format on save
|
|
if client.server_capabilities.documentFormattingProvider then
|
|
vim.lsp.handlers["textDocument/formatting"] = vim.lsp.with(format_handler, {})
|
|
vim.api.nvim_clear_autocmds {
|
|
buffer = bufnr,
|
|
group = format_augroup[client.id],
|
|
}
|
|
vim.api.nvim_create_autocmd({ "BufWritePost" }, {
|
|
group = format_augroup[client.id],
|
|
desc = "format on save",
|
|
pattern = "<buffer>",
|
|
callback = function()
|
|
if format_saving[bufnr] == true then
|
|
return
|
|
end
|
|
vim.lsp.buf.format({ async = true })
|
|
end,
|
|
})
|
|
end
|
|
|
|
if has_lsp_status then
|
|
lsp_status.on_attach(client)
|
|
end
|
|
end
|
|
|
|
local servers = {
|
|
gopls = {},
|
|
-- phpactor = {},
|
|
-- pylsp = {},
|
|
rust_analyzer = {
|
|
assist = {
|
|
importGranularity = "module",
|
|
importPrefix = "self",
|
|
},
|
|
cargo = {
|
|
buildScripts = {
|
|
enable = true,
|
|
},
|
|
},
|
|
procMacro = {
|
|
enable = true,
|
|
},
|
|
},
|
|
lua_ls = {
|
|
settings = {
|
|
Lua = {
|
|
runtime = {
|
|
version = "LuaJIT",
|
|
},
|
|
diagnostics = {
|
|
globals = { "vim" },
|
|
},
|
|
workspace = {
|
|
library = vim.api.nvim_get_runtime_file("", true),
|
|
},
|
|
telemetry = {
|
|
enable = false,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
elixirls = {
|
|
cmd = { "elixir-ls" }
|
|
},
|
|
tsserver = {},
|
|
}
|
|
|
|
for lsp, settings in pairs(servers) do
|
|
lspconfig[lsp].setup(vim.tbl_extend("force", {
|
|
on_attach = on_attach,
|
|
capabilities = capabilities,
|
|
handlers = handlers,
|
|
}, settings))
|
|
end
|