init.lua 16 KB

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