diff --git a/lua/harpoon2/buffer.lua b/lua/harpoon2/buffer.lua index 4ba6d79..6df7c6a 100644 --- a/lua/harpoon2/buffer.lua +++ b/lua/harpoon2/buffer.lua @@ -42,16 +42,6 @@ function M.setup_autocmds_and_keymaps(bufnr) vim.api.nvim_buf_set_option(bufnr, "buftype", "acwrite") vim.api.nvim_buf_set_option(bufnr, "bufhidden", "delete") - --[[ - vim.api.nvim_buf_set_keymap( - bufnr, - "n", - "z", - "lua print('WTF')", - { silent = true } - ) - --]] - vim.api.nvim_buf_set_keymap( bufnr, "n", diff --git a/lua/harpoon2/data.lua b/lua/harpoon2/data.lua index 7050b89..577b6ff 100644 --- a/lua/harpoon2/data.lua +++ b/lua/harpoon2/data.lua @@ -56,7 +56,8 @@ local function read_data() write_data({}) end - local data = vim.json.decode(path:read()) + local out_data = path:read() + local data = vim.json.decode(out_data) return data end diff --git a/lua/harpoon2/init.lua b/lua/harpoon2/init.lua index 593b8b5..25829d9 100644 --- a/lua/harpoon2/init.lua +++ b/lua/harpoon2/init.lua @@ -52,7 +52,6 @@ function Harpoon:setup(partial_config) group = HarpoonGroup, pattern = '*', callback = function(ev) - --[[ self:_for_each_list(function(list, config) local fn = config[ev.event] @@ -64,7 +63,6 @@ function Harpoon:setup(partial_config) self:sync() end end) - --]] end, }) @@ -143,12 +141,4 @@ function Harpoon:__debug_reset() require("plenary.reload").reload_module("harpoon2") end -local harpoon = Harpoon:new() -HARPOON_DEBUG_VAR = HARPOON_DEBUG_VAR or 0 -if HARPOON_DEBUG_VAR == 0 then - harpoon.ui:toggle_quick_menu(harpoon:list()) - HARPOON_DEBUG_VAR = 1 -end --- leave this undone, i sometimes use this for debugging - -return harpoon +return Harpoon:new() diff --git a/lua/harpoon2/scratch/toggle.lua b/lua/harpoon2/scratch/toggle.lua new file mode 100644 index 0000000..93c25c3 --- /dev/null +++ b/lua/harpoon2/scratch/toggle.lua @@ -0,0 +1,6 @@ + +local harpoon = require("harpoon2") + +harpoon.ui:toggle_quick_menu(harpoon:list()) + + diff --git a/lua/harpoon2/test/harpoon_spec.lua b/lua/harpoon2/test/harpoon_spec.lua index 7d72779..66889d4 100644 --- a/lua/harpoon2/test/harpoon_spec.lua +++ b/lua/harpoon2/test/harpoon_spec.lua @@ -1,29 +1,15 @@ local utils = require("harpoon2.test.utils") -local Data = require("harpoon2.data") local harpoon = require("harpoon2") local eq = assert.are.same +local be = utils.before_each(os.tmpname()) + describe("harpoon", function() - before_each(function() - Data.set_data_path("/tmp/harpoon2.json") - Data.__dangerously_clear_data() - require("plenary.reload").reload_module("harpoon2") - Data = require("harpoon2.data") - Data.set_data_path("/tmp/harpoon2.json") + be() harpoon = require("harpoon2") - utils.clean_files() - - harpoon:setup({ - settings = { - key = function() - return "testies" - end - } - }) - end) it("when we change buffers we update the row and column", function() @@ -53,7 +39,7 @@ describe("harpoon", function() {value = file_name, context = {row = row + 1, col = col}}, } - eq(list.items, expected) + eq(expected, list.items) end) @@ -69,7 +55,8 @@ describe("harpoon", function() "qux" }, row, col) - local list = harpoon:list():append() + local list = harpoon:list() + list:append() harpoon:sync() eq(harpoon:dump(), { diff --git a/lua/harpoon2/test/ui_spec.lua b/lua/harpoon2/test/ui_spec.lua index 5ec055f..3c0cb2d 100644 --- a/lua/harpoon2/test/ui_spec.lua +++ b/lua/harpoon2/test/ui_spec.lua @@ -4,13 +4,21 @@ local eq = assert.are.same describe("harpoon", function() - before_each(utils.before_each) + before_each(utils.before_each(os.tmpname())) it("open the ui without any items in the list", function() local harpoon = require("harpoon2") harpoon.ui:toggle_quick_menu(harpoon:list()) - -- no test, just wanted it to run without error'ing + local bufnr = harpoon.ui.bufnr + local win_id = harpoon.ui.win_id + + harpoon.ui:toggle_quick_menu() + + 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) diff --git a/lua/harpoon2/test/utils.lua b/lua/harpoon2/test/utils.lua index 25e0668..d7cfc16 100644 --- a/lua/harpoon2/test/utils.lua +++ b/lua/harpoon2/test/utils.lua @@ -4,22 +4,27 @@ local M = {} M.created_files = {} -function M.before_each() - Data.set_data_path("/tmp/harpoon2.json") - Data.__dangerously_clear_data() - require("plenary.reload").reload_module("harpoon2") - Data = require("harpoon2.data") - Data.set_data_path("/tmp/harpoon2.json") - local harpoon = require("harpoon2") - M.clean_files() +---@param name string +function M.before_each(name) + return function() + Data.set_data_path(name) + Data.__dangerously_clear_data() - harpoon:setup({ - settings = { - key = function() - return "testies" - end - } - }) + require("plenary.reload").reload_module("harpoon2") + Data = require("harpoon2.data") + Data.set_data_path(name) + local harpoon = require("harpoon2") + + M.clean_files() + + harpoon:setup({ + settings = { + key = function() + return "testies" + end + } + }) + end end function M.clean_files() diff --git a/lua/harpoon2/ui.lua b/lua/harpoon2/ui.lua index 3b525ad..9ea84f9 100644 --- a/lua/harpoon2/ui.lua +++ b/lua/harpoon2/ui.lua @@ -23,18 +23,25 @@ function HarpoonUI:new(settings) end function HarpoonUI:close_menu() - print("CLOSING MENU") - if self.win_id ~= nil and vim.api.nvim_win_is_valid(self.win_id) then - vim.api.nvim_win_close(self.win_id, true) + if self.closing then + return end + self.closing = true + if self.bufnr ~= nil and vim.api.nvim_buf_is_valid(self.bufnr) then vim.api.nvim_buf_delete(self.bufnr, { force = true }) end + if self.win_id ~= nil and vim.api.nvim_win_is_valid(self.win_id) then + vim.api.nvim_win_close(self.win_id, true) + end + self.active_list = nil self.win_id = nil self.bufnr = nil + + self.closing = false end ---@return number,number @@ -51,7 +58,7 @@ function HarpoonUI:_create_window() local height = 8 local borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" } local bufnr = vim.api.nvim_create_buf(false, false) - local win_id, _ = popup.create(bufnr, { + local _, popup_info = popup.create(bufnr, { title = "Harpoon", highlight = "HarpoonWindow", line = math.floor(((vim.o.lines - height) / 2) - 1), @@ -60,6 +67,8 @@ function HarpoonUI:_create_window() minheight = height, borderchars = borderchars, }) + local win_id = popup_info.win_id + Buffer.setup_autocmds_and_keymaps(bufnr) self.win_id = win_id @@ -75,11 +84,10 @@ end local count = 0 ----@param list HarpoonList +---@param list? HarpoonList function HarpoonUI:toggle_quick_menu(list) count = count + 1 - print("toggle?", self.win_id, self.bufnr, count) if list == nil or self.win_id ~= nil then self:close_menu() @@ -88,7 +96,6 @@ function HarpoonUI:toggle_quick_menu(list) local win_id, bufnr = self:_create_window() - print("_create_window_results", win_id, bufnr, count) self.win_id = win_id self.bufnr = bufnr self.active_list = list @@ -98,14 +105,12 @@ function HarpoonUI:toggle_quick_menu(list) end function HarpoonUI:select_menu_item() - error("select_menu_item...?") local idx = vim.fn.line(".") self.active_list:select(idx) self:close_menu() end function HarpoonUI:on_menu_save() - error("saving...?") local list = Buffer.get_contents(self.bufnr) self.active_list:resolve_displayed(list) end