dotfiles/modules/common/neovim/lua/packer/lsp.lua

154 lines
6.1 KiB
Lua
Raw Normal View History

2022-07-09 21:27:04 +00:00
-- =======================================================================
-- Language Server
-- =======================================================================
local M = {}
M.packer = function(use)
-- Language server engine
use({
"neovim/nvim-lspconfig",
requires = { "hrsh7th/cmp-nvim-lsp" },
config = function()
2022-07-11 02:47:54 +00:00
local function on_path(program)
return vim.fn.executable(program) == 1
end
2022-10-16 14:20:50 +00:00
local capabilities = require("cmp_nvim_lsp").default_capabilities()
2022-07-11 02:47:54 +00:00
if on_path("lua-language-server") then
require("lspconfig").sumneko_lua.setup({
capabilities = capabilities,
-- Turn off errors for vim global variable
settings = {
Lua = {
diagnostics = {
2022-07-13 02:15:00 +00:00
globals = { "vim", "hs" },
2022-07-11 02:47:54 +00:00
},
},
},
})
end
2022-07-10 14:15:41 +00:00
if on_path("rust-analyzer") then
require("lspconfig").rust_analyzer.setup({ capabilities = capabilities })
end
if on_path("tflint") then
require("lspconfig").tflint.setup({ capabilities = capabilities })
end
if on_path("terraform-ls") then
require("lspconfig").terraformls.setup({ capabilities = capabilities })
end
if on_path("pyright") then
require("lspconfig").pyright.setup({
on_attach = function()
-- set keymaps (requires 0.7.0)
-- vim.keymap.set("n", "", "", {buffer=0})
end,
capabilities = capabilities,
})
end
2022-11-07 13:04:29 +00:00
if on_path("nil") then
require("lspconfig").nil_ls.setup({ capabilities = capabilities })
2022-07-13 04:23:38 +00:00
end
2022-07-11 03:31:48 +00:00
vim.keymap.set("n", "gd", vim.lsp.buf.definition)
vim.keymap.set("n", "gT", vim.lsp.buf.type_definition)
vim.keymap.set("n", "gi", vim.lsp.buf.implementation)
vim.keymap.set("n", "gh", vim.lsp.buf.hover)
-- vim.keymap.set("n", "gr", telescope.lsp_references)
vim.keymap.set("n", "<Leader>R", vim.lsp.buf.rename)
vim.keymap.set("n", "]e", vim.diagnostic.goto_next)
vim.keymap.set("n", "[e", vim.diagnostic.goto_prev)
vim.keymap.set("n", "<Leader>de", vim.diagnostic.open_float)
2022-07-11 03:31:48 +00:00
vim.keymap.set("n", "<Leader>E", vim.lsp.buf.code_action)
2022-07-09 21:27:04 +00:00
end,
})
-- Pretty highlights
use("folke/lsp-colors.nvim")
-- Linting
use({
"jose-elias-alvarez/null-ls.nvim",
branch = "main",
requires = {
"nvim-lua/plenary.nvim",
"neovim/nvim-lspconfig",
},
config = function()
2022-07-11 02:47:54 +00:00
local function on_path(program)
return vim.fn.executable(program) == 1
end
2022-10-30 17:57:14 +00:00
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
2022-07-09 21:27:04 +00:00
require("null-ls").setup({
sources = {
2022-07-10 14:15:41 +00:00
require("null-ls").builtins.formatting.stylua.with({
condition = function()
return on_path("stylua")
end,
}),
require("null-ls").builtins.formatting.black.with({
condition = function()
return on_path("black")
end,
}),
2022-07-19 03:34:32 +00:00
require("null-ls").builtins.diagnostics.flake8.with({
condition = function()
return on_path("flake8")
end,
}),
2022-07-10 14:15:41 +00:00
require("null-ls").builtins.formatting.fish_indent.with({
condition = function()
return on_path("fish_indent")
end,
}),
require("null-ls").builtins.formatting.nixfmt.with({
condition = function()
return on_path("nixfmt")
end,
}),
require("null-ls").builtins.formatting.rustfmt.with({
condition = function()
return on_path("rustfmt")
end,
}),
require("null-ls").builtins.diagnostics.shellcheck.with({
condition = function()
return on_path("shellcheck")
end,
}),
2022-07-09 21:27:04 +00:00
require("null-ls").builtins.formatting.shfmt.with({
extra_args = { "-i", "4", "-ci" },
2022-07-10 14:15:41 +00:00
condition = function()
return on_path("shfmt")
end,
}),
require("null-ls").builtins.formatting.terraform_fmt.with({
condition = function()
return on_path("terraform")
end,
2022-07-09 21:27:04 +00:00
}),
-- require("null-ls").builtins.diagnostics.luacheck,
-- require("null-ls").builtins.diagnostics.markdownlint,
-- require("null-ls").builtins.diagnostics.pylint,
},
-- Format on save
2022-10-30 17:57:14 +00:00
on_attach = function(client, bufnr)
if client.supports_method("textDocument/formatting") then
vim.api.nvim_clear_autocmds({ group = augroup, buffer = bufnr })
2022-07-10 14:15:41 +00:00
vim.api.nvim_create_autocmd("BufWritePre", {
2022-10-30 17:57:14 +00:00
group = augroup,
buffer = bufnr,
callback = function()
vim.lsp.buf.format({ bufnr = bufnr })
end,
2022-07-10 14:15:41 +00:00
})
2022-07-09 21:27:04 +00:00
end
end,
})
end,
})
end
return M