fixes(paths): The paths were too unique...

I had to normalize the paths due to the fact that sometimes a path to a
file would be absolute or relative to the project depending on how you
opened up the file.  To prevent this from creating duplicates in a mark
list, I normalized the path before doing anything.
This commit is contained in:
ThePrimeagen 2021-03-10 08:27:51 -07:00
parent ac6fabb31f
commit 9bf63b4e3e
3 changed files with 71 additions and 49 deletions

View File

@ -1,7 +1,8 @@
local Path = require("plenary.path") local Path = require("plenary.path")
local cwd = cwd or vim.loop.cwd() local cwd = vim.loop.cwd()
local config_path = vim.fn.stdpath("config") local config_path = vim.fn.stdpath("config")
local data_path = vim.fn.stdpath("data") local data_path = vim.fn.stdpath("data")
local utils = require("harpoon.utils")
local user_config = string.format("%s/harpoon.json", config_path) local user_config = string.format("%s/harpoon.json", config_path)
local cache_config = string.format("%s/harpoon.json", data_path) local cache_config = string.format("%s/harpoon.json", data_path)
@ -26,10 +27,10 @@ local M = {}
... high level settings ... high level settings
} }
--]] --]]
harpoon_config = harpoon_config or {} HarpoonConfig = HarpoonConfig or {}
-- tbl_deep_extend does not work the way you would think -- tbl_deep_extend does not work the way you would think
function merge_table_impl(t1, t2) local function merge_table_impl(t1, t2)
for k, v in pairs(t2) do for k, v in pairs(t2) do
if type(v) == "table" then if type(v) == "table" then
if type(t1[k]) == "table" then if type(t1[k]) == "table" then
@ -43,7 +44,7 @@ function merge_table_impl(t1, t2)
end end
end end
function merge_tables(...) local function merge_tables(...)
local out = {} local out = {}
for i = 2, select("#",...) do for i = 2, select("#",...) do
merge_table_impl(out, select(i, ...)) merge_table_impl(out, select(i, ...))
@ -51,7 +52,7 @@ function merge_tables(...)
return out return out
end end
function ensure_correct_config(config) local function ensure_correct_config(config)
local projects = config.projects local projects = config.projects
if projects[cwd] == nil then if projects[cwd] == nil then
projects[cwd] = { projects[cwd] = {
@ -64,18 +65,23 @@ function ensure_correct_config(config)
} }
end end
if projects[cwd].mark == nil then local proj = projects[cwd]
projects[cwd].mark = {marks = {}} if proj.mark == nil then
proj.mark = {marks = {}}
end end
if projects[cwd].term == nil then if proj.term == nil then
projects[cwd].term = {cmds = {}} proj.term = {cmds = {}}
end
local marks = proj.mark.marks
for idx = 1, #marks do
marks[idx] = utils.normalize_path(marks[idx])
end end
end end
function expand_dir(config) local function expand_dir(config)
local projects = config.projects or {} local projects = config.projects or {}
local expanded_config = {}
for k in pairs(projects) do for k in pairs(projects) do
local expanded_path = Path.new(k):expand() local expanded_path = Path.new(k):expand()
projects[expanded_path] = projects[k] projects[expanded_path] = projects[k]
@ -85,20 +91,21 @@ function expand_dir(config)
end end
M.save = function() M.save = function()
Path:new(cache_config):write(vim.fn.json_encode(harpoon_config), 'w') Path:new(cache_config):write(vim.fn.json_encode(HarpoonConfig), 'w')
end
local function read_config(local_config)
return vim.fn.json_decode(Path:new(local_config):read())
end end
-- 1. saved. Where do we save? -- 1. saved. Where do we save?
M.setup = function(config) M.setup = function(config)
function read_config(config)
return vim.fn.json_decode(Path:new(config):read())
end
if not config then if not config then
config = {} config = {}
end end
local ok, u_config = pcall(read_config, user_terminal_config) local ok, u_config = pcall(read_config, user_config)
local ok2, c_config = pcall(read_config, cache_config) local ok2, c_config = pcall(read_config, cache_config)
if not ok then if not ok then
@ -120,22 +127,20 @@ M.setup = function(config)
-- an object for cwd -- an object for cwd
ensure_correct_config(complete_config) ensure_correct_config(complete_config)
harpoon_config = complete_config HarpoonConfig = complete_config
end end
M.get_term_config = function() M.get_term_config = function()
ensure_correct_config(harpoon_config) return HarpoonConfig.projects[cwd].term
return harpoon_config.projects[cwd].term
end end
M.get_mark_config = function() M.get_mark_config = function()
ensure_correct_config(harpoon_config) return HarpoonConfig.projects[cwd].mark
return harpoon_config.projects[cwd].mark
end end
-- should only be called for debug purposes -- should only be called for debug purposes
M.print_config = function() M.print_config = function()
print(vim.inspect(harpoon_config)) print(vim.inspect(HarpoonConfig))
end end
-- Sets a default config with no values -- Sets a default config with no values

View File

