init.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. vim.g.mapleader = ' '
  2. vim.g.maplocalleader = ' '
  3. -- package manager
  4. local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
  5. if not vim.loop.fs_stat(lazypath) then
  6. vim.fn.system({
  7. "git",
  8. "clone",
  9. "--filter=blob:none",
  10. "https://github.com/folke/lazy.nvim.git",
  11. "--branch=stable", -- latest stable release
  12. lazypath,
  13. })
  14. end
  15. vim.opt.rtp:prepend(lazypath)
  16. require("lazy").setup({
  17. "nvim-tree/nvim-tree.lua",
  18. "nvim-tree/nvim-web-devicons",
  19. {
  20. "rafamadriz/neon",
  21. neon_style="doom",
  22. config = function ()
  23. vim.cmd.colorscheme("neon")
  24. end,
  25. },
  26. {
  27. "nvim-lualine/lualine.nvim",
  28. opts = {
  29. options = {
  30. theme = "neon",
  31. },
  32. },
  33. },
  34. -- LSP
  35. {
  36. "neovim/nvim-lspconfig",
  37. dependencies = {
  38. {'williamboman/mason.nvim', config = true},
  39. 'williamboman/mason-lspconfig.nvim',
  40. { 'j-hui/fidget.nvim', tag = 'legacy', opts = {} },
  41. 'folke/neodev.nvim',
  42. }
  43. },
  44. -- Autocompletion
  45. {
  46. 'hrsh7th/nvim-cmp',
  47. dependencies = {
  48. -- Snippet Engine & its associated nvim-cmp source
  49. 'L3MON4D3/LuaSnip',
  50. 'saadparwaiz1/cmp_luasnip',
  51. -- Adds LSP completion capabilities
  52. 'hrsh7th/cmp-nvim-lsp',
  53. -- Adds a number of user-friendly snippets
  54. 'rafamadriz/friendly-snippets',
  55. },
  56. },
  57. -- Useful plugin to show you pending keybinds.
  58. { 'folke/which-key.nvim', opts = {} },
  59. -- "gc" to comment visual regions/lines
  60. { 'numToStr/Comment.nvim', opts = {} },
  61. -- Fuzzy Finder (files, lsp, etc)
  62. {
  63. 'nvim-telescope/telescope.nvim',
  64. branch = '0.1.x',
  65. dependencies = {
  66. 'nvim-lua/plenary.nvim',
  67. -- Fuzzy Finder Algorithm which requires local dependencies to be built.
  68. -- Only load if `make` is available. Make sure you have the system
  69. -- requirements installed.
  70. {
  71. 'nvim-telescope/telescope-fzf-native.nvim',
  72. -- NOTE: If you are having trouble with this installation,
  73. -- refer to the README for telescope-fzf-native for more instructions.
  74. build = 'make',
  75. cond = function()
  76. return vim.fn.executable 'make' == 1
  77. end,
  78. },
  79. },
  80. },
  81. -- Highlight, edit, and navigate code
  82. {
  83. 'nvim-treesitter/nvim-treesitter',
  84. dependencies = {
  85. 'nvim-treesitter/nvim-treesitter-textobjects',
  86. },
  87. build = ':TSUpdate',
  88. },
  89. })
  90. vim.g.loaded_netrw = 1
  91. vim.g.loaded_netrwPlugin = 1
  92. vim.o.termguicolors = true
  93. vim.o.clipboard = "unnamedplus"
  94. vim.o.completeopt = "menuone,noselect"
  95. vim.opt.guifont = "Hack Nerd Font Mono:h12"
  96. vim.opt.number = true
  97. vim.opt.mouse = ""
  98. vim.opt.hlsearch = true
  99. vim.opt.tabstop = 2
  100. vim.opt.shiftwidth = 2
  101. require("nvim-tree").setup({
  102. view = {
  103. width = 30,
  104. },
  105. on_attach = function (bufnr)
  106. local api = require "nvim-tree.api"
  107. api.config.mappings.default_on_attach(bufnr)
  108. vim.keymap.set("n", ".", api.tree.change_root_to_node,
  109. {noremap=true, silent=true, buffer=bufnr, nowait=true})
  110. end,
  111. })
  112. -- unmap space
  113. vim.api.nvim_set_keymap('n', '<Space>', '', {noremap=true, silent=true})
  114. vim.api.nvim_set_keymap("n", "<Leader>w", "<C-w>", {noremap=true, silent=true})
  115. vim.api.nvim_set_keymap("n", "<Leader>ft", ":NvimTreeToggle<CR>", {noremap=true, silent=true})
  116. vim.api.nvim_set_keymap("n", "<BS>", ":NvimTreeFocus<CR>", {noremap=true, silent=true})
  117. -- configure telescope
  118. -- See `:help telescope` and `:help telescope.setup()`
  119. require('telescope').setup {
  120. defaults = {
  121. mappings = {
  122. i = {
  123. ['<C-u>'] = false,
  124. ['<C-d>'] = false,
  125. },
  126. },
  127. },
  128. }
  129. -- Enable telescope fzf native, if installed
  130. pcall(require('telescope').load_extension, 'fzf')
  131. -- See `:help telescope.builtin`
  132. vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
  133. vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
  134. vim.keymap.set('n', '<leader>/', function()
  135. -- You can pass additional configuration to telescope to change theme, layout, etc.
  136. require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
  137. winblend = 10,
  138. previewer = false,
  139. })
  140. end, { desc = '[/] Fuzzily search in current buffer' })
  141. -- vim.keymap.set('n', '<leader>gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' })
  142. vim.keymap.set('n', '<leader>ff', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
  143. -- vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
  144. -- vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
  145. -- vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
  146. -- vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
  147. -- Configure Treesitter
  148. -- See `:help nvim-treesitter`
  149. require('nvim-treesitter.configs').setup {
  150. -- Add languages to be installed here that you want installed for treesitter
  151. ensure_installed = { 'c', 'cpp', 'go', 'lua', 'python', 'rust', 'tsx', 'typescript', 'vimdoc', 'vim' },
  152. -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
  153. auto_install = true,
  154. highlight = { enable = true },
  155. indent = { enable = true },
  156. incremental_selection = {
  157. enable = true,
  158. keymaps = {
  159. init_selection = '<c-space>',
  160. node_incremental = '<c-space>',
  161. scope_incremental = '<c-s>',
  162. node_decremental = '<M-space>',
  163. },
  164. },
  165. textobjects = {
  166. select = {
  167. enable = true,
  168. lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
  169. keymaps = {
  170. -- You can use the capture groups defined in textobjects.scm
  171. ['aa'] = '@parameter.outer',
  172. ['ia'] = '@parameter.inner',
  173. ['af'] = '@function.outer',
  174. ['if'] = '@function.inner',
  175. ['ac'] = '@class.outer',
  176. ['ic'] = '@class.inner',
  177. },
  178. },
  179. move = {
  180. enable = true,
  181. set_jumps = true, -- whether to set jumps in the jumplist
  182. goto_next_start = {
  183. [']m'] = '@function.outer',
  184. [']]'] = '@class.outer',
  185. },
  186. goto_next_end = {
  187. [']M'] = '@function.outer',
  188. [']['] = '@class.outer',
  189. },
  190. goto_previous_start = {
  191. ['[m'] = '@function.outer',
  192. ['[['] = '@class.outer',
  193. },
  194. goto_previous_end = {
  195. ['[M'] = '@function.outer',
  196. ['[]'] = '@class.outer',
  197. },
  198. },
  199. swap = {
  200. enable = true,
  201. swap_next = {
  202. ['<leader>a'] = '@parameter.inner',
  203. },
  204. swap_previous = {
  205. ['<leader>A'] = '@parameter.inner',
  206. },
  207. },
  208. },
  209. }
  210. -- Diagnostic keymaps
  211. vim.keymap.set('n', '<Leader>dp', vim.diagnostic.goto_prev, { desc = 'Go to previous diagnostic message' })
  212. vim.keymap.set('n', '<Leader>dn', vim.diagnostic.goto_next, { desc = 'Go to next diagnostic message' })
  213. vim.keymap.set('n', '<leader>de', vim.diagnostic.open_float, { desc = 'Open floating diagnostic message' })
  214. vim.keymap.set('n', '<leader>dl', vim.diagnostic.setloclist, { desc = 'Open diagnostics list' })
  215. -- Configure LSP
  216. -- This function gets run when an LSP connects to a particular buffer.
  217. local on_attach = function(_, bufnr)
  218. -- NOTE: Remember that lua is a real programming language, and as such it is possible
  219. -- to define small helper and utility functions so you don't have to repeat yourself
  220. -- many times.
  221. --
  222. -- In this case, we create a function that lets us more easily define mappings specific
  223. -- for LSP related items. It sets the mode, buffer and description for us each time.
  224. local nmap = function(keys, func, desc)
  225. if desc then
  226. desc = 'LSP: ' .. desc
  227. end
  228. vim.keymap.set('n', keys, func, { buffer = bufnr, desc = desc })
  229. end
  230. nmap('<leader>rn', vim.lsp.buf.rename, '[R]e[n]ame')
  231. nmap('<leader>ca', vim.lsp.buf.code_action, '[C]ode [A]ction')
  232. nmap('gd', vim.lsp.buf.definition, '[G]oto [D]efinition')
  233. nmap('gr', require('telescope.builtin').lsp_references, '[G]oto [R]eferences')
  234. nmap('gI', vim.lsp.buf.implementation, '[G]oto [I]mplementation')
  235. nmap('<leader>D', vim.lsp.buf.type_definition, 'Type [D]efinition')
  236. nmap('<leader>ds', require('telescope.builtin').lsp_document_symbols, '[D]ocument [S]ymbols')
  237. nmap('<leader>ws', require('telescope.builtin').lsp_dynamic_workspace_symbols, '[W]orkspace [S]ymbols')
  238. -- See `:help K` for why this keymap
  239. nmap('K', vim.lsp.buf.hover, 'Hover Documentation')
  240. nmap('<C-k>', vim.lsp.buf.signature_help, 'Signature Documentation')
  241. -- Lesser used LSP functionality
  242. nmap('gD', vim.lsp.buf.declaration, '[G]oto [D]eclaration')
  243. nmap('<leader>wa', vim.lsp.buf.add_workspace_folder, '[W]orkspace [A]dd Folder')
  244. nmap('<leader>wr', vim.lsp.buf.remove_workspace_folder, '[W]orkspace [R]emove Folder')
  245. nmap('<leader>wl', function()
  246. print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  247. end, '[W]orkspace [L]ist Folders')
  248. -- Create a command `:Format` local to the LSP buffer
  249. vim.api.nvim_buf_create_user_command(bufnr, 'Format', function(_)
  250. vim.lsp.buf.format()
  251. end, { desc = 'Format current buffer with LSP' })
  252. end
  253. -- Enable the following language servers
  254. -- Feel free to add/remove any LSPs that you want here. They will automatically be installed.
  255. --
  256. -- Add any additional override configuration in the following tables. They will be passed to
  257. -- the `settings` field of the server config. You must look up that documentation yourself.
  258. --
  259. -- If you want to override the default filetypes that your language server will attach to you can
  260. -- define the property 'filetypes' to the map in question.
  261. local servers = {
  262. clangd = {},
  263. gopls = {},
  264. pyright = {},
  265. rust_analyzer = {},
  266. -- tsserver = {},
  267. -- html = { filetypes = { 'html', 'twig', 'hbs'} },
  268. lua_ls = {
  269. Lua = {
  270. workspace = { checkThirdParty = false },
  271. telemetry = { enable = false },
  272. },
  273. },
  274. }
  275. -- Setup neovim lua configuration
  276. require('neodev').setup()
  277. -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
  278. local capabilities = vim.lsp.protocol.make_client_capabilities()
  279. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  280. -- Ensure the servers above are installed
  281. local mason_lspconfig = require 'mason-lspconfig'
  282. mason_lspconfig.setup {
  283. ensure_installed = vim.tbl_keys(servers),
  284. }
  285. mason_lspconfig.setup_handlers {
  286. function(server_name)
  287. require('lspconfig')[server_name].setup {
  288. capabilities = capabilities,
  289. on_attach = on_attach,
  290. settings = servers[server_name],
  291. filetypes = (servers[server_name] or {}).filetypes,
  292. }
  293. end
  294. }
  295. -- Configure autocompletion nvim-cmp
  296. -- See `:help cmp`
  297. local cmp = require 'cmp'
  298. local luasnip = require 'luasnip'
  299. require('luasnip.loaders.from_vscode').lazy_load()
  300. luasnip.config.setup {}
  301. cmp.setup {
  302. snippet = {
  303. expand = function(args)
  304. luasnip.lsp_expand(args.body)
  305. end,
  306. },
  307. mapping = cmp.mapping.preset.insert {
  308. ['<C-n>'] = cmp.mapping.select_next_item(),
  309. ['<C-p>'] = cmp.mapping.select_prev_item(),
  310. ['<C-d>'] = cmp.mapping.scroll_docs(-4),
  311. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  312. ['<C-Space>'] = cmp.mapping.complete {},
  313. ['<CR>'] = cmp.mapping.confirm {
  314. behavior = cmp.ConfirmBehavior.Replace,
  315. select = true,
  316. },
  317. ['<Tab>'] = cmp.mapping(function(fallback)
  318. if cmp.visible() then
  319. cmp.select_next_item()
  320. elseif luasnip.expand_or_locally_jumpable() then
  321. luasnip.expand_or_jump()
  322. else
  323. fallback()
  324. end
  325. end, { 'i', 's' }),
  326. ['<S-Tab>'] = cmp.mapping(function(fallback)
  327. if cmp.visible() then
  328. cmp.select_prev_item()
  329. elseif luasnip.locally_jumpable(-1) then
  330. luasnip.jump(-1)
  331. else
  332. fallback()
  333. end
  334. end, { 'i', 's' }),
  335. },
  336. sources = {
  337. { name = 'nvim_lsp' },
  338. { name = 'luasnip' },
  339. },
  340. }