init.lua 13 KB

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