Skip to content

Commit

Permalink
Merge branch 'harpoon2' into fix_marks_index
Browse files Browse the repository at this point in the history
  • Loading branch information
kimabrandt-flx committed Apr 4, 2024
2 parents 0abc1d2 + 4ad05be commit a33cbab
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 31 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ local harpoon = require("harpoon")
harpoon:setup()
-- REQUIRED

vim.keymap.set("n", "<leader>a", function() harpoon:list():append() end)
vim.keymap.set("n", "<leader>a", function() harpoon:list():add() end)
vim.keymap.set("n", "<C-e>", function() harpoon.ui:toggle_quick_menu(harpoon:list()) end)

vim.keymap.set("n", "<C-h>", function() harpoon:list():select(1) end)
Expand All @@ -89,7 +89,7 @@ vim.keymap.set("n", "<C-S-N>", function() harpoon:list():next() end)

### Telescope

In order to use [Telescope](https://github.com/nvim-telescope/telescope.nvim) as a UI,
In order to use [Telescope](https://github.com/nvim-telescope/telescope.nvim) as a UI,
make sure to add `telescope` to your dependencies and paste this following snippet into your configuration.

```lua
Expand Down Expand Up @@ -137,7 +137,7 @@ harpoon:setup({
-- Setting up custom behavior for a list named "cmd"
"cmd" = {

-- When you call list:append() this function is called and the return
-- When you call list:add() this function is called and the return
-- value will be put in the list at the end.
--
-- which means same behavior for prepend except where in the list the
Expand Down Expand Up @@ -204,7 +204,7 @@ There is quite a bit of behavior you can configure via `harpoon:setup()`
* `display`: how to display the list item in the ui menu
* `select`: the action taken when selecting a list item. called from `list:select(idx, options)`
* `equals`: how to compare two list items for equality
* `create_list_item`: called when `list:append()` or `list:prepend()` is called. called with an item, which will be a string, when adding through the ui menu
* `create_list_item`: called when `list:add()` or `list:prepend()` is called. called with an item, which will be a string, when adding through the ui menu
* `BufLeave`: this function is called for every list on BufLeave. if you need custom behavior, this is the place
* `VimLeavePre`: this function is called for every list on VimLeavePre.
* `get_root_dir`: used for creating relative paths. defaults to `vim.loop.cwd()`
Expand Down Expand Up @@ -287,7 +287,7 @@ contribute start with an issue and I am totally willing for PRs, but I will be
very conservative on what I take. I don't want Harpoon _solving_ specific
issues, I want it to create the proper hooks to solve any problem

**Running Tests**
**Running Tests**
To run the tests make sure [plenary](https://github.com/nvim-lua/plenary.nvim) is checked out in the parent directory of *this* repository, then run `make test`.

## ⇁ Social
Expand Down
7 changes: 6 additions & 1 deletion lua/harpoon/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ function M.get_default_config()
---@param list_item_a HarpoonListItem
---@param list_item_b HarpoonListItem
equals = function(list_item_a, list_item_b)
if list_item_a == nil and list_item_b == nil then
return true
elseif list_item_a == nil or list_item_b == nil then
return false
end
return list_item_a.value == list_item_b.value
end,

Expand Down Expand Up @@ -208,7 +213,7 @@ function M.get_default_config()
}
end

---@param partial_config HarpoonPartialConfig
---@param partial_config HarpoonPartialConfig?
---@param latest_config HarpoonConfig?
---@return HarpoonConfig
function M.merge_config(partial_config, latest_config)
Expand Down
1 change: 1 addition & 0 deletions lua/harpoon/extensions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ return {
builtins = Builtins,
extensions = extensions,
event_names = {
REPLACE = "REPLACE",
ADD = "ADD",
SELECT = "SELECT",
REMOVE = "REMOVE",
Expand Down
135 changes: 117 additions & 18 deletions lua/harpoon/list.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,34 @@
local Logger = require("harpoon.logger")
local Extensions = require("harpoon.extensions")

local function guess_length(arr)
local last_known = #arr
for i = 1, 20 do
if arr[i] ~= nil and last_known < i then
last_known = i
end
end

return last_known
end

local function determine_length(arr, previous_length)
local idx = previous_length
for i = previous_length, 1, -1 do
if arr[i] ~= nil then
idx = i
break
end
end
return idx
end

--- @class HarpoonNavOptions
--- @field ui_nav_wrap? boolean

---@param items any[]
---@param element any
---@param config HarpoonPartialConfigItem?
local function index_of(items, element, config)
local equals = config and config.equals
or function(a, b)
Expand All @@ -20,48 +45,104 @@ local function index_of(items, element, config)
return index
end

---@param arr any[]
---@param value any
---@return number
local function prepend_to_array(arr, value)
local idx = 1
local prev = value
while true do
local curr = arr[idx]
arr[idx] = prev
if curr == nil then
break
end
prev = curr
idx = idx + 1
end
return idx
end

--- @class HarpoonItem
--- @field value string
--- @field context any

--- @class HarpoonList
--- @field config HarpoonPartialConfigItem
--- @field name string
--- @field _length number
--- @field _index number
--- @field items HarpoonItem[]
local HarpoonList = {}

HarpoonList.__index = HarpoonList
function HarpoonList:new(config, name, items)
items = items or {}
return setmetatable({
items = items,
config = config,
_length = guess_length(items),
name = name,
_index = 1,
}, self)
end

---@return number
function HarpoonList:length()
return #self.items
return self._length
end

function HarpoonList:clear()
self.items = {}
self._length = 0
end

---@param item? HarpoonListItem
---@return HarpoonList
function HarpoonList:append(item)
print("APPEND IS DEPRICATED -- PLEASE USE `add`")
return self:add(item)
end

---@param idx number
---@param item? HarpoonListItem
function HarpoonList:replace_at(idx, item)
item = item or self.config.create_list_item(self.config)
Extensions.extensions:emit(
Extensions.event_names.REPLACE,
{ list = self, item = item, idx = idx }
)
self.items[idx] = item
if idx > self._length then
self._length = idx
end
end

---@param item? HarpoonListItem
function HarpoonList:add(item)
item = item or self.config.create_list_item(self.config)

local index = index_of(self.items, item, self.config)
Logger:log("HarpoonList:append", { item = item, index = index })
Logger:log("HarpoonList:add", { item = item, index = index })

if index == -1 then
local idx = self._length + 1
for i = 1, self._length + 1 do
if self.items[i] == nil then
idx = i
break
end
end

Extensions.extensions:emit(
Extensions.event_names.ADD,
{ list = self, item = item, idx = #self.items + 1 }
{ list = self, item = item, idx = idx }
)
table.insert(self.items, item)

self.items[idx] = item
if idx > self._length then
self._length = idx
end
end

return self
Expand All @@ -77,7 +158,10 @@ function HarpoonList:prepend(item)
Extensions.event_names.ADD,
{ list = self, item = item, idx = 1 }
)
table.insert(self.items, 1, item)
local stop_idx = prepend_to_array(self.items, item)
if stop_idx > self._length then
self._length = stop_idx
end
end

return self
Expand All @@ -86,22 +170,26 @@ end
---@return HarpoonList
function HarpoonList:remove(item)
item = item or self.config.create_list_item(self.config)
for i, v in ipairs(self.items) do
for i = 1, self._length do
local v = self.items[i]
if self.config.equals(v, item) then
Extensions.extensions:emit(
Extensions.event_names.REMOVE,
{ list = self, item = item, idx = i }
)
Logger:log("HarpoonList:remove", { item = item, index = i })
table.remove(self.items, i)
self.items[i] = nil
if i == self._length then
self._length = determine_length(self.items, self._length)
end
break
end
end
return self
end

---@return HarpoonList
function HarpoonList:removeAt(index)
function HarpoonList:remove_at(index)
if self.items[index] then
Logger:log(
"HarpoonList:removeAt",
Expand All @@ -111,7 +199,10 @@ function HarpoonList:removeAt(index)
Extensions.event_names.REMOVE,
{ list = self, item = self.items[index], idx = index }
)
table.remove(self.items, index)
self.items[index] = nil
if index == self._length then
self._length = determine_length(self.items, self._length)
end
end
return self
end
Expand All @@ -122,7 +213,7 @@ end

function HarpoonList:get_by_display(name)
local displayed = self:display()
local index = index_of(displayed, name)
local index = index_of(displayed, name, self.config)
if index == -1 then
return nil
end
Expand All @@ -131,12 +222,14 @@ end

--- much inefficiencies. dun care
---@param displayed string[]
function HarpoonList:resolve_displayed(displayed)
---@param length number
function HarpoonList:resolve_displayed(displayed, length)
local new_list = {}

local list_displayed = self:display()

for i, v in ipairs(list_displayed) do
for i = 1, self._length do
local v = self.items[i]
local index = index_of(displayed, v)
if index == -1 then
Extensions.extensions:emit(
Expand All @@ -146,9 +239,12 @@ function HarpoonList:resolve_displayed(displayed)
end
end

for i, v in ipairs(displayed) do
for i = 1, length do
local v = displayed[i]
local index = index_of(list_displayed, v)
if index == -1 then
if v == "" then
new_list[i] = nil
elseif index == -1 then
new_list[i] = self.config.create_list_item(self.config, v)
Extensions.extensions:emit(
Extensions.event_names.ADD,
Expand All @@ -163,13 +259,15 @@ function HarpoonList:resolve_displayed(displayed)
end
local index_in_new_list =
index_of(new_list, self.items[index], self.config)

if index_in_new_list == -1 then
new_list[i] = self.items[index]
end
end
end

self.items = new_list
self._length = length
end

function HarpoonList:select(index, options)
Expand All @@ -189,11 +287,11 @@ function HarpoonList:next(opts)
opts = opts or {}

self._index = self._index + 1
if self._index > #self.items then
if self._index > self._length then
if opts.ui_nav_wrap then
self._index = 1
else
self._index = #self.items
self._index = self._length
end
end

Expand All @@ -220,8 +318,9 @@ end
--- @return string[]
function HarpoonList:display()
local out = {}
for _, v in ipairs(self.items) do
table.insert(out, self.config.display(v))
for i = 1, self._length do
local v = self.items[i]
out[i] = v == nil and "" or self.config.display(v)
end

return out
Expand Down
10 changes: 5 additions & 5 deletions lua/harpoon/test/harpoon_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe("harpoon", function()
"qux",
}, row, col)

local list = harpoon:list():append()
local list = harpoon:list():add()
local other_buf = utils.create_file("other-file", {
"foo",
"bar",
Expand Down Expand Up @@ -56,7 +56,7 @@ describe("harpoon", function()
}, row, col)

local list = harpoon:list()
list:append()
list:add()
harpoon:sync()

eq(harpoon:dump(), {
Expand All @@ -66,7 +66,7 @@ describe("harpoon", function()
})
end)

it("prepend/append double add", function()
it("prepend/add double add", function()
local default_list_name = harpoon:info().default_list_name
local file_name_1 = "/tmp/harpoon-test"
local row_1 = 3
Expand All @@ -79,7 +79,7 @@ describe("harpoon", function()
local contents = { "foo", "bar", "baz", "qux" }

local bufnr_1 = utils.create_file(file_name_1, contents, row_1, col_1)
local list = harpoon:list():append()
local list = harpoon:list():add()

utils.create_file(file_name_2, contents, row_2, col_2)
harpoon:list():prepend()
Expand All @@ -97,7 +97,7 @@ describe("harpoon", function()
{ value = file_name_1, context = { row = row_1, col = col_1 } },
})

harpoon:list():append()
harpoon:list():add()
vim.api.nvim_set_current_buf(bufnr_1)
harpoon:list():prepend()

Expand Down
Loading

0 comments on commit a33cbab

Please sign in to comment.