DEVELOPMENT ENVIRONMENT

~alex/dotfiles

ref: d5a5353b4f9d6a7d7a5defb4c4420d00871d4a5f dotfiles/nvim/lua/plugins/lsp.lua -rw-r--r-- 6.8 KiB
d5a5353b — Alejandro Laguna chore: cleanup neovim config 16 days ago
                                                                                
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
244
245
246
247
248
249
250
251
return {
	"neovim/nvim-lspconfig",

	dependencies = {
		{ "williamboman/mason.nvim", config = true },
		"williamboman/mason-lspconfig.nvim",
		"WhoIsSethDaniel/mason-tool-installer.nvim",
		{ "j-hui/fidget.nvim", opts = {} },
		"hrsh7th/cmp-nvim-lsp",
	},

	config = function()
		----------------------------------------------------------------------
		-- LSP Attach
		----------------------------------------------------------------------

		vim.api.nvim_create_autocmd("LspAttach", {
			group = vim.api.nvim_create_augroup("kickstart-lsp-attach", { clear = true }),

			callback = function(event)
				local map = function(keys, func, desc, mode)
					mode = mode or "n"

					vim.keymap.set(mode, keys, func, {
						buffer = event.buf,
						desc = "LSP: " .. desc,
					})
				end

				------------------------------------------------------------------
				-- Keymaps
				------------------------------------------------------------------

				map("gd", require("telescope.builtin").lsp_definitions, "[G]oto [D]efinition")
				map("gr", require("telescope.builtin").lsp_references, "[G]oto [R]eferences")
				map("gI", require("telescope.builtin").lsp_implementations, "[G]oto [I]mplementation")
				map("<leader>D", require("telescope.builtin").lsp_type_definitions, "Type [D]efinition")
				map("<leader>ds", require("telescope.builtin").lsp_document_symbols, "[D]ocument [S]ymbols")
				map("<leader>ws", require("telescope.builtin").lsp_dynamic_workspace_symbols, "[W]orkspace [S]ymbols")

				map("<leader>rn", vim.lsp.buf.rename, "[R]e[n]ame")
				map("<leader>ca", vim.lsp.buf.code_action, "[C]ode [A]ction", { "n", "x" })

				map("gD", vim.lsp.buf.declaration, "[G]oto [D]eclaration")

				------------------------------------------------------------------
				-- Client
				------------------------------------------------------------------

				local client = vim.lsp.get_client_by_id(event.data.client_id)

				------------------------------------------------------------------
				-- Document Highlight
				------------------------------------------------------------------

				if client and client:supports_method(vim.lsp.protocol.Methods.textDocument_documentHighlight) then
					local highlight_augroup = vim.api.nvim_create_augroup("kickstart-lsp-highlight", { clear = false })

					vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
						buffer = event.buf,
						group = highlight_augroup,
						callback = vim.lsp.buf.document_highlight,
					})

					vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
						buffer = event.buf,
						group = highlight_augroup,
						callback = vim.lsp.buf.clear_references,
					})

					vim.api.nvim_create_autocmd("LspDetach", {
						group = vim.api.nvim_create_augroup("kickstart-lsp-detach", { clear = true }),

						callback = function(event2)
							vim.lsp.buf.clear_references()

							vim.api.nvim_clear_autocmds({
								group = "kickstart-lsp-highlight",
								buffer = event2.buf,
							})
						end,
					})
				end

				------------------------------------------------------------------
				-- Inlay Hints
				------------------------------------------------------------------

				if client and client:supports_method(vim.lsp.protocol.Methods.textDocument_inlayHint) then
					map("<leader>th", function()
						vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = event.buf }))
					end, "[T]oggle Inlay [H]ints")
				end
			end,
		})

		----------------------------------------------------------------------
		-- Capabilities
		----------------------------------------------------------------------

		local capabilities = vim.lsp.protocol.make_client_capabilities()

		capabilities = vim.tbl_deep_extend("force", capabilities, require("cmp_nvim_lsp").default_capabilities())

		----------------------------------------------------------------------
		-- Servers
		----------------------------------------------------------------------

		local servers = {
			lua_ls = {
				settings = {
					Lua = {
						completion = {
							callSnippet = "Replace",
						},
					},
				},
			},

			texlab = {
				settings = {
					texlab = {
						build = {
							executable = "latexmk",
							args = {
								"-pdf",
								"-interaction=nonstopmode",
								"-synctex=1",
								"%f",
							},
							onSave = true,
						},

						forwardSearch = {
							executable = "zathura",
							args = {
								"--synctex-forward",
								"%l:1:%f",
								"%p",
							},
						},

						chktex = {
							onOpenAndSave = true,
							onEdit = false,
						},
					},
				},
			},

			zls = {
				settings = {
					zls = {
						zig_exe_path = "/usr/bin/zig",
					},
				},
			},

			hls = {
				filetypes = {
					"haskell",
					"lhaskell",
					"cabal",
				},

				settings = {
					haskell = {
						checkProject = true,
						diagnosticsOnChange = true,
						completionSnippetsOn = true,
						maxCompletions = 40,
					},
				},
			},
		}

		----------------------------------------------------------------------
		-- Mason
		----------------------------------------------------------------------

		require("mason").setup()

		-- Mason names for servers in the `servers` table
		local server_to_mason = {
			lua_ls = "lua-language-server",
			texlab = "texlab",
			zls = "zls",
			hls = "haskell-language-server",
		}

		local ensure_installed = {}
		for server_name, _ in pairs(servers) do
			table.insert(ensure_installed, server_to_mason[server_name] or server_name)
		end

		vim.list_extend(ensure_installed, {
			"stylua",
			"fourmolu",
		})

		require("mason-tool-installer").setup({
			ensure_installed = ensure_installed,
		})

		----------------------------------------------------------------------
		-- Mason LSPConfig
		----------------------------------------------------------------------

		require("mason-lspconfig").setup({
			handlers = {
				-- Skip julials here as it is handled manually
				["julials"] = function() end,

				-- Default handler for all other servers
				function(server_name)
					local server_opts = servers[server_name] or {}

					-- Merge capabilities
					server_opts.capabilities =
						vim.tbl_deep_extend("force", {}, capabilities, server_opts.capabilities or {})

					-- Use new Neovim 0.11+ APIs:
					-- config() defines/extends the configuration
					-- enable() starts/attaches based on filetype and root detection
					vim.lsp.config(server_name, server_opts)
					vim.lsp.enable(server_name)
				end,
			},
		})

		----------------------------------------------------------------------
		-- Julia
		----------------------------------------------------------------------

		vim.lsp.config("julials", {
			cmd = {
				"julia",
				"--startup-file=no",
				"--history-file=no",
				"-e",
				"using LanguageServer, SymbolServer; runserver()",
			},

			filetypes = { "julia" },

			capabilities = capabilities,
		})

		vim.lsp.enable("julials")
	end,
}