init.lua 13 KB

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