init.lua 12 KB

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