-- [[ Basic Autocommands ]] -- See `:help lua-guide-autocommands` local helper = require 'helper' -- Highlight when yanking (copying) text -- Try it with `yap` in normal mode -- See `:help vim.highlight.on_yank()` vim.api.nvim_create_autocmd('TextYankPost', { desc = 'Highlight when yanking (copying) text', group = vim.api.nvim_create_augroup('kickstart-highlight-yank', { clear = true }), callback = function() vim.highlight.on_yank() end, }) -- Dynamically add Cargo / CMake project/file keymaps to a buffer when that buffer -- is found to be part of a Cargo / CMake project. vim.api.nvim_create_autocmd('BufEnter', { group = vim.api.nvim_create_augroup('gered-bufenter-project-keymaps', { clear = true }), callback = function() local bufnr = vim.api.nvim_get_current_buf() local function map(mode, lhs, rhs, opts) opts = opts or {} opts.buffer = bufnr vim.keymap.set(mode, lhs, rhs, opts) end if helper.in_cargo_project() then map('n', 'ra', 'RustLsp codeAction', { desc = 'Rust: Code [A]ction' }) map('n', 'rr', 'RustLsp! runnables', { desc = 'Rust: [R]un Last Target' }) map('n', 'rR', 'RustLsp runnables', { desc = 'Rust: [R]unnables' }) map('n', 'rd', 'RustLsp! debuggables', { desc = 'Rust: [D]ebug Last Target' }) map('n', 'rD', 'RustLsp debuggables', { desc = 'Rust: [D]ebuggables' }) map('n', 'rt', 'RustLsp testables', { desc = 'Rust: [T]estables' }) map('n', 're', 'RustLsp explainError', { desc = 'Rust: [E]xplain Error' }) map('n', 'ri', 'RustLsp renderDiagnostic', { desc = 'Rust: Show Next D[i]agnostic' }) map('n', 'ro', 'RustLsp openCargo', { desc = 'Rust: [O]pen Cargo.toml' }) map('n', 'rl', 'RustLsp reloadWorkspace', { desc = 'Rust: Re[l]oad Workspace' }) end if helper.in_cmake_project() then map('n', 'cc', 'CMakeClean', { desc = 'CMake: [C]lean' }) map('n', 'cg', 'CMakeGenerate', { desc = 'CMake: [G]enerate' }) map('n', 'cr', 'CMakeRun', { desc = 'CMake: [R]un' }) map('n', 'cd', 'CMakeDebug', { desc = 'CMake: [D]ebug' }) map('n', 'cb', 'CMakeBuild', { desc = 'CMake: [B]uild' }) map('n', 'cB', 'CMakeSelectBuildTarget', { desc = 'CMake: Select [B]uild Target' }) map('n', 'cp', 'CMakeSelectBuildType', { desc = 'CMake: Select Build Ty[p]e' }) map('n', 'cl', 'CMakeSelectLaunchTarget', { desc = 'CMake: Select [L]aunch Target' }) map('n', 'cS', 'CMakeSettings', { desc = 'CMake: Project [S]ettings' }) map('n', 'cs', 'CMakeTargetSettings', { desc = 'CMake: Target [S]ettings' }) end end, }) -- Borrowed from nvim-tree wiki recipe: -- https://github.com/nvim-tree/nvim-tree.lua/wiki/Recipes#make-q-and-bd-work-as-if-tree-was-not-visible -- Make :bd and :q behave as usual when tree is visible vim.api.nvim_create_autocmd({ 'BufEnter', 'QuitPre' }, { nested = false, group = vim.api.nvim_create_augroup('gered-bufenter-quitpre-nvim-tree', { clear = true }), callback = function(e) local tree = require('nvim-tree.api').tree -- Nothing to do if tree is not opened if not tree.is_visible() then return end -- How many focusable windows do we have? (excluding e.g. incline status window) local winCount = 0 for _, winId in ipairs(vim.api.nvim_list_wins()) do if vim.api.nvim_win_get_config(winId).focusable then winCount = winCount + 1 end end -- We want to quit and only one window besides tree is left if e.event == 'QuitPre' and winCount == 2 then vim.api.nvim_cmd({ cmd = 'qall' }, {}) end -- :bd was probably issued an only tree window is left -- Behave as if tree was closed (see `:h :bd`) if e.event == 'BufEnter' and winCount == 1 then -- Required to avoid "Vim:E444: Cannot close last window" vim.defer_fn(function() -- close nvim-tree: will go to the last buffer used before closing tree.toggle { find_file = true, focus = true } -- re-open nivm-tree tree.toggle { find_file = true, focus = false } end, 10) end end, })