Skip to content

Commit

Permalink
fix: leaving the window now destroys both border and content win
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrimeagen committed Nov 30, 2023
1 parent 0904ae0 commit 7d5d141
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lua/harpoon/autocmd.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
local augroup = vim.api.nvim_create_augroup
return augroup("Harpoon", {})


18 changes: 11 additions & 7 deletions lua/harpoon/buffer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
local utils = require("harpoon.utils")
local HarpoonGroup = require("harpoon.autocmd")

local M = {}

local HARPOON_MENU = "__harpoon-menu__"
Expand All @@ -18,8 +20,6 @@ end
---strings back into the ui. it feels gross and it puts odd coupling
---@param bufnr number
function M.setup_autocmds_and_keymaps(bufnr)
--[[
-- TODO: Do the highlighting better
local curr_file = vim.api.nvim_buf_get_name(0)
local cmd =
string.format(
Expand All @@ -31,9 +31,7 @@ function M.setup_autocmds_and_keymaps(bufnr)
.. "call matchadd('HarpoonCurrentFile', '\\V'.path.'\\$')",
curr_file:gsub("\\", "\\\\")
)
print(cmd)
vim.cmd(cmd)
--]]

if vim.api.nvim_buf_get_name(bufnr) == "" then
vim.api.nvim_buf_set_name(bufnr, get_harpoon_menu_name())
Expand Down Expand Up @@ -71,6 +69,7 @@ function M.setup_autocmds_and_keymaps(bufnr)
bufnr
)
)

-- TODO: Do we want this? is this a thing?
-- its odd... why save on text change? shouldn't we wait until close / w / esc?
--[[
Expand All @@ -89,9 +88,14 @@ function M.setup_autocmds_and_keymaps(bufnr)
bufnr
)
)
vim.cmd(
"autocmd BufLeave <buffer> ++nested ++once silent lua require('harpoon').ui:toggle_quick_menu()"
)

vim.api.nvim_create_autocmd({ "BufLeave" }, {
group = HarpoonGroup,
pattern = "__harpoon*",
callback = function()
require("harpoon").ui:toggle_quick_menu()
end
})
end

---@param bufnr number
Expand Down
3 changes: 1 addition & 2 deletions lua/harpoon/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local Data = require("harpoon.data")
local Config = require("harpoon.config")
local List = require("harpoon.list")
local Listeners = require("harpoon.listeners")
local HarpoonGroup = require("harpoon.autocmd")

-- setup
-- read from a config file
Expand Down Expand Up @@ -48,8 +49,6 @@ function Harpoon:setup(partial_config)
---TODO: should we go through every seen list and update its config?

if self.hooks_setup == false then
local augroup = vim.api.nvim_create_augroup
local HarpoonGroup = augroup("Harpoon", {})

vim.api.nvim_create_autocmd({ "BufLeave", "VimLeavePre" }, {
group = HarpoonGroup,
Expand Down
19 changes: 19 additions & 0 deletions lua/harpoon/test/ui_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,23 @@ describe("harpoon", function()
eq(created_files, list:display())

end)

it("using :q to leave harpoon should quit everything", function()
harpoon.ui:toggle_quick_menu(harpoon:list())

local bufnr = harpoon.ui.bufnr
local win_id = harpoon.ui.win_id

eq(vim.api.nvim_buf_is_valid(bufnr), true)
eq(vim.api.nvim_win_is_valid(win_id), true)
eq(vim.api.nvim_get_current_buf(), bufnr)

vim.cmd [[ q! ]] -- TODO: I shouldn't need q! here

eq(vim.api.nvim_buf_is_valid(bufnr), false)
eq(vim.api.nvim_win_is_valid(win_id), false)
eq(harpoon.ui.bufnr, nil)
eq(harpoon.ui.win_id, nil)
end)

end)
10 changes: 9 additions & 1 deletion lua/harpoon/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local DEFAULT_WINDOW_WIDTH = 69 -- nice

---@class HarpoonUI
---@field win_id number
---@field border_win_id number
---@field bufnr number
---@field settings HarpoonSettings
---@field active_list HarpoonList
Expand All @@ -16,6 +17,7 @@ HarpoonUI.__index = HarpoonUI
function HarpoonUI:new(settings)
return setmetatable({
win_id = nil,
border_win_id = nil,
bufnr = nil,
active_list = nil,
settings = settings,
Expand All @@ -37,8 +39,13 @@ function HarpoonUI:close_menu()
vim.api.nvim_win_close(self.win_id, true)
end

if self.border_win_id ~= nil and vim.api.nvim_win_is_valid(self.border_win_id) then
vim.api.nvim_win_close(self.border_win_id, true)
end

self.active_list = nil
self.win_id = nil
self.border_win_id = nil
self.bufnr = nil

self.closing = false
Expand Down Expand Up @@ -73,7 +80,8 @@ function HarpoonUI:_create_window()
Buffer.setup_autocmds_and_keymaps(bufnr)

self.win_id = win_id
vim.api.nvim_win_set_option(self.win_id, "number", true)
self.border_win_id = popup_info.border.win_id
vim.api.nvim_win_set_option(win_id, "number", true)
vim.api.nvim_win_set_option(win_id, "winhl", "Normal:HarpoonBorder")

return win_id, bufnr
Expand Down

0 comments on commit 7d5d141

Please sign in to comment.