lspconfig.lua 13 KB

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