init.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. --[[
  2. =====================================================================
  3. ==================== READ THIS BEFORE CONTINUING ====================
  4. =====================================================================
  5. Kickstart.nvim is *not* a distribution.
  6. Kickstart.nvim is a template for your own configuration.
  7. The goal is that you can read every line of code, top-to-bottom, understand
  8. what your configuration is doing, and modify it to suit your needs.
  9. Once you've done that, you should start exploring, configuring and tinkering to
  10. explore Neovim!
  11. If you don't know anything about Lua, I recommend taking some time to read through
  12. a guide. One possible example:
  13. - https://learnxinyminutes.com/docs/lua/
  14. And then you can explore or search through `:help lua-guide`
  15. Kickstart Guide:
  16. I have left several `:help X` comments throughout the init.lua
  17. You should run that command and read that help section for more information.
  18. In addition, I have some `NOTE:` items throughout the file.
  19. These are for you, the reader to help understand what is happening. Feel free to delete
  20. them once you know what you're doing, but they should serve as a guide for when you
  21. are first encountering a few different constructs in your nvim config.
  22. I hope you enjoy your Neovim journey,
  23. - TJ
  24. P.S. You can delete this when you're done too. It's your config now :)
  25. --]]
  26. -- Set <space> as the leader key
  27. -- See `:help mapleader`
  28. -- NOTE: Must happen before plugins are required (otherwise wrong leader will be used)
  29. vim.g.mapleader = ' '
  30. vim.g.maplocalleader = ' '
  31. -- Install package manager
  32. -- https://github.com/folke/lazy.nvim
  33. -- `:help lazy.nvim.txt` for more info
  34. local lazypath = vim.fn.stdpath 'data' .. '/lazy/lazy.nvim'
  35. if not vim.loop.fs_stat(lazypath) then
  36. vim.fn.system {
  37. 'git',
  38. 'clone',
  39. '--filter=blob:none',
  40. 'https://github.com/folke/lazy.nvim.git',
  41. '--branch=stable', -- latest stable release
  42. lazypath,
  43. }
  44. end
  45. vim.opt.rtp:prepend(lazypath)
  46. -- NOTE: Here is where you install your plugins.
  47. -- You can configure plugins using the `config` key.
  48. --
  49. -- You can also configure plugins after the setup call,
  50. -- as they will be available in your neovim runtime.
  51. require('lazy').setup({
  52. -- NOTE: First, some plugins that don't require any configuration
  53. -- Git related plugins
  54. 'tpope/vim-fugitive',
  55. 'tpope/vim-rhubarb',
  56. -- Detect tabstop and shiftwidth automatically
  57. 'tpope/vim-sleuth',
  58. -- NOTE: This is where your plugins related to LSP can be installed.
  59. -- The configuration is done below. Search for lspconfig to find it below.
  60. {
  61. -- LSP Configuration & Plugins
  62. 'neovim/nvim-lspconfig',
  63. dependencies = {
  64. -- Automatically install LSPs to stdpath for neovim
  65. { 'williamboman/mason.nvim', config = true },
  66. 'williamboman/mason-lspconfig.nvim',
  67. -- Useful status updates for LSP
  68. -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
  69. { 'j-hui/fidget.nvim', opts = {} },
  70. -- Additional lua configuration, makes nvim stuff amazing!
  71. 'folke/neodev.nvim',
  72. },
  73. },
  74. {
  75. -- Autocompletion
  76. 'hrsh7th/nvim-cmp',
  77. dependencies = {
  78. -- Snippet Engine & its associated nvim-cmp source
  79. 'L3MON4D3/LuaSnip',
  80. 'saadparwaiz1/cmp_luasnip',
  81. -- Adds LSP completion capabilities
  82. 'hrsh7th/cmp-nvim-lsp',
  83. -- Adds a number of user-friendly snippets
  84. 'rafamadriz/friendly-snippets',
  85. },
  86. },
  87. -- Useful plugin to show you pending keybinds.
  88. { 'folke/which-key.nvim', opts = {} },
  89. {
  90. -- Adds git releated signs to the gutter, as well as utilities for managing changes
  91. 'lewis6991/gitsigns.nvim',
  92. opts = {
  93. -- See `:help gitsigns.txt`
  94. signs = {
  95. add = { text = '+' },
  96. change = { text = '~' },
  97. delete = { text = '_' },
  98. topdelete = { text = '‾' },
  99. changedelete = { text = '~' },
  100. },
  101. on_attach = function(bufnr)
  102. vim.keymap.set('n', '[c', require('gitsigns').prev_hunk, { buffer = bufnr, desc = 'Go to Previous Hunk' })
  103. vim.keymap.set('n', ']c', require('gitsigns').next_hunk, { buffer = bufnr, desc = 'Go to Next Hunk' })
  104. vim.keymap.set('n', '<leader>ph', require('gitsigns').preview_hunk, { buffer = bufnr, desc = '[P]review [H]unk' })
  105. end,
  106. },
  107. },
  108. -- Theme related configs go here
  109. require 'kickstart.plugins.theme',
  110. -- Status line related configs go here
  111. require 'kickstart.plugins.statusline',
  112. {
  113. -- Add indentation guides even on blank lines
  114. 'lukas-reineke/indent-blankline.nvim',
  115. -- Enable `lukas-reineke/indent-blankline.nvim`
  116. -- See `:help indent_blankline.txt`
  117. opts = {
  118. char = '┊',
  119. show_trailing_blankline_indent = false,
  120. },
  121. },
  122. -- "gc" to comment visual regions/lines
  123. { 'numToStr/Comment.nvim', opts = {} },
  124. -- Fuzzy Finder (files, lsp, etc)
  125. { 'nvim-telescope/telescope.nvim', branch = '0.1.x', dependencies = { 'nvim-lua/plenary.nvim' } },
  126. -- Fuzzy Finder Algorithm which requires local dependencies to be built.
  127. -- Only load if `make` is available. Make sure you have the system
  128. -- requirements installed.
  129. {
  130. 'nvim-telescope/telescope-fzf-native.nvim',
  131. -- NOTE: If you are having trouble with this installation,
  132. -- refer to the README for telescope-fzf-native for more instructions.
  133. build = 'make',
  134. cond = function()
  135. return vim.fn.executable 'make' == 1
  136. end,
  137. },
  138. {
  139. -- Highlight, edit, and navigate code
  140. 'nvim-treesitter/nvim-treesitter',
  141. dependencies = {
  142. 'nvim-treesitter/nvim-treesitter-textobjects',
  143. },
  144. build = ':TSUpdate',
  145. },
  146. -- NOTE: Next Step on Your Neovim Journey: Add/Configure additional "plugins" for kickstart
  147. -- These are some example plugins that I've included in the kickstart repository.
  148. -- Uncomment any of the lines below to enable them.
  149. -- require 'kickstart.plugins.autoformat',
  150. -- require 'kickstart.plugins.debug',
  151. -- NOTE: The import below automatically adds your own plugins, configuration, etc from `lua/custom/plugins/*.lua`
  152. -- You can use this folder to prevent any conflicts with this init.lua if you're interested in keeping
  153. -- up-to-date with whatever is in the kickstart repo.
  154. --
  155. -- For additional information see: https://github.com/folke/lazy.nvim#-structuring-your-plugins
  156. { import = 'custom.plugins' },
  157. }, {})
  158. -- [[ Setting options ]]
  159. -- See `:help vim.o`
  160. -- NOTE: You can change these options as you wish!
  161. -- Set highlight on search
  162. vim.o.hlsearch = false
  163. -- Make line numbers default
  164. vim.wo.number = true
  165. -- Enable mouse mode
  166. vim.o.mouse = 'a'
  167. -- Sync clipboard between OS and Neovim.
  168. -- Remove this option if you want your OS clipboard to remain independent.
  169. -- See `:help 'clipboard'`
  170. vim.o.clipboard = 'unnamedplus'
  171. -- Enable break indent
  172. vim.o.breakindent = true
  173. -- Save undo history
  174. vim.o.undofile = true
  175. -- Case insensitive searching UNLESS /C or capital in search
  176. vim.o.ignorecase = true
  177. vim.o.smartcase = true
  178. -- Keep signcolumn on by default
  179. vim.wo.signcolumn = 'yes'
  180. -- Decrease update time
  181. vim.o.updatetime = 250
  182. vim.o.timeout = true
  183. vim.o.timeoutlen = 300
  184. -- Set completeopt to have a better completion experience
  185. vim.o.completeopt = 'menuone,noselect'
  186. -- NOTE: You should make sure your terminal supports this
  187. vim.o.termguicolors = true
  188. -- [[ Basic Keymaps ]]
  189. -- Keymaps for better default experience
  190. -- See `:help vim.keymap.set()`
  191. vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })
  192. -- Remap for dealing with word wrap
  193. vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
  194. vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
  195. -- [[ Highlight on yank ]]
  196. -- See `:help vim.highlight.on_yank()`
  197. local highlight_group = vim.api.nvim_create_augroup('YankHighlight', { clear = true })
  198. vim.api.nvim_create_autocmd('TextYankPost', {
  199. callback = function()
  200. vim.highlight.on_yank()
  201. end,
  202. group = highlight_group,
  203. pattern = '*',
  204. })
  205. -- [[ Configure Telescope ]]
  206. -- See `:help telescope` and `:help telescope.setup()`
  207. require('telescope').setup {
  208. defaults = {
  209. mappings = {
  210. i = {
  211. ['<C-u>'] = false,
  212. ['<C-d>'] = false,
  213. },
  214. },
  215. },
  216. }
  217. -- Enable telescope fzf native, if installed
  218. pcall(require('telescope').load_extension, 'fzf')
  219. -- See `:help telescope.builtin`
  220. vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
  221. vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
  222. vim.keymap.set('n', '<leader>/', function()
  223. -- You can pass additional configuration to telescope to change theme, layout, etc.
  224. require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
  225. winblend = 10,
  226. previewer = false,
  227. })
  228. end, { desc = '[/] Fuzzily search in current buffer' })
  229. vim.keymap.set('n', '<leader>gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' })
  230. vim.keymap.set('n', '<leader>sf', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
  231. vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
  232. vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
  233. vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
  234. vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
  235. -- [[ Configure Treesitter ]]
  236. -- See `:help nvim-treesitter`
  237. require('nvim-treesitter.configs').setup {
  238. -- Add languages to be installed here that you want installed for treesitter
  239. ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'tsx', 'typescript', 'vimdoc', 'vim' },
  240. -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
  241. auto_install = false,
  242. highlight = { enable = true },
  243. indent = { enable = true, disable = { 'python' } },
  244. incremental_selection = {
  245. enable = true,
  246. keymaps = {
  247. init_selection = '<c-space>',
  248. node_incremental = '<c-space>',
  249. scope_incremental = '<c-s>',
  250. node_decremental = '<M-space>',
  251. },
  252. },
  253. textobjects = {
  254. select = {
  255. enable = true,
  256. lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
  257. keymaps = {
  258. -- You can use the capture groups defined in textobjects.scm
  259. ['aa'] = '@parameter.outer',
  260. ['ia'] = '@parameter.inner',
  261. ['af'] = '@function.outer',
  262. ['if'] = '@function.inner',
  263. ['ac'] = '@class.outer',
  264. ['ic'] = '@class.inner',
  265. },
  266. },
  267. move = {
  268. enable = true,
  269. set_jumps = true, -- whether to set jumps in the jumplist
  270. goto_next_start = {
  271. [']m'] = '@function.outer',
  272. [']]'] = '@class.outer',
  273. },
  274. goto_next_end = {
  275. [']M'] = '@function.outer',
  276. [']['] = '@class.outer',
  277. },
  278. goto_previous_start = {
  279. ['[m'] = '@function.outer',
  280. ['[['] = '@class.outer',
  281. },
  282. goto_previous_end = {
  283. ['[M'] = '@function.outer',
  284. ['[]'] = '@class.outer',
  285. },
  286. },
  287. swap = {
  288. enable = true,
  289. swap_next = {
  290. ['<leader>a'] = '@parameter.inner',
  291. },
  292. swap_previous = {
  293. ['<leader>A'] = '@parameter.inner',
  294. },
  295. },
  296. },
  297. }
  298. -- Diagnostic keymaps
  299. vim.keymap.set('n', '[d', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' })
  300. vim.keymap.set('n', ']d', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' })
  301. vim.keymap.set('n', '<leader>e', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' })
  302. vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' })
  303. -- [[ Configure LSP ]]
  304. -- This function gets run when an LSP connects to a particular buffer.
  305. local on_attach = function(_, bufnr)
  306. -- NOTE: Remember that lua is a real programming language, and as such it is possible
  307. -- to define small helper and utility functions so you don't have to repeat yourself
  308. -- many times.
  309. --
  310. -- In this case, we create a function that lets us more easily define mappings specific
  311. -- for LSP related items. It sets the mode, buffer and description for us each time.
  312. local nmap = function(keys, func, desc)
  313. if desc then
  314. desc = 'LSP: ' .. desc
  315. end
  316. vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
  317. end
  318. nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  319. nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
  320. nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
  321. nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
  322. nmap('gI', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
  323. nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
  324. nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  325. nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
  326. -- See `:help K` for why this keymap
  327. nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
  328. nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
  329. -- Lesser used LSP functionality
  330. nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  331. nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
  332. nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
  333. nmap('<leader>wl', function()
  334. print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  335. end, '[W]orkspace [L]ist Folders')
  336. -- Create a command `:Format` local to the LSP buffer
  337. vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
  338. vim.lsp.buf.format()
  339. end, { desc = 'Format current buffer with LSP' })
  340. end
  341. -- Enable the following language servers
  342. -- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  343. --
  344. -- Add any additional override configuration in the following tables. They will be passed to
  345. -- the `settings` field of the server config. You must look up that documentation yourself.
  346. local servers = {
  347. -- clangd = {},
  348. -- gopls = {},
  349. -- pyright = {},
  350. -- rust_analyzer = {},
  351. -- tsserver = {},
  352. lua_ls = {
  353. Lua = {
  354. workspace = { checkThirdParty = false },
  355. telemetry = { enable = false },
  356. },
  357. },
  358. }
  359. -- Setup neovim lua configuration
  360. require('neodev').setup()
  361. -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
  362. local capabilities = vim.lsp.protocol.make_client_capabilities()
  363. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  364. -- Ensure the servers above are installed
  365. local mason_lspconfig = require 'mason-lspconfig'
  366. mason_lspconfig.setup {
  367. ensure_installed = vim.tbl_keys(servers),
  368. }
  369. mason_lspconfig.setup_handlers {
  370. function(server_name)
  371. require('lspconfig')[server_name].setup {
  372. capabilities = capabilities,
  373. on_attach = on_attach,
  374. settings = servers[server_name],
  375. }
  376. end,
  377. }
  378. -- [[ Configure nvim-cmp ]]
  379. -- See `:help cmp`
  380. local cmp = require 'cmp'
  381. local luasnip = require 'luasnip'
  382. require('luasnip.loaders.from_vscode').lazy_load()
  383. luasnip.config.setup {}
  384. cmp.setup {
  385. snippet = {
  386. expand = function(args)
  387. luasnip.lsp_expand(args.body)
  388. end,
  389. },
  390. mapping = cmp.mapping.preset.insert {
  391. ['<C-n>'] = cmp.mapping.select_next_item(),
  392. ['<C-p>'] = cmp.mapping.select_prev_item(),
  393. ['<C-d>'] = cmp.mapping.scroll_docs(-4),
  394. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  395. ['<C-Space>'] = cmp.mapping.complete {},
  396. ['<CR>'] = cmp.mapping.confirm {
  397. behavior = cmp.ConfirmBehavior.Replace,
  398. select = true,
  399. },
  400. ['<Tab>'] = cmp.mapping(function(fallback)
  401. if cmp.visible() then
  402. cmp.select_next_item()
  403. elseif luasnip.expand_or_locally_jumpable() then
  404. luasnip.expand_or_jump()
  405. else
  406. fallback()
  407. end
  408. end, { 'i', 's' }),
  409. ['<S-Tab>'] = cmp.mapping(function(fallback)
  410. if cmp.visible() then
  411. cmp.select_prev_item()
  412. elseif luasnip.locally_jumpable(-1) then
  413. luasnip.jump(-1)
  414. else
  415. fallback()
  416. end
  417. end, { 'i', 's' }),
  418. },
  419. sources = {
  420. { name = 'nvim_lsp' },
  421. { name = 'luasnip' },
  422. },
  423. }
  424. -- The line beneath this is called `modeline`. See `:help modeline`
  425. -- vim: ts=2 sts=2 sw=2 et