.dotfiles/configs/nvim/lua/lsp-utils.lua

223 lines
5.1 KiB
Lua
Raw Normal View History

local nvim_lsp = require "lspconfig"
local lsp_status = require("lsp-status")
2021-09-22 11:44:12 +02:00
local utils = require("utils")
-- function to attach completion when setting up lsp
local function on_attach(client)
2021-11-17 12:54:51 +01:00
local function buf_set_keymap(...)
vim.api.nvim_buf_set_keymap(bufnr, ...)
end
local function buf_set_option(...)
vim.api.nvim_buf_set_option(bufnr, ...)
end
2021-11-17 12:54:51 +01:00
buf_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
2021-11-17 12:54:51 +01:00
-- Mappings.
local opts = {noremap = true, silent = true}
2021-10-27 20:37:42 +02:00
-- Set some keybinds conditional on server capabilities
2021-11-17 12:54:51 +01:00
if client.resolved_capabilities.document_formatting then
buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.formatting()<CR>", opts)
elseif client.resolved_capabilities.document_range_formatting then
buf_set_keymap("n", "<space>f", "<cmd>lua vim.lsp.buf.range_formatting()<CR>", opts)
end
-- Set autocommands conditional on server_capabilities
if client.resolved_capabilities.document_highlight then
vim.api.nvim_exec(
[[
augroup lsp_document_highlight
autocmd! * <buffer>
autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()
autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()
autocmd CursorHold *.* :lua vim.lsp.diagnostic.show_line_diagnostics()
augroup END
]],
false
)
end
end
2021-09-06 13:29:42 +02:00
2021-09-12 16:35:38 +02:00
local system_name = ""
2021-09-06 13:29:42 +02:00
-- Lua Language Server
if vim.fn.has("mac") == 1 then
2021-11-17 12:54:51 +01:00
system_name = "macOS"
2021-09-06 13:29:42 +02:00
elseif vim.fn.has("unix") == 1 then
2021-11-17 12:54:51 +01:00
system_name = "Linux"
2021-09-06 13:29:42 +02:00
elseif vim.fn.has("win32") == 1 then
2021-11-17 12:54:51 +01:00
system_name = "Windows"
2021-09-06 13:29:42 +02:00
else
2021-11-17 12:54:51 +01:00
print("Unsupported system for sumneko")
2021-09-06 13:29:42 +02:00
end
local sumneko_root_path = "/home/jim/.local/share/lua-language-server"
local sumneko_binary = sumneko_root_path .. "/bin/" .. system_name .. "/lua-language-server"
local runtime_path = vim.split(package.path, ";")
table.insert(runtime_path, "lua/?.lua")
table.insert(runtime_path, "lua/?/init.lua")
nvim_lsp.sumneko_lua.setup {
2021-11-17 12:54:51 +01:00
on_attach = on_attach,
capabilities = lsp_status.capabilities,
cmd = {sumneko_binary, "-E", sumneko_root_path .. "/main.lua"},
settings = {
Lua = {
runtime = {
-- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
version = "LuaJIT",
-- Setup your lua path
path = runtime_path
},
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
}
}
}
2021-09-06 13:29:42 +02:00
}
-- Go Language Server
nvim_lsp.gopls.setup {
2021-11-17 12:54:51 +01:00
on_attach = on_attach,
capabilities = lsp_status.capabilities
}
2021-09-06 13:29:42 +02:00
2021-09-12 16:35:38 +02:00
-- Html Setup
nvim_lsp.html.setup {
2021-11-17 12:54:51 +01:00
on_attach = on_attach,
capabilities = lsp_status.capabilities,
filetypes = {"html"}
2021-09-12 16:35:38 +02:00
}
2021-09-06 13:29:42 +02:00
-- Svelte Language Server
nvim_lsp.svelte.setup {
2021-11-17 12:54:51 +01:00
on_attach = on_attach,
capabilities = lsp_status.capabilities
2021-09-06 13:29:42 +02:00
}
-- Typescript Language Server
local function organize_imports()
2021-11-17 12:54:51 +01:00
local params = {
command = "_typescript.organizeImports",
arguments = {vim.api.nvim_buf_get_name(0)},
title = ""
}
vim.lsp.buf.execute_command(params)
end
2021-09-06 13:29:42 +02:00
nvim_lsp.tsserver.setup {
2021-11-17 12:54:51 +01:00
on_attach = on_attach,
capabilities = lsp_status.capabilities,
commands = {
OrganizeImports = {
organize_imports,
description = "Organize Imports"
}
}
2021-09-06 13:29:42 +02:00
}
-- Setup diagnostics formaters and linters for non LSP provided files
nvim_lsp.diagnosticls.setup {
2021-11-17 12:54:51 +01:00
on_attach = on_attach,
capabilities = lsp_status.capabilities,
cmd = {"diagnostic-languageserver", "--stdio"},
filetypes = {
"lua",
"sh",
"markdown",
"json",
2021-11-08 01:29:28 +01:00
"jsonc",
2021-11-17 12:54:51 +01:00
"yaml",
"toml"
},
init_options = {
linters = {
shellcheck = {
command = "shellcheck",
debounce = 100,
args = {"--format", "json", "-"},
sourceName = "shellcheck",
parseJson = {
line = "line",
column = "column",
endLine = "endLine",
endColumn = "endColumn",
message = "${message} [${code}]",
security = "level"
},
securities = {
error = "error",
warning = "warning",
info = "info",
style = "hint"
}
},
markdownlint = {
command = "markdownlint",
isStderr = true,
debounce = 100,
args = {"--stdin"},
offsetLine = 0,
offsetColumn = 0,
sourceName = "markdownlint",
formatLines = 1,
formatPattern = {
"^.*?:\\s?(\\d+)(:(\\d+)?)?\\s(MD\\d{3}\\/[A-Za-z0-9-/]+)\\s(.*)$",
{
line = 1,
column = 3,
message = {4}
}
}
}
},
filetypes = {
sh = "shellcheck",
markdown = "markdownlint"
},
formatters = {
shfmt = {
command = "shfmt",
args = {"-i", "2", "-bn", "-ci", "-sr"}
},
prettier = {
command = "prettier",
args = {"--stdin-filepath", "%filepath"}
}
},
formatFiletypes = {
sh = "shfmt",
json = "prettier",
yaml = "prettier",
toml = "prettier",
markdown = "prettier",
lua = "prettier"
}
}
}
-- Enable diagnostics
vim.lsp.handlers["textDocument/publishDiagnostics"] =
2021-11-17 12:54:51 +01:00
vim.lsp.with(
vim.lsp.diagnostic.on_publish_diagnostics,
{
underline = true,
virtual_text = false,
signs = true,
update_in_insert = true
}
)