init.lua 16 KB

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