init.lua 17 KB

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