Skip to content

Commit 77d52b2

Browse files
author
theprimeagen
committed
feat: holed ui lists even with bonus whitespace
1 parent 0d959f3 commit 77d52b2

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

lua/harpoon/buffer.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ function M.get_contents(bufnr)
8989
local indices = {}
9090

9191
for _, line in pairs(lines) do
92-
if not utils.is_white_space(line) then
93-
table.insert(indices, line)
94-
end
92+
table.insert(indices, line)
9593
end
9694

9795
return indices

lua/harpoon/list.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local Logger = require("harpoon.logger")
2+
local utils = require("harpoon.utils")
23
local Extensions = require("harpoon.extensions")
34

45
local function guess_length(arr)
@@ -252,7 +253,7 @@ function HarpoonList:resolve_displayed(displayed, length)
252253
for i = 1, length do
253254
local v = displayed[i]
254255
local index = index_of(list_displayed, self._length, v)
255-
if v == "" then
256+
if utils.is_white_space(v) then
256257
new_list[i] = nil
257258
elseif index == -1 then
258259
new_list[i] = self.config.create_list_item(self.config, v)

lua/harpoon/test/ui_spec.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,35 @@ describe("harpoon", function()
8181
eq(created_files, list:display())
8282
end)
8383

84+
it("ui with replace_at", function()
85+
local one_f = os.tmpname()
86+
local one = utils.create_file(one_f, { "one", })
87+
local three_f = os.tmpname()
88+
local three = utils.create_file(three_f, { "three", })
89+
local context = { row = 1, col = 0 }
90+
91+
eq(0, harpoon:list():length())
92+
vim.api.nvim_set_current_buf(three)
93+
94+
harpoon:list():replace_at(3)
95+
eq(3, harpoon:list():length())
96+
97+
vim.api.nvim_set_current_buf(one)
98+
harpoon:list():replace_at(1)
99+
eq(3, harpoon:list():length())
100+
101+
harpoon.ui:toggle_quick_menu(harpoon:list())
102+
103+
key("<CR>")
104+
105+
eq(3, harpoon:list():length())
106+
eq({
107+
{ value = one_f, context = context },
108+
nil,
109+
{ value = three_f, context = context },
110+
}, harpoon:list().items)
111+
end)
112+
84113
it("using :q to leave harpoon should quit everything", function()
85114
harpoon.ui:toggle_quick_menu(harpoon:list())
86115

lua/harpoon/ui.lua

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,19 @@ function HarpoonUI:toggle_quick_menu(list, opts)
160160
})
161161
end
162162

163+
function HarpoonUI:_get_processed_ui_contents()
164+
local list = Buffer.get_contents(self.bufnr)
165+
local length = #list
166+
return list, length
167+
end
168+
163169
---@param options? any
164170
function HarpoonUI:select_menu_item(options)
165171
local idx = vim.fn.line(".")
166172

167173
-- must first save any updates potentially made to the list before
168174
-- navigating
169-
local list = Buffer.get_contents(self.bufnr)
170-
local length = #list
175+
local list, length = self:_get_processed_ui_contents()
171176
self.active_list:resolve_displayed(list, length)
172177

173178
Logger:log(
@@ -185,13 +190,7 @@ function HarpoonUI:select_menu_item(options)
185190
end
186191

187192
function HarpoonUI:save()
188-
local list = Buffer.get_contents(self.bufnr)
189-
local length = #list
190-
for i, v in ipairs(list) do
191-
if v == "" then
192-
list[i] = nil
193-
end
194-
end
193+
local list, length = self:_get_processed_ui_contents()
195194

196195
Logger:log("ui#save", list)
197196
self.active_list:resolve_displayed(list, length)

0 commit comments

Comments
 (0)