init.lua 15 KB

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