init.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. -- Initialise dependencies
  2. require 'custom.lazy'
  3. require 'custom.autocomplete'
  4. -- Set <space> as the leader key
  5. -- See `:help mapleader`
  6. -- NOTE: Must happen before plugins are required (otherwise wrong leader will be used)
  7. vim.g.mapleader = ' '
  8. vim.g.maplocalleader = ' '
  9. -- Use system clipboard for yanking/pasting
  10. vim.o.clipboard = 'unnamedplus'
  11. vim.wo.relativenumber = true
  12. -- [[ Basic Keymaps ]]
  13. -- Keymaps for better default experience
  14. -- See `:help vim.keymap.set()`
  15. vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })
  16. -- Remap for dealing with word wrap
  17. vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
  18. vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
  19. -- Diagnostic keymaps
  20. vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' })
  21. vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' })
  22. vim.keymap.set('n', '<leader>e', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' })
  23. vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' })
  24. -- Make window management nice
  25. vim.api.nvim_set_keymap('n', '<C-Left>', ':vertical resize +3<CR>', { silent = true, noremap = true })
  26. vim.api.nvim_set_keymap('n', '<C-Right>', ':vertical resize -3<CR>', { silent = true, noremap = true })
  27. vim.api.nvim_set_keymap('n', '<C-Up>', ':resize -3<CR>', { silent = true, noremap = true })
  28. vim.api.nvim_set_keymap('n', '<C-Down>', ':resize +3<CR>', { silent = true, noremap = true })
  29. vim.api.nvim_set_keymap('n', '<C-h>', '<C-w>h', { silent = true, noremap = true })
  30. vim.api.nvim_set_keymap('n', '<C-j>', '<C-w>j', { silent = true, noremap = true })
  31. vim.api.nvim_set_keymap('n', '<C-k>', '<C-w>k', { silent = true, noremap = true })
  32. vim.api.nvim_set_keymap('n', '<C-l>', '<C-w>l', { silent = true, noremap = true })
  33. -- What you get in abundance when using Nvim
  34. vim.keymap.set('n', '<leader>f', ':Sex', { silent = true, noremap = true })
  35. -- Sets CTRL+Backspace to delete previous word
  36. -- C-H is what the terminal sends when Ctrl+Backspace is pressed
  37. vim.api.nvim_set_keymap('i', '<C-H>', '<C-W>', { noremap = true })
  38. -- Lazygit
  39. vim.api.nvim_set_keymap('n', '<leader>gg', ':LazyGit<CR>', { noremap = true, silent = true })
  40. -- Doc comment generation
  41. vim.api.nvim_set_keymap('n', '<Leader>cd', ":lua require('neogen').generate()<CR>", { noremap = true, silent = true })
  42. -- When lyf give you lemons
  43. vim.keymap.set('n', '<leader>fml', '<cmd>CellularAutomaton make_it_rain<CR>')
  44. -- [[ Setting options ]]
  45. -- See `:help vim.o`
  46. -- NOTE: You can change these options as you wish!
  47. -- Set highlight on search
  48. vim.o.hlsearch = false
  49. -- Make line numbers default
  50. vim.wo.number = true
  51. -- Enable mouse mode
  52. vim.o.mouse = 'a'
  53. -- Sync clipboard between OS and Neovim.
  54. -- Remove this option if you want your OS clipboard to remain independent.
  55. -- See `:help 'clipboard'`
  56. vim.o.clipboard = 'unnamedplus'
  57. -- Enable break indent
  58. vim.o.breakindent = true
  59. -- Save undo history
  60. vim.o.undofile = true
  61. -- Case-insensitive searching UNLESS \C or capital in search
  62. vim.o.ignorecase = true
  63. vim.o.smartcase = true
  64. -- Keep signcolumn on by default
  65. vim.wo.signcolumn = 'yes'
  66. -- Decrease update time
  67. vim.o.updatetime = 250
  68. vim.o.timeoutlen = 300
  69. -- Set completeopt to have a better completion experience
  70. vim.o.completeopt = 'menuone,noselect'
  71. -- NOTE: You should make sure your terminal supports this
  72. vim.o.termguicolors = true
  73. -- [[ Highlight on yank ]]
  74. -- See `:help vim.highlight.on_yank()`
  75. local highlight_group = vim.api.nvim_create_augroup('YankHighlight', { clear = true })
  76. vim.api.nvim_create_autocmd('TextYankPost', {
  77. callback = function()
  78. vim.highlight.on_yank()
  79. end,
  80. group = highlight_group,
  81. pattern = '*',
  82. })
  83. -- [[ Configure Telescope ]]
  84. -- See `:help telescope` and `:help telescope.setup()`
  85. require('telescope').setup {
  86. defaults = {
  87. mappings = {
  88. i = {
  89. ['<C-u>'] = false,
  90. ['<C-d>'] = false,
  91. },
  92. },
  93. },
  94. pickers = {
  95. find_files = {
  96. find_command = { 'rg', '--files', '--hidden', '-g', '!.git' },
  97. },
  98. },
  99. }
  100. -- Enable telescope fzf native, if installed
  101. pcall(require('telescope').load_extension, 'fzf')
  102. -- Telescope live_grep in git root
  103. -- Function to find the git root directory based on the current buffer's path
  104. local function find_git_root()
  105. -- Use the current buffer's path as the starting point for the git search
  106. local current_file = vim.api.nvim_buf_get_name(0)
  107. local current_dir
  108. local cwd = vim.fn.getcwd()
  109. -- If the buffer is not associated with a file, return nil
  110. if current_file == '' then
  111. current_dir = cwd
  112. else
  113. -- Extract the directory from the current file's path
  114. current_dir = vim.fn.fnamemodify(current_file, ':h')
  115. end
  116. -- Find the Git root directory from the current file's path
  117. local git_root = vim.fn.systemlist('git -C ' .. vim.fn.escape(current_dir, ' ') .. ' rev-parse --show-toplevel')[1]
  118. if vim.v.shell_error ~= 0 then
  119. print 'Not a git repository. Searching on current working directory'
  120. return cwd
  121. end
  122. return git_root
  123. end
  124. -- Custom live_grep function to search in git root
  125. local function live_grep_git_root()
  126. local git_root = find_git_root()
  127. if git_root then
  128. require('telescope.builtin').live_grep {
  129. search_dirs = { git_root },
  130. }
  131. end
  132. end
  133. vim.api.nvim_create_user_command('LiveGrepGitRoot', live_grep_git_root, {})
  134. -- See `:help telescope.builtin`
  135. vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
  136. vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
  137. vim.keymap.set('n', '<leader>/', function()
  138. -- You can pass additional configuration to telescope to change theme, layout, etc.
  139. require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
  140. winblend = 10,
  141. previewer = false,
  142. })
  143. end, { desc = '[/] Fuzzily search in current buffer' })
  144. local function telescope_live_grep_open_files()
  145. require('telescope.builtin').live_grep {
  146. grep_open_files = true,
  147. prompt_title = 'Live Grep in Open Files',
  148. }
  149. end
  150. vim.keymap.set('n', '<leader>s/', telescope_live_grep_open_files, { desc = '[S]earch [/] in Open Files' })
  151. vim.keymap.set('n', '<leader>ss', require('telescope.builtin').builtin, { desc = '[S]earch [S]elect Telescope' })
  152. vim.keymap.set('n', '<leader>gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' })
  153. vim.keymap.set('n', '<leader>sf', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
  154. vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
  155. vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
  156. vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
  157. vim.keymap.set('n', '<leader>sG', ':LiveGrepGitRoot<cr>', { desc = '[S]earch by [G]rep on Git Root' })
  158. vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
  159. vim.keymap.set('n', '<leader>sr', require('telescope.builtin').resume, { desc = '[S]earch [R]esume' })
  160. -- [[ Configure Treesitter ]]
  161. -- See `:help nvim-treesitter`
  162. -- Defer Treesitter setup after first render to improve startup time of 'nvim {filename}'
  163. vim.defer_fn(function()
  164. require('nvim-treesitter.configs').setup {
  165. -- Add languages to be installed here that you want installed for treesitter
  166. ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'tsx', 'javascript', 'typescript', 'vimdoc', 'vim', 'bash', 'svelte' },
  167. -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
  168. auto_install = false,
  169. -- Install languages synchronously (only applied to `ensure_installed`)
  170. sync_install = false,
  171. -- List of parsers to ignore installing
  172. ignore_install = {},
  173. -- You can specify additional Treesitter modules here: -- For example: -- playground = {--enable = true,-- },
  174. modules = {},
  175. highlight = { enable = true },
  176. indent = { enable = true },
  177. incremental_selection = {
  178. enable = true,
  179. keymaps = {
  180. init_selection = '<c-space>',
  181. node_incremental = '<c-space>',
  182. scope_incremental = '<c-s>',
  183. node_decremental = '<M-space>',
  184. },
  185. },
  186. textobjects = {
  187. select = {
  188. enable = true,
  189. lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
  190. keymaps = {
  191. -- You can use the capture groups defined in textobjects.scm
  192. ['aa'] = '@parameter.outer',
  193. ['ia'] = '@parameter.inner',
  194. ['af'] = '@function.outer',
  195. ['if'] = '@function.inner',
  196. ['ac'] = '@class.outer',
  197. ['ic'] = '@class.inner',
  198. },
  199. },
  200. move = {
  201. enable = true,
  202. set_jumps = true, -- whether to set jumps in the jumplist
  203. goto_next_start = {
  204. [']m'] = '@function.outer',
  205. [']]'] = '@class.outer',
  206. },
  207. goto_next_end = {
  208. [']M'] = '@function.outer',
  209. [']['] = '@class.outer',
  210. },
  211. goto_previous_start = {
  212. ['[m'] = '@function.outer',
  213. ['[['] = '@class.outer',
  214. },
  215. goto_previous_end = {
  216. ['[M'] = '@function.outer',
  217. ['[]'] = '@class.outer',
  218. },
  219. },
  220. swap = {
  221. enable = true,
  222. swap_next = {
  223. ['<leader>a'] = '@parameter.inner',
  224. },
  225. swap_previous = {
  226. ['<leader>A'] = '@parameter.inner',
  227. },
  228. },
  229. },
  230. }
  231. end, 0)
  232. -- [[ Configure LSP ]]
  233. -- This function gets run when an LSP connects to a particular buffer.
  234. local on_attach = function(_, bufnr)
  235. -- NOTE: Remember that lua is a real programming language, and as such it is possible
  236. -- to define small helper and utility functions so you don't have to repeat yourself
  237. -- many times.
  238. --
  239. -- In this case, we create a function that lets us more easily define mappings specific
  240. -- for LSP related items. It sets the mode, buffer and description for us each time.
  241. local nmap = function(keys, func, desc)
  242. if desc then
  243. desc = 'LSP: ' .. desc
  244. end
  245. vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
  246. end
  247. nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  248. nmap('<leader>ca', function()
  249. vim.lsp.buf.code_action { context = { only = { 'quickfix', 'refactor', 'source' } } }
  250. end, '[C]ode [A]ction')
  251. nmap('gd', require('telescope.builtin').lsp_definitions, '[G]oto [D]efinition')
  252. nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
  253. nmap('gI', require('telescope.builtin').lsp_implementations, '[G]oto [I]mplementation')
  254. nmap('<leader>D', require('telescope.builtin').lsp_type_definitions, 'Type [D]efinition')
  255. nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  256. nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
  257. -- See `:help K` for why this keymap
  258. nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
  259. -- nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
  260. -- Lesser used LSP functionality
  261. nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  262. nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
  263. nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
  264. nmap('<leader>wl', function()
  265. print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  266. end, '[W]orkspace [L]ist Folders')
  267. -- Create a command `:Format` local to the LSP buffer
  268. vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
  269. vim.lsp.buf.format()
  270. end, { desc = 'Format current buffer with LSP' })
  271. end
  272. -- document existing key chains
  273. require('which-key').register {
  274. ['<leader>c'] = { name = '[C]ode', _ = 'which_key_ignore' },
  275. ['<leader>d'] = { name = '[D]ocument', _ = 'which_key_ignore' },
  276. ['<leader>g'] = { name = '[G]it', _ = 'which_key_ignore' },
  277. ['<leader>h'] = { name = 'Git [H]unk', _ = 'which_key_ignore' },
  278. ['<leader>r'] = { name = '[R]ename', _ = 'which_key_ignore' },
  279. ['<leader>s'] = { name = '[S]earch', _ = 'which_key_ignore' },
  280. ['<leader>t'] = { name = '[T]oggle', _ = 'which_key_ignore' },
  281. ['<leader>w'] = { name = '[W]orkspace', _ = 'which_key_ignore' },
  282. }
  283. -- register which-key VISUAL mode
  284. -- required for visual <leader>hs (hunk stage) to work
  285. require('which-key').register({
  286. ['<leader>'] = { name = 'VISUAL <leader>' },
  287. ['<leader>h'] = { 'Git [H]unk' },
  288. }, { mode = 'v' })
  289. -- mason-lspconfig requires that these setup functions are called in this order
  290. -- before setting up the servers.
  291. require('mason').setup()
  292. require('mason-lspconfig').setup()
  293. -- Enable the following language servers
  294. -- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  295. --
  296. -- Add any additional override configuration in the following tables. They will be passed to
  297. -- the `settings` field of the server config. You must look up that documentation yourself.
  298. --
  299. -- If you want to override the default filetypes that your language server will attach to you can
  300. -- define the property 'filetypes' to the map in question.
  301. local servers = {
  302. -- clangd = {},
  303. -- gopls = {},
  304. pyright = {},
  305. rust_analyzer = {
  306. ['rust-analyzer'] = {
  307. checkOnSave = true,
  308. check = {
  309. enable = true,
  310. command = 'clippy',
  311. features = 'all',
  312. },
  313. },
  314. filetypes = { 'rust' },
  315. },
  316. -- tsserver = {},
  317. -- html = { filetypes = { 'html', 'twig', 'hbs'} },
  318. lua_ls = {
  319. Lua = {
  320. workspace = { checkThirdParty = false },
  321. telemetry = { enable = false },
  322. -- NOTE: toggle below to ignore Lua_LS's noisy `missing-fields` warnings
  323. -- diagnostics = { disable = { 'missing-fields' } },
  324. },
  325. },
  326. svelte = {
  327. filetypes = {
  328. 'svelte',
  329. },
  330. },
  331. }
  332. -- Setup neovim lua configuration
  333. require('neodev').setup()
  334. -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
  335. local capabilities = vim.lsp.protocol.make_client_capabilities()
  336. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  337. -- Ensure the servers above are installed
  338. local mason_lspconfig = require 'mason-lspconfig'
  339. mason_lspconfig.setup {
  340. ensure_installed = vim.tbl_keys(servers),
  341. }
  342. mason_lspconfig.setup_handlers {
  343. function(server_name)
  344. require('lspconfig')[server_name].setup {
  345. capabilities = capabilities,
  346. on_attach = on_attach,
  347. settings = servers[server_name],
  348. filetypes = (servers[server_name] or {}).filetypes,
  349. }
  350. end,
  351. }
  352. -- Autoformat on save
  353. -- local auLspFormatting = vim.api.nvim_create_augroup("LspFormatting", {})
  354. -- vim.api.nvim_create_autocmd('BufWritePre', {
  355. -- callback = function()
  356. -- vim.lsp.buf.format()
  357. -- end,
  358. -- group = auLspFormatting,
  359. -- pattern = '*',
  360. -- })
  361. local null_ls = require 'null-ls'
  362. local augroup = vim.api.nvim_create_augroup('LspFormatting', {})
  363. null_ls.setup {
  364. sources = {
  365. null_ls.builtins.formatting.stylua,
  366. null_ls.builtins.formatting.black,
  367. null_ls.builtins.completion.pyright,
  368. -- null_ls.builtins.formatting.rustfmt,
  369. null_ls.builtins.formatting.markdownlint,
  370. null_ls.builtins.formatting.prettier,
  371. null_ls.builtins.diagnostics.eslint,
  372. },
  373. on_attach = function(client, bufnr)
  374. if client.supports_method 'textDocument/formatting' then
  375. vim.api.nvim_clear_autocmds { group = augroup, buffer = bufnr }
  376. vim.api.nvim_create_autocmd('BufWritePre', {
  377. group = augroup,
  378. buffer = bufnr,
  379. callback = function()
  380. vim.lsp.buf.format()
  381. end,
  382. })
  383. end
  384. end,
  385. }