init.lua 13 KB

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