init.lua 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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, and understand
  8. what your configuration is doing.
  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. { -- LSP Configuration & Plugins
  61. 'neovim/nvim-lspconfig',
  62. dependencies = {
  63. -- Automatically install LSPs to stdpath for neovim
  64. 'williamboman/mason.nvim',
  65. 'williamboman/mason-lspconfig.nvim',
  66. -- Useful status updates for LSP
  67. -- NOTE: `opts = {}` is the same as calling `require('fidget').setup({})`
  68. { 'j-hui/fidget.nvim', opts = {} },
  69. -- Additional lua configuration, makes nvim stuff amazing!
  70. 'folke/neodev.nvim',
  71. },
  72. },
  73. { -- Autocompletion
  74. 'hrsh7th/nvim-cmp',
  75. dependencies = { 'hrsh7th/cmp-nvim-lsp', 'L3MON4D3/LuaSnip', 'saadparwaiz1/cmp_luasnip' },
  76. },
  77. -- Useful plugin to show you pending keybinds.
  78. { 'folke/which-key.nvim', opts = {} },
  79. { -- Adds git releated signs to the gutter, as well as utilities for managing changes
  80. 'lewis6991/gitsigns.nvim',
  81. opts = {
  82. -- See `:help gitsigns.txt`
  83. signs = {
  84. add = { text = '+' },
  85. change = { text = '~' },
  86. delete = { text = '_' },
  87. topdelete = { text = '‾' },
  88. changedelete = { text = '~' },
  89. },
  90. },
  91. },
  92. { -- Theme inspired by Atom
  93. 'navarasu/onedark.nvim',
  94. priority = 1000,
  95. config = function()
  96. vim.cmd.colorscheme 'onedark'
  97. end,
  98. },
  99. { -- Set lualine as statusline
  100. 'nvim-lualine/lualine.nvim',
  101. -- See `:help lualine.txt`
  102. opts = {
  103. options = {
  104. icons_enabled = false,
  105. theme = 'onedark',
  106. component_separators = '|',
  107. section_separators = '',
  108. },
  109. },
  110. },
  111. { -- Add indentation guides even on blank lines
  112. 'lukas-reineke/indent-blankline.nvim',
  113. -- Enable `lukas-reineke/indent-blankline.nvim`
  114. -- See `:help indent_blankline.txt`
  115. opts = {
  116. char = '┊',
  117. show_trailing_blankline_indent = false,
  118. },
  119. },
  120. -- "gc" to comment visual regions/lines
  121. { 'numToStr/Comment.nvim', opts = {} },
  122. -- Fuzzy Finder (files, lsp, etc)
  123. { 'nvim-telescope/telescope.nvim', version = '*', dependencies = { 'nvim-lua/plenary.nvim' } },
  124. -- Fuzzy Finder Algorithm which requires local dependencies to be built.
  125. -- Only load if `make` is available. Make sure you have the system
  126. -- requirements installed.
  127. {
  128. 'nvim-telescope/telescope-fzf-native.nvim',
  129. -- NOTE: If you are having trouble with this installation,
  130. -- refer to the README for telescope-fzf-native for more instructions.
  131. build = 'make',
  132. cond = function()
  133. return vim.fn.executable 'make' == 1
  134. end,
  135. },
  136. { -- Highlight, edit, and navigate code
  137. 'nvim-treesitter/nvim-treesitter',
  138. dependencies = {
  139. 'nvim-treesitter/nvim-treesitter-textobjects',
  140. },
  141. config = function()
  142. pcall(require('nvim-treesitter.install').update { with_sync = true })
  143. end,
  144. },
  145. -- NOTE: Next Step on Your Neovim Journey: Add/Configure additional "plugins" for kickstart
  146. -- These are some example plugins that I've included in the kickstart repository.
  147. -- Uncomment any of the lines below to enable them.
  148. -- require 'kickstart.plugins.autoformat',
  149. -- require 'kickstart.plugins.debug',
  150. -- NOTE: The import below automatically adds your own plugins, configuration, etc from `lua/custom/plugins/*.lua`
  151. -- You can use this folder to prevent any conflicts with this init.lua if you're interested in keeping
  152. -- up-to-date with whatever is in the kickstart repo.
  153. --
  154. -- For additional information see: https://github.com/folke/lazy.nvim#-structuring-your-plugins
  155. --
  156. -- An additional note is that if you only copied in the `init.lua`, you can just comment this line
  157. -- to get rid of the warning telling you that there are not plugins in `lua/custom/plugins/`.
  158. { import = 'custom.plugins' },
  159. }, {})
  160. -- [[ Setting options ]]
  161. -- See `:help vim.o`
  162. -- Set highlight on search
  163. vim.o.hlsearch = false
  164. -- Make line numbers default
  165. vim.wo.number = true
  166. -- Enable mouse mode
  167. vim.o.mouse = 'a'
  168. -- Enable break indent
  169. vim.o.breakindent = true
  170. -- Save undo history
  171. vim.o.undofile = true
  172. -- Case insensitive searching UNLESS /C or capital in search
  173. vim.o.ignorecase = true
  174. vim.o.smartcase = true
  175. -- Keep signcolumn on by default
  176. vim.wo.signcolumn = 'yes'
  177. -- Decrease update time
  178. vim.o.updatetime = 250
  179. vim.o.timeout = true
  180. vim.o.timeoutlen = 300
  181. -- Set completeopt to have a better completion experience
  182. vim.o.completeopt = 'menuone,noselect'
  183. -- NOTE: You should make sure your terminal supports this
  184. vim.o.termguicolors = true
  185. -- [[ Basic Keymaps ]]
  186. -- Keymaps for better default experience
  187. -- See `:help vim.keymap.set()`
  188. vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })
  189. -- Remap for dealing with word wrap
  190. vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
  191. vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
  192. -- [[ Highlight on yank ]]
  193. -- See `:help vim.highlight.on_yank()`
  194. local highlight_group = vim.api.nvim_create_augroup('YankHighlight', { clear = true })
  195. vim.api.nvim_create_autocmd('TextYankPost', {
  196. callback = function()
  197. vim.highlight.on_yank()
  198. end,
  199. group = highlight_group,
  200. pattern = '*',
  201. })
  202. -- [[ Configure Telescope ]]
  203. -- See `:help telescope` and `:help telescope.setup()`
  204. require('telescope').setup {
  205. defaults = {
  206. mappings = {
  207. i = {
  208. ['<C-u>'] = false,
  209. ['<C-d>'] = false,
  210. },
  211. },
  212. },
  213. }
  214. -- Enable telescope fzf native, if installed
  215. pcall(require('telescope').load_extension, 'fzf')
  216. -- See `:help telescope.builtin`
  217. vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
  218. vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
  219. vim.keymap.set('n', '<leader>/', function()
  220. -- You can pass additional configuration to telescope to change theme, layout, etc.
  221. require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
  222. winblend = 10,
  223. previewer = false,
  224. })
  225. end, { desc = '[/] Fuzzily search in current buffer' })
  226. vim.keymap.set('n', '<leader>sf', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
  227. vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
  228. vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
  229. vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
  230. vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
  231. -- [[ Configure Treesitter ]]
  232. -- See `:help nvim-treesitter`
  233. require('nvim-treesitter.configs').setup {
  234. -- Add languages to be installed here that you want installed for treesitter
  235. ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'tsx', 'typescript', 'help', 'vim' },
  236. -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
  237. auto_install = false,
  238. highlight = { enable = true },
  239. indent = { enable = true, disable = { 'python' } },
  240. incremental_selection = {
  241. enable = true,
  242. keymaps = {
  243. init_selection = '<c-space>',
  244. node_incremental = '<c-space>',
  245. scope_incremental = '<c-s>',
  246. node_decremental = '<M-space>',
  247. },
  248. },
  249. textobjects = {
  250. select = {
  251. enable = true,
  252. lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
  253. keymaps = {
  254. -- You can use the capture groups defined in textobjects.scm
  255. ['aa'] = '@parameter.outer',
  256. ['ia'] = '@parameter.inner',
  257. ['af'] = '@function.outer',
  258. ['if'] = '@function.inner',
  259. ['ac'] = '@class.outer',
  260. ['ic'] = '@class.inner',
  261. },
  262. },
  263. move = {
  264. enable = true,
  265. set_jumps = true, -- whether to set jumps in the jumplist
  266. goto_next_start = {
  267. [']m'] = '@function.outer',
  268. [']]'] = '@class.outer',
  269. },
  270. goto_next_end = {
  271. [']M'] = '@function.outer',
  272. [']['] = '@class.outer',
  273. },
  274. goto_previous_start = {
  275. ['[m'] = '@function.outer',
  276. ['[['] = '@class.outer',
  277. },
  278. goto_previous_end = {
  279. ['[M'] = '@function.outer',
  280. ['[]'] = '@class.outer',
  281. },
  282. },
  283. swap = {
  284. enable = true,
  285. swap_next = {
  286. ['<leader>a'] = '@parameter.inner',
  287. },
  288. swap_previous = {
  289. ['<leader>A'] = '@parameter.inner',
  290. },
  291. },
  292. },
  293. }
  294. -- Diagnostic keymaps
  295. vim.keymap.set('n', '[d', vim.diagnostic.goto_prev)
  296. vim.keymap.set('n', ']d', vim.diagnostic.goto_next)
  297. vim.keymap.set('n', '<leader>e', vim.diagnostic.open_float)
  298. vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist)
  299. -- LSP settings.
  300. -- This function gets run when an LSP connects to a particular buffer.
  301. local on_attach = function(_, bufnr)
  302. -- NOTE: Remember that lua is a real programming language, and as such it is possible
  303. -- to define small helper and utility functions so you don't have to repeat yourself
  304. -- many times.
  305. --
  306. -- In this case, we create a function that lets us more easily define mappings specific
  307. -- for LSP related items. It sets the mode, buffer and description for us each time.
  308. local nmap = function(keys, func, desc)
  309. if desc then
  310. desc = 'LSP: ' .. desc
  311. end
  312. vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
  313. end
  314. nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  315. nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
  316. nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
  317. nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
  318. nmap('gI', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
  319. nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
  320. nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  321. nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
  322. -- See `:help K` for why this keymap
  323. nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
  324. nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
  325. -- Lesser used LSP functionality
  326. nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  327. nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
  328. nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
  329. nmap('<leader>wl', function()
  330. print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  331. end, '[W]orkspace [L]ist Folders')
  332. -- Create a command `:Format` local to the LSP buffer
  333. vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
  334. vim.lsp.buf.format()
  335. end, { desc = 'Format current buffer with LSP' })
  336. end
  337. -- Enable the following language servers
  338. -- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  339. --
  340. -- Add any additional override configuration in the following tables. They will be passed to
  341. -- the `settings` field of the server config. You must look up that documentation yourself.
  342. local servers = {
  343. -- clangd = {},
  344. -- gopls = {},
  345. -- pyright = {},
  346. -- rust_analyzer = {},
  347. -- tsserver = {},
  348. lua_ls = {
  349. Lua = {
  350. workspace = { checkThirdParty = false },
  351. telemetry = { enable = false },
  352. },
  353. },
  354. }
  355. -- Setup neovim lua configuration
  356. require('neodev').setup()
  357. -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
  358. local capabilities = vim.lsp.protocol.make_client_capabilities()
  359. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  360. -- Setup mason so it can manage external tooling
  361. require('mason').setup()
  362. -- Ensure the servers above are installed
  363. local mason_lspconfig = require 'mason-lspconfig'
  364. mason_lspconfig.setup {
  365. ensure_installed = vim.tbl_keys(servers),
  366. }
  367. mason_lspconfig.setup_handlers {
  368. function(server_name)
  369. require('lspconfig')[server_name].setup {
  370. capabilities = capabilities,
  371. on_attach = on_attach,
  372. settings = servers[server_name],
  373. }
  374. end,
  375. }
  376. -- nvim-cmp setup
  377. local cmp = require 'cmp'
  378. local luasnip = require 'luasnip'
  379. luasnip.config.setup {}
  380. cmp.setup {
  381. snippet = {
  382. expand = function(args)
  383. luasnip.lsp_expand(args.body)
  384. end,
  385. },
  386. mapping = cmp.mapping.preset.insert {
  387. ['<C-d>'] = cmp.mapping.scroll_docs(-4),
  388. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  389. ['<C-Space>'] = cmp.mapping.complete {},
  390. ['<CR>'] = cmp.mapping.confirm {
  391. behavior = cmp.ConfirmBehavior.Replace,
  392. select = true,
  393. },
  394. ['<Tab>'] = cmp.mapping(function(fallback)
  395. if cmp.visible() then
  396. cmp.select_next_item()
  397. elseif luasnip.expand_or_jumpable() then
  398. luasnip.expand_or_jump()
  399. else
  400. fallback()
  401. end
  402. end, { 'i', 's' }),
  403. ['<S-Tab>'] = cmp.mapping(function(fallback)
  404. if cmp.visible() then
  405. cmp.select_prev_item()
  406. elseif luasnip.jumpable(-1) then
  407. luasnip.jump(-1)
  408. else
  409. fallback()
  410. end
  411. end, { 'i', 's' }),
  412. },
  413. sources = {
  414. { name = 'nvim_lsp' },
  415. { name = 'luasnip' },
  416. },
  417. }
  418. -- The line beneath this is called `modeline`. See `:help modeline`
  419. -- vim: ts=2 sts=2 sw=2 et