init.lua 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. -- disable netrw for nvim-tree plugin
  2. vim.g.loaded_netrw = 1
  3. vim.g.loaded_netrwPlugin = 1
  4. -- package manager
  5. local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
  6. if not (vim.uv or vim.loop).fs_stat(lazypath) then
  7. local lazyrepo = "https://github.com/folke/lazy.nvim.git"
  8. local out = vim.fn.system({
  9. "git",
  10. "clone",
  11. "--filter=blob:none",
  12. "--branch=stable", -- latest stable release
  13. lazyrepo,
  14. lazypath,
  15. })
  16. if vim.v.shell_error ~= 0 then
  17. vim.api.nvim_echo({
  18. { "Failed to clone lazy.nvim:\n", "ErrorMsg" },
  19. { out, "WarningMsg" },
  20. { "\nPress any key to exit..." },
  21. }, true, {})
  22. vim.fn.getchar()
  23. os.exit(1)
  24. end
  25. end
  26. vim.opt.rtp:prepend(lazypath)
  27. -- set space key as a leader key,
  28. -- this has to be done before loading lazy.nvim
  29. vim.g.mapleader = ' '
  30. vim.g.maplocalleader = ' '
  31. require("lazy").setup({
  32. spec = {
  33. "nvim-tree/nvim-tree.lua",
  34. "nvim-tree/nvim-web-devicons",
  35. "nvim-lualine/lualine.nvim",
  36. -- color theme
  37. {
  38. "catppuccin/nvim",
  39. name = "catppuccin",
  40. -- make sure to load this during startup
  41. lazy = false,
  42. -- make sure to load this beofre all other plugins
  43. priority = 1000,
  44. config = function()
  45. vim.cmd.colorscheme("catppuccin-macchiato")
  46. end,
  47. },
  48. -- LSP
  49. {
  50. "mason-org/mason-lspconfig.nvim",
  51. opts = {
  52. ensure_installed = {
  53. "lua_ls",
  54. "rust_analyzer",
  55. "pyright",
  56. "clangd",
  57. },
  58. },
  59. dependencies = {
  60. { "mason-org/mason.nvim", opts = {} },
  61. "neovim/nvim-lspconfig",
  62. }
  63. },
  64. -- configure lua language server for nvim config file
  65. {
  66. "folke/lazydev.nvim",
  67. ft = "lua", -- only load for lua files
  68. opts = {},
  69. },
  70. -- Autocompletion
  71. -- TODO: look into github.com/ms-jpq/coq_nvim
  72. {
  73. 'hrsh7th/nvim-cmp',
  74. dependencies = {
  75. -- Snippet Engine & its associated nvim-cmp source
  76. 'L3MON4D3/LuaSnip',
  77. 'saadparwaiz1/cmp_luasnip',
  78. -- Adds LSP completion capabilities
  79. 'hrsh7th/cmp-nvim-lsp',
  80. -- Adds a number of user-friendly snippets
  81. 'rafamadriz/friendly-snippets',
  82. },
  83. },
  84. -- Useful plugin to show you pending keybinds.
  85. "folke/which-key.nvim",
  86. -- "gc" to comment visual regions/lines
  87. "numToStr/Comment.nvim",
  88. -- Fuzzy Finder (files, lsp, etc)
  89. {
  90. 'nvim-telescope/telescope.nvim',
  91. branch = '0.1.x',
  92. dependencies = {
  93. 'nvim-lua/plenary.nvim',
  94. -- Fuzzy Finder Algorithm which requires local dependencies to be built.
  95. -- Only load if `make` is available. Make sure you have the system
  96. -- requirements installed.
  97. {
  98. 'nvim-telescope/telescope-fzf-native.nvim',
  99. -- NOTE: If you are having trouble with this installation,
  100. -- refer to the README for telescope-fzf-native for more instructions.
  101. build = 'make',
  102. cond = function()
  103. return vim.fn.executable 'make' == 1
  104. end,
  105. },
  106. },
  107. },
  108. -- Highlight, edit, and navigate code
  109. {
  110. 'nvim-treesitter/nvim-treesitter',
  111. dependencies = {
  112. 'nvim-treesitter/nvim-treesitter-textobjects',
  113. },
  114. build = ':TSUpdate',
  115. },
  116. -- Cut text without populating unnamed register
  117. {
  118. "gbprod/cutlass.nvim",
  119. opts = {
  120. cut_key = "<Leader>x",
  121. }
  122. },
  123. },
  124. -- automatically check for plugin updates
  125. checker = { enabled = true },
  126. })
  127. -- copy to system clipboard by default
  128. vim.o.clipboard = "unnamedplus"
  129. vim.o.termguicolors = true
  130. vim.o.completeopt = "menuone,noselect"
  131. vim.opt.guifont = "Hack Nerd Font Mono:h12"
  132. vim.opt.number = true
  133. vim.opt.mouse = ""
  134. vim.opt.hlsearch = true
  135. vim.opt.tabstop = 2
  136. vim.opt.shiftwidth = 2
  137. require("lualine").setup({})
  138. require("nvim-tree").setup({
  139. view = {
  140. width = 30,
  141. },
  142. on_attach = function (bufnr)
  143. local api = require "nvim-tree.api"
  144. api.config.mappings.default_on_attach(bufnr)
  145. vim.keymap.set("n", ".", api.tree.change_root_to_node,
  146. {noremap=true, silent=true, buffer=bufnr, nowait=true})
  147. end,
  148. })
  149. -- unmap space
  150. vim.api.nvim_set_keymap('n', '<Space>', '', {noremap=true, silent=true})
  151. -- window operations with leader key
  152. vim.api.nvim_set_keymap("n", "<Leader>w", "<C-w>", {noremap=true, silent=true})
  153. -- custom nvim tree mappings
  154. vim.api.nvim_set_keymap("n", "<Leader>ft", ":NvimTreeToggle<CR>", {noremap=true, silent=true})
  155. vim.api.nvim_set_keymap("n", "<BS>", ":NvimTreeFocus<CR>", {noremap=true, silent=true})
  156. -- configure telescope
  157. -- See `:help telescope` and `:help telescope.setup()`
  158. require('telescope').setup {
  159. defaults = {
  160. mappings = {
  161. i = {
  162. ['<C-u>'] = false,
  163. ['<C-d>'] = false,
  164. },
  165. },
  166. },
  167. }
  168. -- Enable telescope fzf native, if installed
  169. pcall(require('telescope').load_extension, 'fzf')
  170. -- See `:help telescope.builtin`
  171. vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
  172. vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
  173. vim.keymap.set('n', '<leader>/', function()
  174. -- You can pass additional configuration to telescope to change theme, layout, etc.
  175. require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
  176. winblend = 10,
  177. previewer = false,
  178. })
  179. end, { desc = '[/] Fuzzily search in current buffer' })
  180. -- vim.keymap.set('n', '<leader>gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' })
  181. vim.keymap.set('n', '<leader>ff', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
  182. -- vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
  183. -- vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
  184. -- vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
  185. -- vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
  186. -- Configure Treesitter
  187. -- See `:help nvim-treesitter`
  188. require('nvim-treesitter.configs').setup {
  189. modules = {},
  190. -- Add languages to be installed here that you want installed for treesitter
  191. ensure_installed = {'haskell', 'c', 'cpp', 'go', 'glsl', 'lua', 'python', 'rust', 'tsx', 'typescript', 'vimdoc', 'vim' },
  192. -- Install parsers synchronously (only applied to `ensure_installed`)
  193. sync_install = false,
  194. -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!)
  195. auto_install = true,
  196. -- List of parsers to ignore installing (or "all")
  197. ignore_install = {},
  198. highlight = { enable = true },
  199. indent = { enable = true },
  200. incremental_selection = {
  201. enable = true,
  202. keymaps = {
  203. init_selection = '<c-space>',
  204. node_incremental = '<c-space>',
  205. scope_incremental = '<c-s>',
  206. node_decremental = '<M-space>',
  207. },
  208. },
  209. textobjects = {
  210. select = {
  211. enable = true,
  212. lookahead = true, -- Automatically jump forward to textobj, similar to targets.vim
  213. keymaps = {
  214. -- You can use the capture groups defined in textobjects.scm
  215. ['aa'] = '@parameter.outer',
  216. ['ia'] = '@parameter.inner',
  217. ['af'] = '@function.outer',
  218. ['if'] = '@function.inner',
  219. ['ac'] = '@class.outer',
  220. ['ic'] = '@class.inner',
  221. },
  222. },
  223. move = {
  224. enable = true,
  225. set_jumps = true, -- whether to set jumps in the jumplist
  226. goto_next_start = {
  227. [']m'] = '@function.outer',
  228. [']]'] = '@class.outer',
  229. },
  230. goto_next_end = {
  231. [']M'] = '@function.outer',
  232. [']['] = '@class.outer',
  233. },
  234. goto_previous_start = {
  235. ['[m'] = '@function.outer',
  236. ['[['] = '@class.outer',
  237. },
  238. goto_previous_end = {
  239. ['[M'] = '@function.outer',
  240. ['[]'] = '@class.outer',
  241. },
  242. },
  243. swap = {
  244. enable = true,
  245. swap_next = {
  246. ['<leader>a'] = '@parameter.inner',
  247. },
  248. swap_previous = {
  249. ['<leader>A'] = '@parameter.inner',
  250. },
  251. },
  252. },
  253. }
  254. -- Diagnostic keymaps
  255. vim.keymap.set('n', '<Leader>dp',
  256. function() vim.diagnostic.jump({count=-1, float=true}) end,
  257. { desc = 'Go to previous diagnostic message' })
  258. vim.keymap.set('n', '<Leader>dn',
  259. function() vim.diagnostic.jump({count=1, float=true}) end,
  260. { desc = 'Go to next diagnostic message' })
  261. vim.keymap.set('n', '<Leader>de', vim.diagnostic.open_float,
  262. { desc = 'Open floating diagnostic message' })
  263. vim.keymap.set('n', '<Leader>dl', vim.diagnostic.setloclist,
  264. { desc = 'Open diagnostics list' })
  265. -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
  266. local capabilities = vim.lsp.protocol.make_client_capabilities()
  267. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  268. -- Configure autocompletion nvim-cmp
  269. -- See `:help cmp`
  270. local cmp = require 'cmp'
  271. local luasnip = require 'luasnip'
  272. require('luasnip.loaders.from_vscode').lazy_load()
  273. luasnip.config.setup {}
  274. cmp.setup {
  275. snippet = {
  276. expand = function(args)
  277. luasnip.lsp_expand(args.body)
  278. end,
  279. },
  280. mapping = cmp.mapping.preset.insert {
  281. ['<C-n>'] = cmp.mapping.select_next_item(),
  282. ['<C-p>'] = cmp.mapping.select_prev_item(),
  283. ['<C-d>'] = cmp.mapping.scroll_docs(-4),
  284. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  285. ['<C-Space>'] = cmp.mapping.complete {},
  286. ['<CR>'] = cmp.mapping.confirm {
  287. behavior = cmp.ConfirmBehavior.Replace,
  288. select = true,
  289. },
  290. ['<Tab>'] = cmp.mapping(function(fallback)
  291. if cmp.visible() then
  292. cmp.select_next_item()
  293. elseif luasnip.expand_or_locally_jumpable() then
  294. luasnip.expand_or_jump()
  295. else
  296. fallback()
  297. end
  298. end, { 'i', 's' }),
  299. ['<S-Tab>'] = cmp.mapping(function(fallback)
  300. if cmp.visible() then
  301. cmp.select_prev_item()
  302. elseif luasnip.locally_jumpable(-1) then
  303. luasnip.jump(-1)
  304. else
  305. fallback()
  306. end
  307. end, { 'i', 's' }),
  308. },
  309. sources = {
  310. { name = 'nvim_lsp' },
  311. { name = 'luasnip' },
  312. },
  313. }
  314. -- Do not yank when pasting over selected text
  315. -- "x" - visual mode
  316. vim.keymap.set("x", "p", "P", { silent = true })