Skip to content

Commit

Permalink
use Plenary.Path:absolute() and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrzaw committed Jan 6, 2024
1 parent 6d41f71 commit 7d6c570
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 54 deletions.
47 changes: 24 additions & 23 deletions lua/harpoon/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ M.DEFAULT_LIST = DEFAULT_LIST
---@field decode? (fun(obj: string): any)
---@field display? (fun(list_item: HarpoonListItem): string)
---@field select? (fun(list_item?: HarpoonListItem, list: HarpoonList, options: any?): nil)
---@field equals? (fun(list_line_a: HarpoonListItem, list_line_b: HarpoonListItem): boolean)
---@field equals? (fun(list_item_a: HarpoonListItem, list_item_b: HarpoonListItem): boolean)
---@field create_list_item? fun(config: HarpoonPartialConfigItem, item: any?): HarpoonListItem
---@field BufLeave? fun(evt: any, list: HarpoonList): nil
---@field VimLeavePre? fun(evt: any, list: HarpoonList): nil
Expand Down Expand Up @@ -147,34 +147,35 @@ function M.get_default_config()
end,

---@param config HarpoonPartialConfigItem
---@param name? any
---@param item? any
---@return HarpoonListItem
create_list_item = function(config, name)
if name ~= nil and string.sub(name, 1, 1) ~= "/" then
if string.sub(name, 1, 2) == "./" then
name = string.sub(name, 3)
end
name = config.get_root_dir() .. "/" .. name
create_list_item = function(config, item)
if item == nil then
item = vim.api.nvim_buf_get_name(
vim.api.nvim_get_current_buf()
)
end
name = name
or vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())

Logger:log("config_default#create_list_item", name)

local bufnr = vim.fn.bufnr(name, false)
if type(item) == "string" then
local name = Path:new(item):absolute()
local bufnr = vim.fn.bufnr(name, false)

local pos = { 1, 0 }
if bufnr ~= -1 then
pos = vim.api.nvim_win_get_cursor(0)
local pos = { 1, 0 }
if bufnr ~= -1 then
pos = vim.api.nvim_win_get_cursor(0)
end
item = {
value = name,
context = {
row = pos[1],
col = pos[2],
},
}
end

return {
value = name,
context = {
row = pos[1],
col = pos[2],
},
}
Logger:log("config_default#create_list_item", item)

return item
end,

BufLeave = function(arg, list)
Expand Down
86 changes: 62 additions & 24 deletions lua/harpoon/test/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,67 @@ local Config = require("harpoon.config")
local eq = assert.are.same

describe("config", function()
it("default.create_list_item", function()
local config = Config.get_default_config()
local config_item = Config.get_config(config, "foo")

local filename = os.tmpname()
local bufnr = vim.fn.bufnr(filename, true)

vim.api.nvim_set_current_buf(bufnr)
vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, {
"foo",
"bar",
"baz",
"qux",
})
vim.api.nvim_win_set_cursor(0, { 3, 1 })

local item = config_item.create_list_item(config_item)
eq(item, {
value = filename,
context = {
row = 3,
col = 1,
},
})
describe("default.create_list_item", function()
it("sets position", function()
local config = Config.get_default_config()
local config_item = Config.get_config(config, "foo")

local filename = os.tmpname()
local bufnr = vim.fn.bufnr(filename, true)

vim.api.nvim_set_current_buf(bufnr)
vim.api.nvim_buf_set_text(0, 0, 0, 0, 0, {
"foo",
"bar",
"baz",
"qux",
})
vim.api.nvim_win_set_cursor(0, { 3, 1 })

local item = config_item.create_list_item(config_item)
eq(item, {
value = filename,
context = {
row = 3,
col = 1,
},
})
end)
it("normalizes filename with /./ in it", function()
local config = Config.get_default_config()
local config_item = Config.get_config(config, "foo")

local filename = "/foo/./bar/./baz.txt"

local item = config_item.create_list_item(config_item, filename)
eq("/foo/bar/baz.txt", item.value)
end)
it("normalizes filename with bar/../bar/ in it", function()
local config = Config.get_default_config()
local config_item = Config.get_config(config, "foo")

local filename = "/foo/bar/../bar/baz.txt"

local item = config_item.create_list_item(config_item, filename)
eq("/foo/bar/baz.txt", item.value)
end)
it("converts backtracking relative path to absolute", function()
local config = Config.get_default_config()
local config_item = Config.get_config(config, "foo")

local filename = "../foo/bar/baz.txt"

local item = config_item.create_list_item(config_item, filename)
eq("/foo/bar/baz.txt", item.value)
end)
it("converts forward relative path to absolute", function()
local config = Config.get_default_config()
local config_item = Config.get_config(config, "foo")

local filename = "foo/bar/baz.txt"

local item = config_item.create_list_item(config_item, filename)
eq(vim.loop.cwd() .. "/foo/bar/baz.txt", item.value)
end)
end)
end)
20 changes: 13 additions & 7 deletions lua/harpoon/test/ui_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,16 @@ describe("harpoon", function()
end)

it("add relative files from ui contents and save", function()
-- vim.loop.cwd() is /harpoon
local list = harpoon:list()
local created_files = utils.fill_list_with_files(3, list)

table.insert(created_files, "../relative_file_for_testing1")
created_files[4] = "../relative_file_for_testing1"
table.insert(created_files, "../harpoon/relative_file_for_testing2")
created_files[5] = "relative_file_for_testing2"
table.insert(created_files, "lua/relative_file_for_testing3")
created_files[6] = "lua/relative_file_for_testing3"
table.insert(created_files, "./lua/relative_file_for_testing3")
created_files[7] = "lua/relative_file_for_testing4"
table.insert(created_files, "./lua/relative_file_for_testing4")
table.insert(created_files, "../tests/relative_file_for_testing5")
table.insert(created_files, "lua/../lua/./relative_file_for_testing6")

eq(list:length(), 3)

Expand All @@ -87,8 +86,15 @@ describe("harpoon", function()
harpoon.ui:save()
harpoon.ui:toggle_quick_menu()

eq(list:length(), 7)
eq(list:display(), created_files)
created_files[4] = "/relative_file_for_testing1"
created_files[5] = "relative_file_for_testing2"
created_files[6] = "lua/relative_file_for_testing3"
created_files[7] = "lua/relative_file_for_testing4"
created_files[8] = "/tests/relative_file_for_testing5"
created_files[9] = "lua/relative_file_for_testing6"

eq(9, list:length())
eq(created_files, list:display())
end)

it("edit ui but toggle should not save", function()
Expand Down

0 comments on commit 7d6c570

Please sign in to comment.