feat: holed ui lists even with bonus whitespace

This commit is contained in:
theprimeagen 2024-04-04 09:58:14 -06:00
parent 0d959f34c0
commit 77d52b2a88
4 changed files with 40 additions and 13 deletions

View File

@ -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

View File

@ -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)

View File

@ -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("<CR>")
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())

View File

@ -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)