init.lua 13 KB

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