summaryrefslogtreecommitdiff
path: root/dot_config/nvim/lua/plugins/lsp.lua
blob: b9445722be5ba607dfda73b9d45c86e0416277f0 (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
return {
	{
		"VonHeikemen/lsp-zero.nvim",
		branch = "v4.x",
		lazy = true,
		config = false,
	},

	{
		"williamboman/mason.nvim",
		lazy = false,
		config = true,
	},

	-- Autocompletion
	{
		"hrsh7th/nvim-cmp",
		event = { "InsertEnter", "CmdlineEnter" },
		dependencies = {
			"L3MON4D3/LuaSnip", -- Snippet engine
			"onsails/lspkind.nvim", -- Pretty formatting in the completion menu

			-- Completion sources
			"hrsh7th/cmp-nvim-lsp",
			"hrsh7th/cmp-nvim-lsp-signature-help",
			"hrsh7th/cmp-buffer",
			"hrsh7th/cmp-path",
			"saadparwaiz1/cmp_luasnip",
		},
		config = function()
			local cmp = require("cmp")
			local luasnip = require("luasnip")

			cmp.setup({
				completion = {
					-- See :help completeopt
					completeopt = "menu,menuone,noinsert,preview",
				},
				snippet = {
					expand = function(args)
						luasnip.lsp_expand(args.body)
					end,
				},
				mapping = cmp.mapping.preset.insert({
					["<C-n>"] = cmp.mapping.select_next_item({ behavior = cmp.SelectBehavior.Insert }),
					["<C-p>"] = cmp.mapping.select_prev_item({ behavior = cmp.SelectBehavior.Insert }),
					["<C-b>"] = cmp.mapping.scroll_docs(-4),
					["<C-f>"] = cmp.mapping.scroll_docs(4),

					-- Ctrl+Space does not currently work in Neovim core on Windows
					-- https://github.com/neovim/neovim/issues/8435
					-- ["<C-Space>"] = cmp.mapping.complete(),
					["<C-c>"] = cmp.mapping.complete(),

					["<C-e>"] = cmp.mapping.abort(),
					["<C-y>"] = cmp.mapping.confirm({
						-- Accept currently selected item
						behavior = cmp.ConfirmBehavior.Insert,
						select = true,
					}),
					["<C-S-y>"] = cmp.mapping.confirm({
						-- Accept currently selected item, replacing stuff as needed
						behavior = cmp.ConfirmBehavior.Replace,
						select = true,
					}),
					["<CR>"] = cmp.mapping({
						-- https://github.com/hrsh7th/nvim-cmp/wiki/Example-mappings#safely-select-entries-with-cr
						i = function(fallback)
							-- In insert mode, only use Enter to complete something if cmp
							-- is active and the user has explicitly selected an item
							if cmp.visible() and cmp.get_active_entry() then
								cmp.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = false })
							else
								fallback()
							end
						end,
						s = cmp.mapping.confirm({ select = true }),
						c = cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true }),
					}),
				}),
				sorting = {
					priority_weight = 2,
					comparators = {
						cmp.config.compare.exact,

						-- Mostly default sort order from cmp
						cmp.config.compare.offset,
						-- cmp.config.compare.scopes, --this is commented in nvim-cmp too
						cmp.config.compare.score,
						cmp.config.compare.recently_used,
						cmp.config.compare.locality,
						cmp.config.compare.kind,
						cmp.config.compare.sort_text,
						cmp.config.compare.length,
						cmp.config.compare.order,
					},
				},
				sources = {
					-- LSP results should be first priority
					{ name = "nvim_lsp", group_index = 1 },

					{ name = "nvim_lsp_signature_help", group_index = 2 },
					{ name = "lazydev", group_index = 2 },
					{ name = "luasnip", group_index = 2 },
					{ name = "path", group_index = 2 },
					{ name = "buffer", group_index = 2, keyword_length = 5 },
				},
				---@diagnostic disable-next-line: missing-fields
				formatting = {
					format = require("lspkind").cmp_format({
						mode = "symbol_text",
						maxwidth = function()
							return math.floor(0.45 * vim.o.columns)
						end,
						ellipsis_char = "...",
						show_labelDetails = true,
					}),
				},
				experimental = {
					ghost_text = {
						hl_group = "LspCodeLens",
					},
				},
				window = {
					completion = cmp.config.window.bordered(),
					documentation = cmp.config.window.bordered(),
				},
			})
		end,
	},

	-- LSP
	{
		"neovim/nvim-lspconfig",
		cmd = { "LspInfo", "LspInstall", "LspStart" },
		event = { "BufReadPre", "BufNewFile" },
		dependencies = {
			{ "hrsh7th/cmp-nvim-lsp" },
			{ "williamboman/mason.nvim" },
			{ "williamboman/mason-lspconfig.nvim" },
		},
		config = function()
			local lsp_zero = require("lsp-zero")

			lsp_zero.extend_lspconfig({
				sign_text = true,
				capabilities = require("cmp_nvim_lsp").default_capabilities(),
			})

			local border = {
				{ "🭽", "FloatBorder" },
				{ "▔", "FloatBorder" },
				{ "🭾", "FloatBorder" },
				{ "▕", "FloatBorder" },
				{ "🭿", "FloatBorder" },
				{ "▁", "FloatBorder" },
				{ "🭼", "FloatBorder" },
				{ "▏", "FloatBorder" },
			}

			local border_handlers = {
				["textDocument/hover"] = vim.lsp.with(vim.lsp.handlers.hover, { border = border }),
				["textDocument/signatureHelp"] = vim.lsp.with(vim.lsp.handlers.signature_help, { border = border }),
			}

			require("mason-lspconfig").setup({
				ensure_installed = { "basedpyright", "ruff", "lua_ls", "gopls", "rust_analyzer" },
				handlers = {
					-- this first function is the "default handler"
					-- it applies to every language server without a "custom handler"
					function(server_name)
						require("lspconfig")[server_name].setup({ handlers = border_handlers })
					end,

					["basedpyright"] = function()
						local lspconfig = require("lspconfig")
						lspconfig.basedpyright.setup({
							settings = {
								basedpyright = {
									typeCheckingMode = "standard",
								},
							},
						})
					end,
				},
			})

			vim.lsp.inlay_hint.enable()
		end,
	},

	-- Formatting
	{
		"stevearc/conform.nvim",
		config = function()
			local conform = require("conform")
			local prettier = { "prettierd", "prettier" }
			conform.setup({
				formatters_by_ft = {
					lua = { "stylua" },
					python = { "ruff", lsp_format = "fallback" },
					go = { "gopls" },
					rust = { "clippy", lsp_format = "fallback" },
					markdown = prettier,
				},
				format_after_save = {
					async = true,
					stop_after_first = true,
					lsp_format = "fallback",
				},
			})
		end,
	},

	-- Code actions
	{
		"aznhe21/actions-preview.nvim",
		config = function()
			vim.keymap.set({ "v", "n" }, "<leader>ca", require("actions-preview").code_actions)
		end,
	},

	-- Cool neovim dev stuff
	{
		"folke/lazydev.nvim",
		ft = "lua", -- only load on lua files
		opts = {
			library = {
				-- See the configuration section for more details
				-- Load luvit types when the `vim.uv` word is found
				{ path = "luvit-meta/library", words = { "vim%.uv" } },
			},
		},
	},

	{ "Bilal2453/luvit-meta", lazy = true }, -- optional `vim.uv` typings
}