summaryrefslogtreecommitdiff
path: root/lua/plugins/lsp.lua
blob: 9f6075cc75d2aaf268bc8fe228d5c36c8f0b7454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
return {
	{
		"williamboman/mason-lspconfig.nvim",
		dependencies = {
			"williamboman/mason.nvim",
		},
		event = { "BufReadPre", "BufNewFile" },
	},
	{
		"williamboman/mason.nvim",
		cmd = "Mason",
		config = function()
			local mason = require("mason")
			local mlsp = require("mason-lspconfig")
			mason.setup({
				ui = {
					icons = {
						package_installed = "",
						package_not_installed = "",
						package_pending = "",
					},
				},
			})
			-- Global so it can be used by both mason and lspconfig ;)
			local myServers = {
				-- LSPs:
				"gopls",
				"golangci_lint_ls",
				"lua_ls",
				"bashls",
				"graphql",
				"texlab",
				"clangd",
				"marksman",
				"dockerls",
				"ruff_lsp",
				"docker_compose_language_service",
				"rust_analyzer",
			}
			mlsp.setup({
				ensure_installed = myServers,
				automatic_installation = false,
			})
		end,
	},
	{
		"neovim/nvim-lspconfig",
		event = { "BufReadPre", "BufNewFile" },
		dependencies = {
			"hrsh7th/nvim-cmp",
			"hrsh7th/cmp-nvim-lsp",
			"nvim-telescope/telescope.nvim",
		},
		config = function()
			-- Hide inline virtual text and use only the icons for debbuging/tips
			vim.diagnostic.config({
				virtual_text = false,
				signs = {
					text = {
						[vim.diagnostic.severity.ERROR] = "",
						[vim.diagnostic.severity.WARN] = "",
						[vim.diagnostic.severity.HINT] = "󰈈",
						[vim.diagnostic.severity.INFO] = "",
					},
				},
				underline = true,
			})

			local lsp = require("lspconfig")
			local lspCaps = require("cmp_nvim_lsp").default_capabilities()
			local lspMaps = function()
				vim.keymap.set("n", "K", vim.lsp.buf.hover, {
					buffer = 0,
					desc = "Show object description on hover",
				})

				vim.keymap.set(
					"n",
					"<leader>r",
					vim.lsp.buf.rename,
					{ buffer = 0, desc = "Rename object across all occurences" }
				)

				vim.keymap.set(
					"n",
					"gd",
					vim.lsp.buf.definition,
					{ buffer = 0, desc = "Go to the location where the object is defined" }
				)

				vim.keymap.set(
					"n",
					"gt",
					vim.lsp.buf.type_definition,
					{ buffer = 0, desc = "Go to the definition of the objects type" }
				)

				vim.keymap.set(
					"n",
					"gi",
					vim.lsp.buf.implementation,
					{ buffer = 0, desc = "Go to the method implementation" }
				)

				vim.keymap.set(
					"n",
					"gr",
					vim.lsp.buf.references,
					{ buffer = 0, desc = "Go to references of the object" }
				)

				vim.keymap.set("n", "<leader>fa", vim.lsp.buf.code_action, { buffer = 0, desc = "" })
				vim.keymap.set(
					"n",
					"]d",
					vim.diagnostic.goto_next,
					{ buffer = 0, desc = "Go to the next diagnostic/issue" }
				)

				vim.keymap.set(
					"n",
					"[d",
					vim.diagnostic.goto_prev,
					{ buffer = 0, desc = "Go to the previous diagnostic/issue" }
				)

				-- S is the same as cc, I'd rather use it for something more useful
				vim.keymap.set(
					"n",
					"S",
					vim.diagnostic.open_float,
					{ buffer = 0, desc = "View diagnostics information in a floating window" }
				)
			end

			-- Configure every lsp installed and managed by mason
			-- TODO: automate this with a global myServers table
			local myServers = {
				"gopls",
				"golangci_lint_ls",
				"lua_ls",
				"bashls",
				"dockerls",
				"docker_compose_language_service",
				"graphql",
				"texlab",
				"ruff_lsp",
				"clangd",
				"marksman",
			}
			for _, server in ipairs(myServers) do
				lsp[server].setup({
					capabilities = lspCaps,
					on_attach = lspMaps,
				})
			end

			-- Non standard language settings:
			lsp.lua_ls.setup({
				capabilities = lspCaps,
				on_attach = lspMaps,
				settings = {
					Lua = {
						runtime = {
							-- Tell the language server which version of Lua you're using
							-- -- (most likely LuaJIT in the case of Neovim)
							version = "LuaJIT",
						},
						diagnostic = {
							-- Get the language server to recognize the `vim` global
							globals = {
								"vim",
								"require",
							},
						},
						workspace = {
							checkThirdParty = false,
							library = vim.api.nvim_get_runtime_file("", true),
						},
						telemetry = {
							enable = false,
						},
					},
				},
			})
			lsp.clangd.setup({
				on_attach = function(client)
					client.server_capabilities.documentFormattingProvider = false
					client.server_capabilities.documentRangeFormattingProvider = false
					lspMaps()
				end,
			})
		end,
	},
	{
		"stevearc/conform.nvim",
		event = { "BufWritePre" },
		cmd = { "ConformInfo" },
		keys = {
			{
				-- Customize or remove this keymap to your liking
				"<leader>f",
				function()
					require("conform").format({ async = true, lsp_format = "fallback" })
				end,
				mode = "",
				desc = "Format buffer",
			},
		},
		-- Everything in opts will be passed to setup()
		opts = {
			-- Define your formatters
			formatters_by_ft = {
				lua = { "stylua" },
				javascript = { "prettierd" },
				php = { "pretty-php" },
				go = { "gofumpt", "gci", "goimports" },
				yaml = { "yamlfix" },
			},
			-- Set up format-on-save
			format_on_save = { timeout_ms = 500, lsp_format = "fallback" },
			-- Customize formatters
			formatters = {
				shfmt = {
					prepend_args = { "-i", "2" },
				},
			},
		},
		init = function()
			-- If you want the formatexpr, here is the place to set it
			vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
		end,
	},
	{
		"zapling/mason-conform.nvim",
		event = { "BufReadPre", "BufNewFile" },
		dependencies = {
			"williamboman/mason.nvim",
			"stevearc/conform.nvim",
		},
		config = true,
	},
}