@ -1,25 +1,23 @@
local Path = require('plenary.path') local Path = require('plenary.path')
local harpoon = require('harpoon') local harpoon = require('harpoon')
local utils = require('harpoon.utils')
local M = {} local M = {}
function get_id_or_current_buffer(id) local function valid_index(idx)
if id == nil then return idx ~= nil and harpoon.get_mark_config().marks[idx] ~= nil
return vim.fn.bufname(vim.fn.bufnr())
end
return id
end end
function get_index_of(item) local function get_index_of(item)
if item == nil then if item == nil then
error("You have provided a nil value to Harpoon, please provide a string rep of the file or the file idx.") error("You have provided a nil value to Harpoon, please provide a string rep of the file or the file idx.")
return return
end end
local config = harpoon.get_mark_config() local config = harpoon.get_mark_config()
if type(item) == 'string' then if type(item) == 'string' then
local relative_item = utils.normalize_path(item)
for idx = 1, #config.marks do for idx = 1, #config.marks do
if config.marks[idx] == item then if config.marks[idx] == relative_item then
return idx return idx
end end
end end
@ -38,14 +36,27 @@ function get_index_of(item)
return nil return nil
end end
function valid_index(idx) local function get_id_or_current_buffer(id)
return idx ~= nil and harpoon.get_mark_config().marks[idx] ~= nil if id == nil then
return utils.normalize_path(vim.fn.bufname(vim.fn.bufnr()))
elseif type(id) == "string" then
return utils.normalize_path(id)
end
local idx = get_index_of(id)
if valid_index(idx) then
return harpoon.get_mark_config().marks[idx]
end
--
-- not sure what to do here...
--
return ""
end end
M.get_index_of = get_index_of M.get_index_of = get_index_of
M.valid_index = valid_index M.valid_index = valid_index
function swap(a_idx, b_idx) local function swap(a_idx, b_idx)
local config = harpoon.get_mark_config() local config = harpoon.get_mark_config()
local tmp = config.marks[a_idx] local tmp = config.marks[a_idx]
config.marks[a_idx] = config.marks[b_idx] config.marks[a_idx] = config.marks[b_idx]
@ -54,6 +65,7 @@ end
M.add_file = function() M.add_file = function()
local buf_name = get_id_or_current_buffer() local buf_name = get_id_or_current_buffer()
if valid_index(get_index_of(buf_name)) then if valid_index(get_index_of(buf_name)) then
-- we don't alter file layout. -- we don't alter file layout.
return return
@ -70,9 +82,9 @@ M.add_file = function()
table.insert(config.marks, buf_name) table.insert(config.marks, buf_name)
end end
M.store_offset = function() M.store_offset = function()
local id = get_id_or_current_buffer() local buf_name = get_id_or_current_buffer()
local idx = get_index_of(id) local idx = get_index_of(buf_name)
if not valid_index(idx) then if not valid_index(idx) then
return return
end end
@ -92,8 +104,8 @@ M.swap = function(a, b)
end end
M.rm_file = function() M.rm_file = function()
local id = get_id_or_current_buffer() local buf_name = get_id_or_current_buffer()
local idx = get_index_of(id) local idx = get_index_of(buf_name)
if not valid_index(idx) then if not valid_index(idx) then
return return
@ -102,7 +114,7 @@ M.rm_file = function()
harpoon.get_mark_config().marks[idx] = nil harpoon.get_mark_config().marks[idx] = nil
end end
M.trim = function() M.trim = function()
M.shorten_list(idx) M.shorten_list(idx)
end end
@ -111,8 +123,8 @@ M.clear_all = function()
end end
M.promote = function(id) M.promote = function(id)
local id = get_id_or_current_buffer(id) local buf_name = get_id_or_current_buffer(id)
local idx = get_index_of(id) local idx = get_index_of(buf_name)
if not valid_index(idx) or idx == 1 then if not valid_index(idx) or idx == 1 then
return return
@ -122,9 +134,9 @@ M.promote = function(id)
end end
M.promote_to_front = function(id) M.promote_to_front = function(id)
id = get_id_or_current_buffer(id) local buf_name = get_id_or_current_buffer(id)
local idx = get_index_of(buf_name)
idx = get_index_of(id)
if not valid_index(idx) or idx == 1 then if not valid_index(idx) or idx == 1 then
return return
end end
@ -144,10 +156,10 @@ M.remove_nils = function()
config.marks = next config.marks = next
end end
M.shorten_list = function(count) M.shorten_list = function(count)
if not count then if not count then
local id = get_id_or_current_buffer() local buf_name = get_id_or_current_buffer()
local idx = get_index_of(id) local idx = get_index_of(buf_name)
if not valid_index(idx) then if not valid_index(idx) then
return return
@ -169,7 +181,7 @@ M.get_marked_file = function(idx)
return harpoon.get_mark_config().marks[idx] return harpoon.get_mark_config().marks[idx]
end end
M.get_length = function() M.get_length = function()
return #harpoon.get_mark_config().marks return #harpoon.get_mark_config().marks
end end

View File

@ -1,8 +1,13 @@
local cwd = cwd or vim.loop.cwd() local Path = require("plenary.path")
local cwd = vim.loop.cwd()
local data_path = vim.fn.stdpath("data") local data_path = vim.fn.stdpath("data")
local M = { local M = {
cwd = cwd, cwd = cwd,
data_path, data_path = data_path,
normalize_path = function(item)
return Path:new(item):make_relative(cwd)
end
} }
return M