init.lua 13 KB

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