init.lua 14 KB

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