init.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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' },
  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. ['af'] = '@function.outer',
  201. ['if'] = '@function.inner',
  202. ['ac'] = '@class.outer',
  203. ['ic'] = '@class.inner',
  204. },
  205. },
  206. move = {
  207. enable = true,
  208. set_jumps = true, -- whether to set jumps in the jumplist
  209. goto_next_start = {
  210. [']m'] = '@function.outer',
  211. [']]'] = '@class.outer',
  212. },
  213. goto_next_end = {
  214. [']M'] = '@function.outer',
  215. [']['] = '@class.outer',
  216. },
  217. goto_previous_start = {
  218. ['[m'] = '@function.outer',
  219. ['[['] = '@class.outer',
  220. },
  221. goto_previous_end = {
  222. ['[M'] = '@function.outer',
  223. ['[]'] = '@class.outer',
  224. },
  225. },
  226. swap = {
  227. enable = true,
  228. swap_next = {
  229. ['<leader>a'] = '@parameter.inner',
  230. },
  231. swap_previous = {
  232. ['<leader>A'] = '@parameter.inner',
  233. },
  234. },
  235. },
  236. }
  237. -- Diagnostic keymaps
  238. vim.keymap.set('n', '[d', vim.diagnostic.goto_prev)
  239. vim.keymap.set('n', ']d', vim.diagnostic.goto_next)
  240. vim.keymap.set('n', '<leader>e', vim.diagnostic.open_float)
  241. vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist)
  242. -- LSP settings.
  243. -- This function gets run when an LSP connects to a particular buffer.
  244. local on_attach = function(_, bufnr)
  245. -- NOTE: Remember that lua is a real programming language, and as such it is possible
  246. -- to define small helper and utility functions so you don't have to repeat yourself
  247. -- many times.
  248. --
  249. -- In this case, we create a function that lets us more easily define mappings specific
  250. -- for LSP related items. It sets the mode, buffer and description for us each time.
  251. local nmap = function(keys, func, desc)
  252. if desc then
  253. desc = 'LSP: ' .. desc
  254. end
  255. vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
  256. end
  257. nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  258. nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
  259. nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
  260. nmap('gi', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
  261. nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
  262. nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  263. nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
  264. -- See `:help K` for why this keymap
  265. nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
  266. nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
  267. -- Lesser used LSP functionality
  268. nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  269. nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
  270. nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
  271. nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
  272. nmap('<leader>wl', function()
  273. print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  274. end, '[W]orkspace [L]ist Folders')
  275. -- Create a command `:Format` local to the LSP buffer
  276. vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
  277. if vim.lsp.buf.format then
  278. vim.lsp.buf.format()
  279. elseif vim.lsp.buf.formatting then
  280. vim.lsp.buf.formatting()
  281. end
  282. end, { desc = 'Format current buffer with LSP' })
  283. end
  284. -- Setup mason so it can manage external tooling
  285. require('mason').setup()
  286. -- Enable the following language servers
  287. -- Feel free to add/remove any LSPs that you want here. They will automatically be installed
  288. local servers = { 'clangd', 'rust_analyzer', 'pyright', 'tsserver', 'sumneko_lua', 'gopls' }
  289. -- Ensure the servers above are installed
  290. require('mason-lspconfig').setup {
  291. ensure_installed = servers,
  292. }
  293. -- nvim-cmp supports additional completion capabilities
  294. local capabilities = vim.lsp.protocol.make_client_capabilities()
  295. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  296. for _, lsp in ipairs(servers) do
  297. require('lspconfig')[lsp].setup {
  298. on_attach = on_attach,
  299. capabilities = capabilities,
  300. }
  301. end
  302. -- Turn on status information
  303. require('fidget').setup()
  304. -- Example custom configuration for lua
  305. --
  306. -- Make runtime files discoverable to the server
  307. local runtime_path = vim.split(package.path, ';')
  308. table.insert(runtime_path, 'lua/?.lua')
  309. table.insert(runtime_path, 'lua/?/init.lua')
  310. require('lspconfig').sumneko_lua.setup {
  311. on_attach = on_attach,
  312. capabilities = capabilities,
  313. settings = {
  314. Lua = {
  315. runtime = {
  316. -- Tell the language server which version of Lua you're using (most likely LuaJIT)
  317. version = 'LuaJIT',
  318. -- Setup your lua path
  319. path = runtime_path,
  320. },
  321. diagnostics = {
  322. globals = { 'vim' },
  323. },
  324. workspace = { library = vim.api.nvim_get_runtime_file('', true) },
  325. -- Do not send telemetry data containing a randomized but unique identifier
  326. telemetry = { enable = false },
  327. },
  328. },
  329. }
  330. -- nvim-cmp setup
  331. local cmp = require 'cmp'
  332. local luasnip = require 'luasnip'
  333. cmp.setup {
  334. snippet = {
  335. expand = function(args)
  336. luasnip.lsp_expand(args.body)
  337. end,
  338. },
  339. mapping = cmp.mapping.preset.insert {
  340. ['<C-d>'] = cmp.mapping.scroll_docs(-4),
  341. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  342. ['<C-Space>'] = cmp.mapping.complete(),
  343. ['<CR>'] = cmp.mapping.confirm {
  344. behavior = cmp.ConfirmBehavior.Replace,
  345. select = true,
  346. },
  347. ['<Tab>'] = cmp.mapping(function(fallback)
  348. if cmp.visible() then
  349. cmp.select_next_item()
  350. elseif luasnip.expand_or_jumpable() then
  351. luasnip.expand_or_jump()
  352. else
  353. fallback()
  354. end
  355. end, { 'i', 's' }),
  356. ['<S-Tab>'] = cmp.mapping(function(fallback)
  357. if cmp.visible() then
  358. cmp.select_prev_item()
  359. elseif luasnip.jumpable(-1) then
  360. luasnip.jump(-1)
  361. else
  362. fallback()
  363. end
  364. end, { 'i', 's' }),
  365. },
  366. sources = {
  367. { name = 'nvim_lsp' },
  368. { name = 'luasnip' },
  369. },
  370. }
  371. -- The line beneath this is called `modeline`. See `:help modeline`
  372. -- vim: ts=2 sts=2 sw=2 et