From cdf35d8502f42ebfc079023d47685fa9b1a57bcd Mon Sep 17 00:00:00 2001 From: Jeremie Fraeys Date: Sun, 8 Feb 2026 16:52:49 -0500 Subject: [PATCH] fix: harpoon now works in oil buffers --- lua/custom/plugins/harpoon.lua | 28 ++++++++++++++++++++++++++++ lua/custom/plugins/oil.lua | 27 --------------------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/lua/custom/plugins/harpoon.lua b/lua/custom/plugins/harpoon.lua index d543e10..19351bb 100755 --- a/lua/custom/plugins/harpoon.lua +++ b/lua/custom/plugins/harpoon.lua @@ -73,10 +73,38 @@ return { -- end vim.keymap.set('n', 'a', function() + local harpoon = require('harpoon') + + -- Check if we're in an oil buffer if vim.fn.expand('%:p'):sub(1, 6) == 'oil://' then + local ok_oil, oil = pcall(require, 'oil') + if not ok_oil then + vim.notify('Oil not found', vim.log.levels.WARN) + return + end + + local entry = oil.get_cursor_entry() + if not entry or not entry.name then + vim.notify('No valid entry selected in Oil', vim.log.levels.INFO) + return + end + + local full_path = oil.get_current_dir() .. entry.name + local stat = vim.loop.fs_stat(full_path) + if not stat or stat.type ~= 'file' then + vim.notify('Selected entry is not a file: ' .. full_path, vim.log.levels.INFO) + return + end + + harpoon:list():add({ + value = entry.name, + context = { filename = entry.name, cwd = oil.get_current_dir() or 'global' }, + }) + vim.notify('Added ' .. entry.name .. ' to harpoon', vim.log.levels.INFO) return end + -- Regular file buffer harpoon:list():add() end, { desc = 'Harpoon: Add file to list (if not already present)' }) diff --git a/lua/custom/plugins/oil.lua b/lua/custom/plugins/oil.lua index 4b6057c..cbcea85 100755 --- a/lua/custom/plugins/oil.lua +++ b/lua/custom/plugins/oil.lua @@ -62,32 +62,5 @@ return { pattern = 'background', callback = set_oil_highlights, }) - - -- Add selected file in oil.nvim to Harpoon (only when in oil buffer) - vim.keymap.set('n', 'a', function() - local ok_harpoon, harpoon = pcall(require, 'harpoon') - if not ok_harpoon then - vim.notify('Harpoon not found', vim.log.levels.WARN) - return - end - - local entry = oil.get_cursor_entry() - if not entry or not entry.name then - vim.notify('No valid entry selected in Oil', vim.log.levels.INFO) - return - end - - local full_path = oil.get_current_dir() .. entry.name - local stat = vim.loop.fs_stat(full_path) - if not stat or stat.type ~= 'file' then - vim.notify('Selected entry is not a file: ' .. full_path, vim.log.levels.INFO) - return - end - - harpoon:list():add({ - value = entry.name, - context = { filename = entry.name, cwd = oil.get_current_dir() or 'global' }, - }) - end, { desc = 'Add selected file in oil.nvim to Harpoon' }) end, }