init.lua 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. -- color theme
  34. {
  35. "catppuccin/nvim",
  36. name = "catppuccin",
  37. -- make sure to load this during startup
  38. lazy = false,
  39. -- make sure to load this beofre all other plugins
  40. priority = 1000,
  41. config = function()
  42. vim.cmd.colorscheme("catppuccin-macchiato")
  43. end,
  44. },
  45. "nvim-tree/nvim-tree.lua",
  46. "nvim-tree/nvim-web-devicons",
  47. "nvim-lualine/lualine.nvim",
  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. -- venv selector for python
  71. {
  72. "linux-cultist/venv-selector.nvim",
  73. dependencies = {
  74. "neovim/nvim-lspconfig",
  75. {
  76. "nvim-telescope/telescope.nvim",
  77. branch = "0.1.x",
  78. dependencies = { "nvim-lua/plenary.nvim" }
  79. }, -- optional: you can also use fzf-lua, snacks, mini-pick instead.
  80. },
  81. ft = "python", -- Load when opening Python files
  82. keys = {
  83. { ",v", "<cmd>VenvSelect<cr>" }, -- Open picker on keymap
  84. },
  85. opts = { -- this can be an empty lua table - just showing below for clarity.
  86. search = {}, -- if you add your own searches, they go here.
  87. options = {} -- if you add plugin options, they go here.
  88. },
  89. },
  90. -- Autocompletion
  91. -- TODO: look into github.com/ms-jpq/coq_nvim
  92. {
  93. 'hrsh7th/nvim-cmp',
  94. dependencies = {
  95. -- Snippet Engine & its associated nvim-cmp source
  96. 'L3MON4D3/LuaSnip',
  97. 'saadparwaiz1/cmp_luasnip',
  98. -- Adds LSP completion capabilities
  99. 'hrsh7th/cmp-nvim-lsp',
  100. -- Adds a number of user-friendly snippets
  101. 'rafamadriz/friendly-snippets',
  102. },
  103. },
  104. -- Useful plugin to show you pending keybinds.
  105. "folke/which-key.nvim",
  106. -- "gc" to comment visual regions/lines
  107. "numToStr/Comment.nvim",
  108. -- Fuzzy Finder (files, lsp, etc)
  109. {
  110. 'nvim-telescope/telescope.nvim',
  111. branch = '0.1.x',
  112. dependencies = {
  113. 'nvim-lua/plenary.nvim',
  114. -- Fuzzy Finder Algorithm which requires local dependencies to be built.
  115. -- Only load if `make` is available. Make sure you have the system
  116. -- requirements installed.
  117. {
  118. 'nvim-telescope/telescope-fzf-native.nvim',
  119. -- NOTE: If you are having trouble with this installation,
  120. -- refer to the README for telescope-fzf-native for more instructions.
  121. build = 'make',
  122. cond = function()
  123. return vim.fn.executable 'make' == 1
  124. end,
  125. },
  126. },
  127. },
  128. -- Highlight, edit, and navigate code
  129. {
  130. 'nvim-treesitter/nvim-treesitter',
  131. lazy = false,
  132. build = ':TSUpdate',
  133. },
  134. -- Cut text without populating unnamed register
  135. {
  136. "gbprod/cutlass.nvim",
  137. opts = {
  138. cut_key = "<Leader>x",
  139. }
  140. },
  141. },
  142. -- automatically check for plugin updates
  143. checker = { enabled = true },
  144. })
  145. -- copy to system clipboard by default
  146. vim.o.clipboard = "unnamedplus"
  147. vim.o.termguicolors = true
  148. vim.o.completeopt = "menuone,noselect"
  149. vim.opt.guifont = "Hack Nerd Font Mono:h12"
  150. vim.opt.number = true
  151. vim.opt.mouse = ""
  152. vim.opt.hlsearch = true
  153. vim.opt.tabstop = 2
  154. vim.opt.shiftwidth = 2
  155. require("lualine").setup({})
  156. -- Configure Treesitter
  157. require('nvim-treesitter').install {
  158. 'haskell', 'c', 'cpp', 'go', 'glsl', 'lua', 'python', 'rust', 'tsx',
  159. 'typescript', 'javascript', 'vimdoc', 'vim', 'zig'
  160. }
  161. require("nvim-tree").setup({
  162. view = {
  163. width = 30,
  164. },
  165. on_attach = function (bufnr)
  166. local api = require "nvim-tree.api"
  167. --api.config.mappings.default_on_attach()
  168. api.map.on_attach.default(bufnr)
  169. vim.keymap.set("n", ".", api.tree.change_root_to_node,
  170. {noremap=true, silent=true, buffer=bufnr, nowait=true})
  171. end,
  172. })
  173. -- unmap space
  174. vim.api.nvim_set_keymap('n', '<Space>', '', {noremap=true, silent=true})
  175. -- window operations with leader key
  176. vim.api.nvim_set_keymap("n", "<Leader>w", "<C-w>", {noremap=true, silent=true})
  177. -- custom nvim tree mappings
  178. vim.api.nvim_set_keymap("n", "<Leader>ft", ":NvimTreeToggle<CR>", {noremap=true, silent=true})
  179. vim.api.nvim_set_keymap("n", "<BS>", ":NvimTreeFocus<CR>", {noremap=true, silent=true})
  180. -- configure telescope
  181. -- See `:help telescope` and `:help telescope.setup()`
  182. require('telescope').setup {
  183. defaults = {
  184. mappings = {
  185. i = {
  186. ['<C-u>'] = false,
  187. ['<C-d>'] = false,
  188. },
  189. },
  190. },
  191. }
  192. -- Enable telescope fzf native, if installed
  193. pcall(require('telescope').load_extension, 'fzf')
  194. -- See `:help telescope.builtin`
  195. vim.keymap.set('n', '<leader>?', require('telescope.builtin').oldfiles, { desc = '[?] Find recently opened files' })
  196. vim.keymap.set('n', '<leader><space>', require('telescope.builtin').buffers, { desc = '[ ] Find existing buffers' })
  197. vim.keymap.set('n', '<leader>/', function()
  198. -- You can pass additional configuration to telescope to change theme, layout, etc.
  199. require('telescope.builtin').current_buffer_fuzzy_find(require('telescope.themes').get_dropdown {
  200. winblend = 10,
  201. previewer = false,
  202. })
  203. end, { desc = '[/] Fuzzily search in current buffer' })
  204. -- vim.keymap.set('n', '<leader>gf', require('telescope.builtin').git_files, { desc = 'Search [G]it [F]iles' })
  205. vim.keymap.set('n', '<leader>ff', require('telescope.builtin').find_files, { desc = '[S]earch [F]iles' })
  206. -- vim.keymap.set('n', '<leader>sh', require('telescope.builtin').help_tags, { desc = '[S]earch [H]elp' })
  207. -- vim.keymap.set('n', '<leader>sw', require('telescope.builtin').grep_string, { desc = '[S]earch current [W]ord' })
  208. -- vim.keymap.set('n', '<leader>sg', require('telescope.builtin').live_grep, { desc = '[S]earch by [G]rep' })
  209. -- vim.keymap.set('n', '<leader>sd', require('telescope.builtin').diagnostics, { desc = '[S]earch [D]iagnostics' })
  210. -- Diagnostic keymaps
  211. vim.keymap.set('n', '<Leader>dp',
  212. function() vim.diagnostic.jump({count=-1, float=true}) end,
  213. { desc = 'Go to previous diagnostic message' })
  214. vim.keymap.set('n', '<Leader>dn',
  215. function() vim.diagnostic.jump({count=1, float=true}) end,
  216. { desc = 'Go to next diagnostic message' })
  217. vim.keymap.set('n', '<Leader>de', vim.diagnostic.open_float,
  218. { desc = 'Open floating diagnostic message' })
  219. vim.keymap.set('n', '<Leader>dl', vim.diagnostic.setloclist,
  220. { desc = 'Open diagnostics list' })
  221. -- nvim-cmp supports additional completion capabilities, so broadcast that to servers
  222. local capabilities = vim.lsp.protocol.make_client_capabilities()
  223. capabilities = require('cmp_nvim_lsp').default_capabilities(capabilities)
  224. -- Configure autocompletion nvim-cmp
  225. -- See `:help cmp`
  226. local cmp = require 'cmp'
  227. local luasnip = require 'luasnip'
  228. require('luasnip.loaders.from_vscode').lazy_load()
  229. luasnip.config.setup {}
  230. cmp.setup {
  231. snippet = {
  232. expand = function(args)
  233. luasnip.lsp_expand(args.body)
  234. end,
  235. },
  236. mapping = cmp.mapping.preset.insert {
  237. ['<C-n>'] = cmp.mapping.select_next_item(),
  238. ['<C-p>'] = cmp.mapping.select_prev_item(),
  239. ['<C-d>'] = cmp.mapping.scroll_docs(-4),
  240. ['<C-f>'] = cmp.mapping.scroll_docs(4),
  241. ['<C-Space>'] = cmp.mapping.complete {},
  242. ['<CR>'] = cmp.mapping.confirm {
  243. behavior = cmp.ConfirmBehavior.Replace,
  244. select = true,
  245. },
  246. ['<Tab>'] = cmp.mapping(function(fallback)
  247. if cmp.visible() then
  248. cmp.select_next_item()
  249. elseif luasnip.expand_or_locally_jumpable() then
  250. luasnip.expand_or_jump()
  251. else
  252. fallback()
  253. end
  254. end, { 'i', 's' }),
  255. ['<S-Tab>'] = cmp.mapping(function(fallback)
  256. if cmp.visible() then
  257. cmp.select_prev_item()
  258. elseif luasnip.locally_jumpable(-1) then
  259. luasnip.jump(-1)
  260. else
  261. fallback()
  262. end
  263. end, { 'i', 's' }),
  264. },
  265. sources = {
  266. { name = 'nvim_lsp' },
  267. { name = 'luasnip' },
  268. },
  269. }
  270. -- Do not yank when pasting over selected text
  271. -- "x" - visual mode
  272. vim.keymap.set("x", "p", "P", { silent = true })
  273. -- Auto format selected code
  274. vim.keymap.set("v", "<leader>f", function()
  275. local start_pos = vim.api.nvim_buf_get_mark(0, "<")
  276. local end_pos = vim.api.nvim_buf_get_mark(0, ">")
  277. vim.lsp.buf.format({
  278. range = {
  279. ["start"] = { start_pos[1] - 1, start_pos[2] },
  280. ["end"] = { end_pos[1] - 1, end_pos[2] },
  281. },
  282. })
  283. end)