.files/.config/nvim/plugin/lsp.lua

145 lines
3.9 KiB
Lua

local map = vim.keymap.set
----------------------
-- Language servers --
----------------------
-- NOTE: put settings into `settings`
-- use another `on_attach` field if needed
local servers = {
clangd = {}, -- C/CPP
cssls = {}, -- CSS
html = {}, -- HTML
jsonls = {}, -- JSON
lemminx = {}, -- XML
phpactor = {}, -- PHP
pylsp = {}, -- Python
pyright = {},
taplo = {}, -- TOML
texlab = {}, -- texlab
ts_ls = {}, -- TypeScript
vimls = {}, -- Vim Script
ocamllsp = {}, -- OCaml
gleam = {}, -- Gleam
yamlls = {}, -- yaml
gopls = {}, -- Golang
elmls = {}, -- elm
-- -- fix this, it shows spamming message on top
-- golangci_lint_ls = {},
fish_lsp = {},
bashls = { -- Bash
on_attach = function(_, bufno)
map("n", "<leader>f", function()
local saved = vim.fn.winsaveview()
vim.cmd([[silent exec "%!shfmt"]])
vim.fn.winrestview(saved)
end, { buffer = bufno })
end,
},
tinymist = {
on_attach = function(_, bufno)
map("n", "<leader>f", function()
local saved = vim.fn.winsaveview()
vim.cmd([[silent exec "%!typstyle -c 100"]])
vim.fn.winrestview(saved)
end, { buffer = bufno })
end,
},
lua_ls = { -- Lua
on_attach = function(_, bufno)
map("n", "<leader>f", function()
local saved = vim.fn.winsaveview()
vim.cmd([[silent exec "!stylua %"]])
vim.fn.winrestview(saved)
end, { buffer = bufno })
end,
settings = {
Lua = {
workspace = {
checkThirdParty = "Disable",
library = { vim.env.VIMRUNTIME },
},
},
},
},
nil_ls = { -- Nix
settings = { ["nil"] = { formatting = { command = { "alejandra" } } } },
},
}
-------------
-- Helpers --
-------------
vim.diagnostic.config {
signs = {
text = {
[vim.diagnostic.severity.ERROR] = "E",
[vim.diagnostic.severity.WARN] = "W",
[vim.diagnostic.severity.INFO] = "H",
[vim.diagnostic.severity.HINT] = "·",
},
},
severity_sort = true,
underline = {
severity = {
vim.diagnostic.severity.ERROR,
vim.diagnostic.severity.WARN,
},
},
}
local mylsp = require("lsp")
-- Helix style border
local orig_util_open_floating_preview = vim.lsp.util.open_floating_preview
function vim.lsp.util.open_floating_preview(contents, syntax, opts, ...)
opts = opts or {}
opts.border = mylsp.border
return orig_util_open_floating_preview(contents, syntax, opts, ...)
end
-- Set log level
vim.lsp.set_log_level("off")
----------
-- Init --
----------
for name, config in pairs(servers) do
require("lspconfig")[name].setup {
capabilities = mylsp.capabilities,
settings = config.settings,
on_attach = function(client, bufno)
mylsp.on_attach(client, bufno);
(config.on_attach or function(...) end)(client, bufno)
end,
}
end
------------------------
-- Standalone plugins --
------------------------
-- Java
local config = {
on_attach = mylsp.on_attach,
capabilities = mylsp.capabilities,
cmd = {
-- https://github.com/NixOS/nixpkgs/issues/232822#issuecomment-1564243667
-- `-data` argument is necessary
"jdtls",
"-data",
vim.fn.expand("~/.cache/jdtls") .. vim.fn.expand("%:p:h"),
},
root_dir = vim.fs.dirname(vim.fs.find({ "gradlew", ".git", "mvnw" }, { upward = true })[1]),
}
local jdtls_group = vim.api.nvim_create_augroup("jdtls", { clear = true })
vim.api.nvim_create_autocmd("FileType", {
pattern = { "java" },
callback = function() require("jdtls").start_or_attach(config) end,
group = jdtls_group,
})