lua/josean/plugins/lsp/lspconfig.lua (5385B)
1 return {
2 "neovim/nvim-lspconfig",
3 event = { "BufReadPre", "BufNewFile" },
4 dependencies = {
5 "hrsh7th/cmp-nvim-lsp",
6 { "antosha417/nvim-lsp-file-operations", config = true },
7 },
8 config = function()
9 -- import lspconfig plugin
10 local lspconfig = require("lspconfig")
11
12 -- import cmp-nvim-lsp plugin
13 local cmp_nvim_lsp = require("cmp_nvim_lsp")
14
15 local keymap = vim.keymap -- for conciseness
16
17 local opts = { noremap = true, silent = true }
18 local on_attach = function(client, bufnr)
19 opts.buffer = bufnr
20
21 -- set keybinds
22 opts.desc = "Show LSP references"
23 keymap.set("n", "gR", "<cmd>Telescope lsp_references<CR>", opts) -- show definition, references
24
25 opts.desc = "Go to declaration"
26 keymap.set("n", "gD", vim.lsp.buf.declaration, opts) -- go to declaration
27
28 opts.desc = "Show LSP definitions"
29 keymap.set("n", "gd", "<cmd>Telescope lsp_definitions<CR>", opts) -- show lsp definitions
30
31 opts.desc = "Show LSP implementations"
32 keymap.set("n", "gi", "<cmd>Telescope lsp_implementations<CR>", opts) -- show lsp implementations
33
34 opts.desc = "Show LSP type definitions"
35 keymap.set("n", "gt", "<cmd>Telescope lsp_type_definitions<CR>", opts) -- show lsp type definitions
36
37 opts.desc = "See available code actions"
38 keymap.set({ "n", "v" }, "<leader>ca", vim.lsp.buf.code_action, opts) -- see available code actions, in visual mode will apply to selection
39
40 opts.desc = "Smart rename"
41 keymap.set("n", "<leader>rn", vim.lsp.buf.rename, opts) -- smart rename
42
43 opts.desc = "Show buffer diagnostics"
44 keymap.set("n", "<leader>D", "<cmd>Telescope diagnostics bufnr=0<CR>", opts) -- show diagnostics for file
45
46 opts.desc = "Show line diagnostics"
47 keymap.set("n", "<leader>d", vim.diagnostic.open_float, opts) -- show diagnostics for line
48
49 opts.desc = "Go to previous diagnostic"
50 keymap.set("n", "[d", vim.diagnostic.goto_prev, opts) -- jump to previous diagnostic in buffer
51
52 opts.desc = "Go to next diagnostic"
53 keymap.set("n", "]d", vim.diagnostic.goto_next, opts) -- jump to next diagnostic in buffer
54
55 opts.desc = "Show documentation for what is under cursor"
56 keymap.set("n", "K", vim.lsp.buf.hover, opts) -- show documentation for what is under cursor
57
58 opts.desc = "Restart LSP"
59 keymap.set("n", "<leader>rs", ":LspRestart<CR>", opts) -- mapping to restart lsp if necessary
60 end
61
62 -- used to enable autocompletion (assign to every lsp server config)
63 local capabilities = cmp_nvim_lsp.default_capabilities()
64
65 -- Change the Diagnostic symbols in the sign column (gutter)
66 -- (not in youtube nvim video)
67 local signs = { Error = " ", Warn = " ", Hint = " ", Info = " " }
68 for type, icon in pairs(signs) do
69 local hl = "DiagnosticSign" .. type
70 vim.fn.sign_define(hl, { text = icon, texthl = hl, numhl = "" })
71 end
72
73 -- configure html server
74 lspconfig["html"].setup({
75 capabilities = capabilities,
76 on_attach = on_attach,
77 })
78
79 -- configure typescript server with plugin
80 lspconfig["tsserver"].setup({
81 capabilities = capabilities,
82 on_attach = on_attach,
83 })
84
85 -- configure css server
86 lspconfig["cssls"].setup({
87 capabilities = capabilities,
88 on_attach = on_attach,
89 })
90
91 -- configure tailwindcss server
92 lspconfig["tailwindcss"].setup({
93 capabilities = capabilities,
94 on_attach = on_attach,
95 })
96
97 -- configure svelte server
98 lspconfig["svelte"].setup({
99 capabilities = capabilities,
100 on_attach = function(client, bufnr)
101 on_attach(client, bufnr)
102
103 vim.api.nvim_create_autocmd("BufWritePost", {
104 pattern = { "*.js", "*.ts" },
105 callback = function(ctx)
106 if client.name == "svelte" then
107 client.notify("$/onDidChangeTsOrJsFile", { uri = ctx.file })
108 end
109 end,
110 })
111 end,
112 })
113
114 -- configure prisma orm server
115 lspconfig["prismals"].setup({
116 capabilities = capabilities,
117 on_attach = on_attach,
118 })
119
120 -- configure graphql language server
121 lspconfig["graphql"].setup({
122 capabilities = capabilities,
123 on_attach = on_attach,
124 filetypes = { "graphql", "gql", "svelte", "typescriptreact", "javascriptreact" },
125 })
126
127 -- configure emmet language server
128 lspconfig["emmet_ls"].setup({
129 capabilities = capabilities,
130 on_attach = on_attach,
131 filetypes = { "html", "typescriptreact", "javascriptreact", "css", "sass", "scss", "less", "svelte" },
132 })
133
134 -- configure python server
135 lspconfig["pyright"].setup({
136 capabilities = capabilities,
137 on_attach = on_attach,
138 })
139
140 -- configure lua server (with special settings)
141 lspconfig["lua_ls"].setup({
142 capabilities = capabilities,
143 on_attach = on_attach,
144 settings = { -- custom settings for lua
145 Lua = {
146 -- make the language server recognize "vim" global
147 diagnostics = {
148 globals = { "vim" },
149 },
150 workspace = {
151 -- make language server aware of runtime files
152 library = {
153 [vim.fn.expand("$VIMRUNTIME/lua")] = true,
154 [vim.fn.stdpath("config") .. "/lua"] = true,
155 },
156 },
157 },
158 },
159 })
160 end,
161 }