Neovim Config
1return {
2 -- Main LSP Configuration
3 "neovim/nvim-lspconfig",
4 cond = function()
5 if os.getenv("WICKED_VIM_MODE") == "OBSIDIAN" then
6 return false
7 end
8 if os.getenv("WICKED_VIM_MODE") == nil then
9 return true
10 end
11 end,
12 dependencies = {
13 -- Automatically install LSPs and related tools to stdpath for Neovim
14 -- Mason must be loaded before its dependents so we need to set it up here.
15 -- NOTE: `opts = {}` is the same as calling `require('mason').setup({})`
16 { "mason-org/mason.nvim", opts = {} },
17 "mason-org/mason-lspconfig.nvim",
18 "WhoIsSethDaniel/mason-tool-installer.nvim",
19
20 -- Useful status updates for LSP.
21 { "j-hui/fidget.nvim", opts = {} },
22
23 -- Allows extra capabilities provided by blink.cmp
24 "saghen/blink.cmp",
25 },
26 config = function()
27 -- This function gets run when an LSP attaches to a particular buffer.
28 -- That is to say, every time a new file is opened that is associated with
29 -- an lsp (for example, opening `main.rs` is associated with `rust_analyzer`) this
30 -- function will be executed to configure the current buffer
31 vim.api.nvim_create_autocmd("LspAttach", {
32 group = vim.api.nvim_create_augroup("kickstart-lsp-attach", { clear = true }),
33 callback = function(event)
34 -- NOTE: Remember that Lua is a real programming language, and as such it is possible
35 -- to define small helper and utility functions so you don't have to repeat yourself.
36 --
37 -- In this case, we create a function that lets us more easily define mappings specific
38 -- for LSP related items. It sets the mode, buffer and description for us each time.
39 local map = function(keys, func, desc, mode)
40 mode = mode or "n"
41 vim.keymap.set(mode, keys, func, { buffer = event.buf, desc = "LSP: " .. desc })
42 end
43
44 -- Rename the variable under your cursor.
45 -- Most Language Servers support renaming across files, etc.
46 map("grn", vim.lsp.buf.rename, "[R]e[n]ame")
47
48 -- Execute a code action, usually your cursor needs to be on top of an error
49 -- or a suggestion from your LSP for this to activate.
50 map("gra", vim.lsp.buf.code_action, "[G]oto Code [A]ction", { "n", "x" })
51
52 -- Find references for the word under your cursor.
53 map("grr", require("telescope.builtin").lsp_references, "[G]oto [R]eferences")
54
55 -- Jump to the implementation of the word under your cursor.
56 -- Useful when your language has ways of declaring types without an actual implementation.
57 map("gI", require("telescope.builtin").lsp_implementations, "[G]oto [I]mplementation")
58
59 -- Jump to the definition of the word under your cursor.
60 -- This is where a variable was first declared, or where a function is defined, etc.
61 -- To jump back, press <C-t>.
62 map("gd", require("telescope.builtin").lsp_definitions, "[G]oto [D]efinition")
63
64 -- WARN: This is not Goto Definition, this is Goto Declaration.
65 -- For example, in C this would take you to the header.
66 map("gD", vim.lsp.buf.declaration, "[G]oto [D]eclaration")
67
68 -- Fuzzy find all the symbols in your current document.
69 -- Symbols are things like variables, functions, types, etc.
70 map("gO", require("telescope.builtin").lsp_document_symbols, "Open Document Symbols")
71
72 -- Fuzzy find all the symbols in your current workspace.
73 -- Similar to document symbols, except searches over your entire project.
74 map("gW", require("telescope.builtin").lsp_dynamic_workspace_symbols, "Open Workspace Symbols")
75
76 -- Jump to the type of the word under your cursor.
77 -- Useful when you're not sure what type a variable is and you want to see
78 -- the definition of its *type*, not where it was *defined*.
79 map("grt", require("telescope.builtin").lsp_type_definitions, "[G]oto [T]ype Definition")
80
81 -- This function resolves a difference between neovim nightly (version 0.11) and stable (version 0.10)
82 ---@param client vim.lsp.Client
83 ---@param method vim.lsp.protocol.Method
84 ---@param bufnr? integer some lsp support methods only in specific files
85 ---@return boolean
86 local function client_supports_method(client, method, bufnr)
87 if vim.fn.has("nvim-0.11") == 1 then
88 return client:supports_method(method, bufnr)
89 else
90 return client.supports_method(method, { bufnr = bufnr })
91 end
92 end
93
94 -- The following two autocommands are used to highlight references of the
95 -- word under your cursor when your cursor rests there for a little while.
96 -- See `:help CursorHold` for information about when this is executed
97 --
98 -- When you move your cursor, the highlights will be cleared (the second autocommand).
99 local client = vim.lsp.get_client_by_id(event.data.client_id)
100 if
101 client
102 and client_supports_method(
103 client,
104 vim.lsp.protocol.Methods.textDocument_documentHighlight,
105 event.buf
106 )
107 then
108 local highlight_augroup = vim.api.nvim_create_augroup("kickstart-lsp-highlight", { clear = false })
109 vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
110 buffer = event.buf,
111 group = highlight_augroup,
112 callback = vim.lsp.buf.document_highlight,
113 })
114
115 vim.api.nvim_create_autocmd({ "CursorMoved", "CursorMovedI" }, {
116 buffer = event.buf,
117 group = highlight_augroup,
118 callback = vim.lsp.buf.clear_references,
119 })
120
121 vim.api.nvim_create_autocmd("LspDetach", {
122 group = vim.api.nvim_create_augroup("kickstart-lsp-detach", { clear = true }),
123 callback = function(event2)
124 vim.lsp.buf.clear_references()
125 vim.api.nvim_clear_autocmds({ group = "kickstart-lsp-highlight", buffer = event2.buf })
126 end,
127 })
128 end
129
130 -- The following code creates a keymap to toggle inlay hints in your
131 -- code, if the language server you are using supports them
132 --
133 -- This may be unwanted, since they displace some of your code
134 if
135 client
136 and client_supports_method(client, vim.lsp.protocol.Methods.textDocument_inlayHint, event.buf)
137 then
138 map("<leader>th", function()
139 vim.lsp.inlay_hint.enable(not vim.lsp.inlay_hint.is_enabled({ bufnr = event.buf }))
140 end, "[T]oggle Inlay [H]ints")
141 end
142 end,
143 })
144
145 -- Diagnostic Config
146 -- See :help vim.diagnostic.Opts
147 vim.diagnostic.config({
148 severity_sort = true,
149 float = { border = "rounded", source = "if_many" },
150 underline = { severity = vim.diagnostic.severity.ERROR },
151 signs = vim.g.have_nerd_font and {
152 text = {
153 [vim.diagnostic.severity.ERROR] = " ",
154 [vim.diagnostic.severity.WARN] = " ",
155 [vim.diagnostic.severity.INFO] = " ",
156 [vim.diagnostic.severity.HINT] = " ",
157 },
158 } or {},
159 virtual_text = {
160 source = "if_many",
161 spacing = 2,
162 format = function(diagnostic)
163 local diagnostic_message = {
164 [vim.diagnostic.severity.ERROR] = diagnostic.message,
165 [vim.diagnostic.severity.WARN] = diagnostic.message,
166 [vim.diagnostic.severity.INFO] = diagnostic.message,
167 [vim.diagnostic.severity.HINT] = diagnostic.message,
168 }
169 return diagnostic_message[diagnostic.severity]
170 end,
171 },
172 })
173
174 -- LSP servers and clients are able to communicate to each other what features they support.
175 -- By default, Neovim doesn't support everything that is in the LSP specification.
176 -- When you add blink.cmp, luasnip, etc. Neovim now has *more* capabilities.
177 -- So, we create new capabilities with blink.cmp, and then broadcast that to the servers.
178 local capabilities = require("blink.cmp").get_lsp_capabilities()
179
180 -- Enable the following language servers
181 -- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
182 --
183 -- Add any additional override configuration in the following tables. Available keys are:
184 -- - cmd (table): Override the default command used to start the server
185 -- - filetypes (table): Override the default list of associated filetypes for the server
186 -- - capabilities (table): Override fields in capabilities. Can be used to disable certain LSP features.
187 -- - settings (table): Override the default settings passed when initializing the server.
188 -- For example, to see the options for `lua_ls`, you could go to: https://luals.github.io/wiki/settings/
189 local servers = {
190 -- clangd = {},
191 -- gopls = {},
192 -- pyright = {},
193 -- rust_analyzer = {},
194 -- ... etc. See `:help lspconfig-all` for a list of all the pre-configured LSPs
195 --
196 -- Some languages (like typescript) have entire language plugins that can be useful:
197 -- https://github.com/pmizio/typescript-tools.nvim
198 --
199 -- But for many setups, the LSP (`ts_ls`) will work just fine
200 -- ts_ls = {},
201 --
202
203 lua_ls = {
204 -- cmd = { ... },
205 -- filetypes = { ... },
206 -- capabilities = {},
207 settings = {
208 Lua = {
209 completion = {
210 callSnippet = "Replace",
211 },
212 -- You can toggle below to ignore Lua_LS's noisy `missing-fields` warnings
213 -- diagnostics = { disable = { 'missing-fields' } },
214 },
215 },
216 },
217 }
218
219 -- Ensure the servers and tools above are installed
220 --
221 -- To check the current status of installed tools and/or manually install
222 -- other tools, you can run
223 -- :Mason
224 --
225 -- You can press `g?` for help in this menu.
226 --
227 -- `mason` had to be setup earlier: to configure its options see the
228 -- `dependencies` table for `nvim-lspconfig` above.
229 --
230 -- You can add other tools here that you want Mason to install
231 -- for you, so that they are available from within Neovim.
232 local ensure_installed = vim.tbl_keys(servers or {})
233 vim.list_extend(ensure_installed, {
234 "stylua", -- Used to format Lua code
235 })
236 require("mason-tool-installer").setup({ ensure_installed = ensure_installed })
237
238 require("mason-lspconfig").setup({
239 ensure_installed = {}, -- explicitly set to an empty table (Kickstart populates installs via mason-tool-installer)
240 automatic_installation = false,
241 handlers = {
242 function(server_name)
243 local server = servers[server_name] or {}
244 -- This handles overriding only values explicitly passed
245 -- by the server configuration above. Useful when disabling
246 -- certain features of an LSP (for example, turning off formatting for ts_ls)
247 server.capabilities = vim.tbl_deep_extend("force", {}, capabilities, server.capabilities or {})
248 require("lspconfig")[server_name].setup(server)
249 end,
250 },
251 })
252 end,
253}