init.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. -- Install packer
  2. local install_path = vim.fn.stdpath 'data' .. '/site/pack/packer/start/packer.nvim'
  3. local is_bootstrap = false
  4. if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
  5. is_bootstrap = true
  6. vim.fn.system { 'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path }
  7. vim.cmd [[packadd packer.nvim]]
  8. end
  9. require('packer').startup(function(use)
  10. -- Package manager
  11. use 'wbthomason/packer.nvim'
  12. use { -- LSP Configuration & Plugins
  13. 'neovim/nvim-lspconfig',
  14. requires = {
  15. -- Automatically install LSPs to stdpath for neovim
  16. 'williamboman/mason.nvim',
  17. 'williamboman/mason-lspconfig.nvim',
  18. -- Useful status updates for LSP
  19. 'j-hui/fidget.nvim',
  20. -- Additional lua configuration, makes nvim stuff amazing
  21. 'folke/neodev.nvim',
  22. },
  23. }
  24. use { -- Autocompletion
  25. 'hrsh7th/nvim-cmp',
  26. requires = { 'hrsh7th/cmp-nvim-lsp', 'L3MON4D3/LuaSnip', 'saadparwaiz1/cmp_luasnip' },
  27. }
  28. use { -- Highlight, edit, and navigate code
  29. 'nvim-treesitter/nvim-treesitter',
  30. run = function()
  31. pcall(require('nvim-treesitter.install').update { with_sync = true })
  32. end,
  33. }
  34. use { -- Additional text objects via treesitter
  35. 'nvim-treesitter/nvim-treesitter-textobjects',
  36. after = 'nvim-treesitter',
  37. }
  38. -- Git related plugins
  39. use 'tpope/vim-fugitive'
  40. use 'tpope/vim-rhubarb'
  41. use 'lewis6991/gitsigns.nvim'
  42. use 'navarasu/onedark.nvim' -- Theme inspired by Atom
  43. use 'nvim-lualine/lualine.nvim' -- Fancier statusline
  44. use 'lukas-reineke/indent-blankline.nvim' -- Add indentation guides even on blank lines
  45. use 'numToStr/Comment.nvim' -- "gc" to comment visual regions/lines
  46. use 'tpope/vim-sleuth' -- Detect tabstop and shiftwidth automatically
  47. -- Fuzzy Finder (files, lsp, etc)
  48. use { 'nvim-telescope/telescope.nvim', branch = '0.1.x', requires = { 'nvim-lua/plenary.nvim' } }
  49. -- Fuzzy Finder Algorithm which requires local dependencies to be built. Only load if `make` is available
  50. use { 'nvim-telescope/telescope-fzf-native.nvim', run = 'make', cond = vim.fn.executable 'make' == 1 }
  51. -- Add custom plugins to packer from ~/.config/nvim/lua/custom/plugins.lua
  52. local has_plugins, plugins = pcall(require, 'custom.plugins')
  53. if has_plugins then
  54. plugins(use)
  55. end
  56. if is_bootstrap then
  57. require('packer').sync()
  58. end
  59. end)
  60. -- When we are bootstrapping a configuration, it doesn't
  61. -- make sense to execute the rest of the init.lua.
  62. --
  63. -- You'll need to restart nvim, and then it will work.
  64. if is_bootstrap then
  65. print '=================================='
  66. print ' Plugins are being installed'
  67. print ' Wait until Packer completes,'
  68. print ' then restart nvim'
  69. print '=================================='
  70. return
  71. end
  72. -- Automatically source and re-compile packer whenever you save this init.lua
  73. local packer_group = vim.api.nvim_create_augroup('Packer', { clear = true })
  74. vim.api.nvim_create_autocmd('BufWritePost', {
  75. command = 'source <afile> | silent! LspStop | silent! LspStart | PackerCompile',
  76. group = packer_group,
  77. pattern = vim.fn.expand '$MYVIMRC',
  78. })
  79. -- [[ Setting options ]]
  80. -- See `:help vim.o`
  81. -- Set highlight on search
  82. vim.o.hlsearch = false
  83. -- Make line numbers default
  84. vim.wo.number = true
  85. -- Enable mouse mode
  86. vim.o.mouse = 'a'
  87. -- Enable break indent
  88. vim.o.breakindent = true
  89. -- Save undo history
  90. vim.o.undofile = true
  91. -- Case insensitive searching UNLESS /C or capital in search
  92. vim.o.ignorecase = true
  93. vim.o.smartcase = true
  94. -- Decrease update time
  95. vim.o.updatetime = 250
  96. vim.wo.signcolumn = 'yes'
  97. -- Set colorscheme
  98. vim.o.termguicolors = true
  99. vim.cmd [[colorscheme onedark]]
  100. -- Set completeopt to have a better completion experience
  101. vim.o.completeopt = 'menuone,noselect'
  102. -- [[ Basic Keymaps ]]
  103. -- Set <space> as the leader key
  104. -- See `:help mapleader`
  105. -- NOTE: Must happen before plugins are required (otherwise wrong leader will be used)
  106. vim.g.mapleader = ' '
  107. vim.g.maplocalleader = ' '
  108. -- Keymaps for better default experience
  109. -- See `:help vim.keymap.set()`
  110. vim.keymap.set({ 'n', 'v' }, '<Space>', '<Nop>', { silent = true })
  111. -- Remap for dealing with word wrap
  112. vim.keymap.set('n', 'k', "v:count == 0 ? 'gk' : 'k'", { expr = true, silent = true })
  113. vim.keymap.set('n', 'j', "v:count == 0 ? 'gj' : 'j'", { expr = true, silent = true })
  114. -- [[ Highlight on yank ]]
  115. -- See `:help vim.highlight.on_yank()`
  116. local highlight_group = vim.api.nvim_create_augroup('YankHighlight', { clear = true })
  117. vim.api.nvim_create_autocmd('TextYankPost', {
  118. callback = function()
  119. vim.highlight.on_yank()
  120. end,
  121. group = highlight_group,
  122. pattern = '*',
  123. })
  124. -- Set lualine as statusline
  125. -- See `:help lualine.txt`
  126. require('lualine').setup {
  127. options = {
  128. icons_enabled = false,
  129. theme = 'onedark',
  130. component_separators = '|',
  131. section_separators = '',
  132. },
  133. }
  134. -- Enable Comment.nvim
  135. require('Comment').setup()
  136. -- Enable `lukas-reineke/indent-blankline.nvim`
  137. -- See `:help indent_blankline.txt`
  138. require('indent_blankline').setup {
  139. char = '┊',
  140. show_trailing_blankline_indent = false,
  141. }
  142. -- Gitsigns
  143. -- See `:help gitsigns.txt`
  144. require('gitsigns').setup {
  145. signs = {
  146. add = { text = '+' },
  147. change = { text = '~' },
  148. delete = { text = '_' },
  149. topdelete = { text = '‾' },
  150. changedelete = { text = '~' },
  151. },
  152. }
  153. -- [[ Configure Telescope ]]
  154. -- See `:help telescope` and `:help telescope.setup()`
  155. require('telescope').setup {
  156. defaults = {
  157. mappings = {
  158. i = {
  159. ['<C-u>'] = false,
  160. ['<C-d>'] = false,
  161. },
  162. },
  163. },
  164. }
  165. -- Enable telescope fzf native, if installed
  166. pcall(require('telescope').load_extension, 'fzf')
  167. -- See `:help telescope.builtin`
  168. vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
  169. vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
  170. vim.keymap.set('n', '<leader>/', function()
  171. -- You can pass additional configuration to telescope to change theme, layout, etc.
  172. require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
  173. winblend = 10,
  174. previewer = false,
  175. })
  176. end, { desc = '[/] Fuzzily search in current buffer]' })
  177. vim.keymap.set('n', '<leader>sf', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
  178. vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
  179. vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
  180. vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
  181. vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
  182. -- [[ Configure Treesitter ]]
  183. -- See `:help nvim-treesitter`
  184. require('nvim-treesitter.configs').setup {
  185. -- Add languages to be installed here that you want installed for treesitter
  186. ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'typescript', 'help' },
  187. highlight = { enable = true },
  188. indent = { enable = true, disable = { 'python' } },
  189. incremental_selection = {
  190. enable = true,
  191. keymaps = {
  192. init_selection = '<c-space>',
  193. node_incremental = '<c-space>',
  194. scope_incremental = '<c-s>',
  195. node_decremental = '<c-backspace>',
  196. },
  197. },
  198. textobjects = {
  199. select = {
  200. enable = true,
  201. lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
  202. keymaps = {
  203. -- You can use the capture groups defined in textobjects.scm
  204. ['aa'] = '@parameter.outer',
  205. ['ia'] = '@parameter.inner',
  206. ['af'] = '@function.outer',
  207. ['if'] = '@function.inner',
  208. ['ac'] = '@class.outer',
  209. ['ic'] = '@class.inner',
  210. },
  211. },
  212. move = {
  213. enable = true,
  214. set_jumps = true, -- whether to set jumps in the jumplist
  215. goto_next_start = {
  216. [']m'] = '@function.outer',
  217. [']]'] = '@class.outer',
  218. },
  219. goto_next_end = {
  220. [']M'] = '@function.outer',
  221. [']['] = '@class.outer',
  222. },
  223. goto_previous_start = {
  224. ['[m'] = '@function.outer',
  225. ['[['] = '@class.outer',
  226. },
  227. goto_previous_end = {
  228. ['[M'] = '@function.outer',
  229. ['[]'] = '@class.outer',
  230. },
  231. },
  232. swap = {
  233. enable = true,
  234. swap_next = {
  235. ['<leader>a'] = '@parameter.inner',
  236. },
  237. swap_previous = {
  238. ['<leader>A'] = '@parameter.inner',
  239. },
  240. },
  241. },
  242. }
  243. -- Diagnostic keymaps
  244. vim.keymap.set('n', '[d', vim.diagnostic.goto_prev)
  245. vim.keymap.set('n', ']d', vim.diagnostic.goto_next)
  246. vim.keymap.set('n', '<leader>e', vim.diagnostic.open_float)
  247. vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist)
  248. -- LSP settings.
  249. -- This function gets run when an LSP connects to a particular buffer.
  250. local on_attach = function(_, bufnr)
  251. -- NOTE: Remember that lua is a real programming language, and as such it is possible
  252. -- to define small helper and utility functions so you don't have to repeat yourself
  253. -- many times.
  254. --
  255. -- In this case, we create a function that lets us more easily define mappings specific
  256. -- for LSP related items. It sets the mode, buffer and description for us each time.
  257. local nmap = function(keys, func, desc)
  258. if desc then
  259. desc = 'LSP: ' .. desc
  260. end
  261. vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
  262. end
  263. nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  264. nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
  265. nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
  266. nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
  267. nmap('gI', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
  268. nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
  269. nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  270. nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
  271. -- See `:help K` for why this keymap
  272. nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
  273. nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
  274. -- Lesser used LSP functionality
  275. nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  276. nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
  277. nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
  278. nmap('<leader>wl', function()
  279. print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  280. end, '[W]orkspace [L]ist Folders')
  281. -- Create a command `:Format` local to the LSP buffer
  282. vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
  283. vim.lsp.buf.format()
  284. end, { desc = 'Format current buffer with LSP' })
  285. end
  286. -- Enable the following language servers
  287. -- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  288. --
  289. -- Add any additional override configuration in the following tables. They will be passed to
  290. -- the `settings` field of the server config. You must look up that documentation yourself.
  291. local servers = {
  292. -- clangd = {},
  293. -- gopls = {},
  294. -- pyright = {},
  295. -- rust_analyzer = {},
  296. -- tsserver = {},
  297. sumneko_lua = {
  298. Lua = {
  299. workspace = { checkThirdParty = false },
  300. telemetry = { enable = false },
  301. },
  302. },
  303. }
  304. -- Setup neovim lua configuration
  305. require('neodev').setup()
  306. --
  307. -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
  308. local capabilities = vim.lsp.protocol.make_client_capabilities()
  309. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  310. -- Setup mason so it can manage external tooling
  311. require('mason').setup()
  312. -- Ensure the servers above are installed
  313. local mason_lspconfig = require 'mason-lspconfig'
  314. mason_lspconfig.setup {
  315. ensure_installed = vim.tbl_keys(servers),
  316. }
  317. mason_lspconfig.setup_handlers {
  318. function(server_name)
  319. require('lspconfig')[server_name].setup {
  320. capabilities = capabilities,
  321. on_attach = on_attach,
  322. settings = servers[server_name],
  323. }
  324. end,
  325. }
  326. -- Turn on lsp status information
  327. require('fidget').setup()
  328. -- nvim-cmp setup
  329. local cmp = require 'cmp'
  330. local luasnip = require 'luasnip'
  331. cmp.setup {
  332. snippet = {
  333. expand = function(args)
  334. luasnip.lsp_expand(args.body)
  335. end,
  336. },
  337. mapping = cmp.mapping.preset.insert {
  338. ['<C-d>'] = cmp.mapping.scroll_docs(-4),
  339. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  340. ['<C-Space>'] = cmp.mapping.complete(),
  341. ['<CR>'] = cmp.mapping.confirm {
  342. behavior = cmp.ConfirmBehavior.Replace,
  343. select = true,
  344. },
  345. ['<Tab>'] = cmp.mapping(function(fallback)
  346. if cmp.visible() then
  347. cmp.select_next_item()
  348. elseif luasnip.expand_or_jumpable() then
  349. luasnip.expand_or_jump()
  350. else
  351. fallback()
  352. end
  353. end, { 'i', 's' }),
  354. ['<S-Tab>'] = cmp.mapping(function(fallback)
  355. if cmp.visible() then
  356. cmp.select_prev_item()
  357. elseif luasnip.jumpable(-1) then
  358. luasnip.jump(-1)
  359. else
  360. fallback()
  361. end
  362. end, { 'i', 's' }),
  363. },
  364. sources = {
  365. { name = 'nvim_lsp' },
  366. { name = 'luasnip' },
  367. },
  368. }
  369. -- The line beneath this is called `modeline`. See `:help modeline`
  370. -- vim: ts=2 sts=2 sw=2 et