dev-environment-files/.config/nvim/lua/josean/plugins/lsp/lspconfig.lua

112 lines
4 KiB
Lua
Raw Normal View History

2022-10-17 20:40:36 +02:00
-- import lspconfig plugin safely
local lspconfig_status, lspconfig = pcall(require, "lspconfig")
if not lspconfig_status then
return
2022-10-17 20:40:36 +02:00
end
-- import cmp-nvim-lsp plugin safely
local cmp_nvim_lsp_status, cmp_nvim_lsp = pcall(require, "cmp_nvim_lsp")
if not cmp_nvim_lsp_status then
return
2022-10-17 20:40:36 +02:00
end
-- import typescript plugin safely
local typescript_setup, typescript = pcall(require, "typescript")
if not typescript_setup then
return
2022-10-17 20:40:36 +02:00
end
local keymap = vim.keymap -- for conciseness
-- enable keybinds only for when lsp server available
local on_attach = function(client, bufnr)
-- keybind options
local opts = { noremap = true, silent = true, buffer = bufnr }
2022-10-17 20:40:36 +02:00
-- set keybinds
keymap.set("n", "gf", "<cmd>Lspsaga lsp_finder<CR>", opts) -- show definition, references
keymap.set("n", "gD", "<Cmd>lua vim.lsp.buf.declaration()<CR>", opts) -- got to declaration
keymap.set("n", "gd", "<cmd>Lspsaga peek_definition<CR>", opts) -- see definition and make edits in window
keymap.set("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opts) -- go to implementation
keymap.set("n", "<leader>ca", "<cmd>Lspsaga code_action<CR>", opts) -- see available code actions
keymap.set("n", "<leader>rn", "<cmd>Lspsaga rename<CR>", opts) -- smart rename
keymap.set("n", "<leader>D", "<cmd>Lspsaga show_line_diagnostics<CR>", opts) -- show diagnostics for line
keymap.set("n", "<leader>d", "<cmd>Lspsaga show_cursor_diagnostics<CR>", opts) -- show diagnostics for cursor
keymap.set("n", "[d", "<cmd>Lspsaga diagnostic_jump_prev<CR>", opts) -- jump to previous diagnostic in buffer
keymap.set("n", "]d", "<cmd>Lspsaga diagnostic_jump_next<CR>", opts) -- jump to next diagnostic in buffer
keymap.set("n", "K", "<cmd>Lspsaga hover_doc<CR>", opts) -- show documentation for what is under cursor
keymap.set("n", "<leader>o", "<cmd>LSoutlineToggle<CR>", opts) -- see outline on right hand side
2022-10-17 20:40:36 +02:00
-- typescript specific keymaps (e.g. rename file and update imports)
if client.name == "tsserver" then
keymap.set("n", "<leader>rf", ":TypescriptRenameFile<CR>") -- rename file and update imports
keymap.set("n", "<leader>oi", ":TypescriptOrganizeImports<CR>") -- organize imports (not in youtube nvim video)
keymap.set("n", "<leader>ru", ":TypescriptRemoveUnused<CR>") -- remove unused variables (not in youtube nvim video)
end
2022-10-17 20:40:36 +02:00
end
-- used to enable autocompletion (assign to every lsp server config)
local capabilities = cmp_nvim_lsp.default_capabilities()
-- 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 = "" })
2022-10-17 20:40:36 +02:00
end
-- configure html server
lspconfig["html"].setup({
capabilities = capabilities,
on_attach = on_attach,
2022-10-17 20:40:36 +02:00
})
-- configure typescript server with plugin
typescript.setup({
server = {
capabilities = capabilities,
on_attach = on_attach,
},
2022-10-17 20:40:36 +02:00
})
-- configure css server
lspconfig["cssls"].setup({
capabilities = capabilities,
on_attach = on_attach,
2022-10-17 20:40:36 +02:00
})
-- configure tailwindcss server
lspconfig["tailwindcss"].setup({
capabilities = capabilities,
on_attach = on_attach,
2022-10-17 20:40:36 +02:00
})
2022-11-06 22:35:01 +01:00
-- configure emmet language server
lspconfig["emmet_ls"].setup({
capabilities = capabilities,
on_attach = on_attach,
filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "svelte" },
})
2022-10-17 20:40:36 +02:00
-- configure lua server (with special settings)
lspconfig["lua_ls"].setup({
capabilities = capabilities,
on_attach = on_attach,
settings = { -- custom settings for lua
Lua = {
-- make the language server recognize "vim" global
diagnostics = {
globals = { "vim" },
},
workspace = {
-- make language server aware of runtime files
library = {
[vim.fn.expand("$VIMRUNTIME/lua")] = true,
[vim.fn.stdpath("config") .. "/lua"] = true,
},
},
},
},
2022-10-17 20:40:36 +02:00
})