init.lua 16 KB

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