init.lua 9.4 KB

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