From 77d52b2a88d6fadac90673f3a024b35fc3b25bb4 Mon Sep 17 00:00:00 2001 From: theprimeagen Date: Thu, 4 Apr 2024 09:58:14 -0600 Subject: [PATCH] feat: holed ui lists even with bonus whitespace --- lua/harpoon/buffer.lua | 4 +--- lua/harpoon/list.lua | 3 ++- lua/harpoon/test/ui_spec.lua | 29 +++++++++++++++++++++++++++++ lua/harpoon/ui.lua | 17 ++++++++--------- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/lua/harpoon/buffer.lua b/lua/harpoon/buffer.lua index 0a5576e..2afec32 100644 --- a/lua/harpoon/buffer.lua +++ b/lua/harpoon/buffer.lua @@ -89,9 +89,7 @@ function M.get_contents(bufnr) local indices = {} for _, line in pairs(lines) do - if not utils.is_white_space(line) then - table.insert(indices, line) - end + table.insert(indices, line) end return indices diff --git a/lua/harpoon/list.lua b/lua/harpoon/list.lua index 1b2554a..0c4b791 100644 --- a/lua/harpoon/list.lua +++ b/lua/harpoon/list.lua @@ -1,4 +1,5 @@ local Logger = require("harpoon.logger") +local utils = require("harpoon.utils") local Extensions = require("harpoon.extensions") local function guess_length(arr) @@ -252,7 +253,7 @@ function HarpoonList:resolve_displayed(displayed, length) for i = 1, length do local v = displayed[i] local index = index_of(list_displayed, self._length, v) - if v == "" then + if utils.is_white_space(v) then new_list[i] = nil elseif index == -1 then new_list[i] = self.config.create_list_item(self.config, v) diff --git a/lua/harpoon/test/ui_spec.lua b/lua/harpoon/test/ui_spec.lua index c4e7038..02b318d 100644 --- a/lua/harpoon/test/ui_spec.lua +++ b/lua/harpoon/test/ui_spec.lua @@ -81,6 +81,35 @@ describe("harpoon", function() eq(created_files, list:display()) end) + it("ui with replace_at", function() + local one_f = os.tmpname() + local one = utils.create_file(one_f, { "one", }) + local three_f = os.tmpname() + local three = utils.create_file(three_f, { "three", }) + local context = { row = 1, col = 0 } + + eq(0, harpoon:list():length()) + vim.api.nvim_set_current_buf(three) + + harpoon:list():replace_at(3) + eq(3, harpoon:list():length()) + + vim.api.nvim_set_current_buf(one) + harpoon:list():replace_at(1) + eq(3, harpoon:list():length()) + + harpoon.ui:toggle_quick_menu(harpoon:list()) + + key("") + + eq(3, harpoon:list():length()) + eq({ + { value = one_f, context = context }, + nil, + { value = three_f, context = context }, + }, harpoon:list().items) + end) + it("using :q to leave harpoon should quit everything", function() harpoon.ui:toggle_quick_menu(harpoon:list()) diff --git a/lua/harpoon/ui.lua b/lua/harpoon/ui.lua index 492e8ee..e6c568c 100644 --- a/lua/harpoon/ui.lua +++ b/lua/harpoon/ui.lua @@ -160,14 +160,19 @@ function HarpoonUI:toggle_quick_menu(list, opts) }) end +function HarpoonUI:_get_processed_ui_contents() + local list = Buffer.get_contents(self.bufnr) + local length = #list + return list, length +end + ---@param options? any function HarpoonUI:select_menu_item(options) local idx = vim.fn.line(".") -- must first save any updates potentially made to the list before -- navigating - local list = Buffer.get_contents(self.bufnr) - local length = #list + local list, length = self:_get_processed_ui_contents() self.active_list:resolve_displayed(list, length) Logger:log( @@ -185,13 +190,7 @@ function HarpoonUI:select_menu_item(options) end function HarpoonUI:save() - local list = Buffer.get_contents(self.bufnr) - local length = #list - for i, v in ipairs(list) do - if v == "" then - list[i] = nil - end - end + local list, length = self:_get_processed_ui_contents() Logger:log("ui#save", list) self.active_list:resolve_displayed(list, length)