Skip to content

Commit

Permalink
feat: holed ui lists even with bonus whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
theprimeagen committed Apr 4, 2024
1 parent 0d959f3 commit 77d52b2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
4 changes: 1 addition & 3 deletions lua/harpoon/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lua/harpoon/list.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Logger = require("harpoon.logger")
local utils = require("harpoon.utils")
local Extensions = require("harpoon.extensions")

local function guess_length(arr)
Expand Down Expand Up @@ -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)
Expand Down
29 changes: 29 additions & 0 deletions lua/harpoon/test/ui_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
17 changes: 8 additions & 9 deletions lua/harpoon/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down

0 comments on commit 77d52b2

Please sign in to comment.