From 9bf63b4e3ebc8dca60923ef7320c8a3398904a26 Mon Sep 17 00:00:00 2001 From: ThePrimeagen Date: Wed, 10 Mar 2021 08:27:51 -0700 Subject: [PATCH 1/2] 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. --- lua/harpoon/init.lua | 49 +++++++++++++++++++--------------- lua/harpoon/mark.lua | 62 ++++++++++++++++++++++++++----------------- lua/harpoon/utils.lua | 9 +++++-- 3 files changed, 71 insertions(+), 49 deletions(-) diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua index 4bb4387..787c8ce 100644 --- a/lua/harpoon/init.lua +++ b/lua/harpoon/init.lua @@ -1,7 +1,8 @@ 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 data_path = vim.fn.stdpath("data") +local utils = require("harpoon.utils") local user_config = string.format("%s/harpoon.json", config_path) local cache_config = string.format("%s/harpoon.json", data_path) @@ -26,10 +27,10 @@ local M = {} ... high level settings } --]] -harpoon_config = harpoon_config or {} +HarpoonConfig = HarpoonConfig or {} -- 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 if type(v) == "table" then if type(t1[k]) == "table" then @@ -43,7 +44,7 @@ function merge_table_impl(t1, t2) end end -function merge_tables(...) +local function merge_tables(...) local out = {} for i = 2, select("#",...) do merge_table_impl(out, select(i, ...)) @@ -51,7 +52,7 @@ function merge_tables(...) return out end -function ensure_correct_config(config) +local function ensure_correct_config(config) local projects = config.projects if projects[cwd] == nil then projects[cwd] = { @@ -64,18 +65,23 @@ function ensure_correct_config(config) } end - if projects[cwd].mark == nil then - projects[cwd].mark = {marks = {}} + local proj = projects[cwd] + if proj.mark == nil then + proj.mark = {marks = {}} end - if projects[cwd].term == nil then - projects[cwd].term = {cmds = {}} + if proj.term == nil then + proj.term = {cmds = {}} + end + + local marks = proj.mark.marks + for idx = 1, #marks do + marks[idx] = utils.normalize_path(marks[idx]) end end -function expand_dir(config) +local function expand_dir(config) local projects = config.projects or {} - local expanded_config = {} for k in pairs(projects) do local expanded_path = Path.new(k):expand() projects[expanded_path] = projects[k] @@ -85,20 +91,21 @@ function expand_dir(config) end 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 -- 1. saved. Where do we save? M.setup = function(config) - function read_config(config) - return vim.fn.json_decode(Path:new(config):read()) - end if not config then config = {} 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) if not ok then @@ -120,22 +127,20 @@ M.setup = function(config) -- an object for cwd ensure_correct_config(complete_config) - harpoon_config = complete_config + HarpoonConfig = complete_config end M.get_term_config = function() - ensure_correct_config(harpoon_config) - return harpoon_config.projects[cwd].term + return HarpoonConfig.projects[cwd].term end M.get_mark_config = function() - ensure_correct_config(harpoon_config) - return harpoon_config.projects[cwd].mark + return HarpoonConfig.projects[cwd].mark end -- should only be called for debug purposes M.print_config = function() - print(vim.inspect(harpoon_config)) + print(vim.inspect(HarpoonConfig)) end -- Sets a default config with no values diff --git a/lua/harpoon/mark.lua b/lua/harpoon/mark.lua index 21d38b2..5bf8f9a 100644 --- a/lua/harpoon/mark.lua +++ b/lua/harpoon/mark.lua @@ -1,25 +1,23 @@ local Path = require('plenary.path') local harpoon = require('harpoon') +local utils = require('harpoon.utils') local M = {} -function get_id_or_current_buffer(id) - if id == nil then - return vim.fn.bufname(vim.fn.bufnr()) - end - - return id +local function valid_index(idx) + return idx ~= nil and harpoon.get_mark_config().marks[idx] ~= nil end -function get_index_of(item) +local function get_index_of(item) 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.") return end local config = harpoon.get_mark_config() if type(item) == 'string' then + local relative_item = utils.normalize_path(item) for idx = 1, #config.marks do - if config.marks[idx] == item then + if config.marks[idx] == relative_item then return idx end end @@ -38,14 +36,27 @@ function get_index_of(item) return nil end -function valid_index(idx) - return idx ~= nil and harpoon.get_mark_config().marks[idx] ~= nil +local function get_id_or_current_buffer(id) + 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 M.get_index_of = get_index_of 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 tmp = config.marks[a_idx] config.marks[a_idx] = config.marks[b_idx] @@ -54,6 +65,7 @@ end M.add_file = function() local buf_name = get_id_or_current_buffer() + if valid_index(get_index_of(buf_name)) then -- we don't alter file layout. return @@ -70,9 +82,9 @@ M.add_file = function() table.insert(config.marks, buf_name) end -M.store_offset = function() - local id = get_id_or_current_buffer() - local idx = get_index_of(id) +M.store_offset = function() + local buf_name = get_id_or_current_buffer() + local idx = get_index_of(buf_name) if not valid_index(idx) then return end @@ -92,8 +104,8 @@ M.swap = function(a, b) end M.rm_file = function() - local id = get_id_or_current_buffer() - local idx = get_index_of(id) + local buf_name = get_id_or_current_buffer() + local idx = get_index_of(buf_name) if not valid_index(idx) then return @@ -102,7 +114,7 @@ M.rm_file = function() harpoon.get_mark_config().marks[idx] = nil end -M.trim = function() +M.trim = function() M.shorten_list(idx) end @@ -111,8 +123,8 @@ M.clear_all = function() end M.promote = function(id) - local id = get_id_or_current_buffer(id) - local idx = get_index_of(id) + local buf_name = get_id_or_current_buffer(id) + local idx = get_index_of(buf_name) if not valid_index(idx) or idx == 1 then return @@ -122,9 +134,9 @@ M.promote = function(id) end 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 return end @@ -144,10 +156,10 @@ M.remove_nils = function() config.marks = next end -M.shorten_list = function(count) +M.shorten_list = function(count) if not count then - local id = get_id_or_current_buffer() - local idx = get_index_of(id) + local buf_name = get_id_or_current_buffer() + local idx = get_index_of(buf_name) if not valid_index(idx) then return @@ -169,7 +181,7 @@ M.get_marked_file = function(idx) return harpoon.get_mark_config().marks[idx] end -M.get_length = function() +M.get_length = function() return #harpoon.get_mark_config().marks end diff --git a/lua/harpoon/utils.lua b/lua/harpoon/utils.lua index 74e9650..8b285da 100644 --- a/lua/harpoon/utils.lua +++ b/lua/harpoon/utils.lua @@ -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 M = { cwd = cwd, - data_path, + data_path = data_path, + normalize_path = function(item) + return Path:new(item):make_relative(cwd) + end } +return M From 22896b9b1535d1a2fe4dccb56d9ab254fa65f40b Mon Sep 17 00:00:00 2001 From: ThePrimeagen Date: Wed, 10 Mar 2021 08:33:01 -0700 Subject: [PATCH 2/2] fix(made-clear): made get_id_or_current_buffer more clear --- lua/harpoon/mark.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lua/harpoon/mark.lua b/lua/harpoon/mark.lua index 5bf8f9a..abb7485 100644 --- a/lua/harpoon/mark.lua +++ b/lua/harpoon/mark.lua @@ -36,7 +36,7 @@ local function get_index_of(item) return nil end -local function get_id_or_current_buffer(id) +local function get_buf_name(id) if id == nil then return utils.normalize_path(vim.fn.bufname(vim.fn.bufnr())) elseif type(id) == "string" then @@ -64,7 +64,7 @@ local function swap(a_idx, b_idx) end M.add_file = function() - local buf_name = get_id_or_current_buffer() + local buf_name = get_buf_name() if valid_index(get_index_of(buf_name)) then -- we don't alter file layout. @@ -83,7 +83,7 @@ M.add_file = function() end M.store_offset = function() - local buf_name = get_id_or_current_buffer() + local buf_name = get_buf_name() local idx = get_index_of(buf_name) if not valid_index(idx) then return @@ -94,7 +94,7 @@ end M.swap = function(a, b) local a_idx = get_index_of(a) - local b_idx = get_index_of(get_id_or_current_buffer(b)) + local b_idx = get_index_of(get_buf_name(b)) if not valid_index(a_idx) or not valid_index(b_idx) then return @@ -104,7 +104,7 @@ M.swap = function(a, b) end M.rm_file = function() - local buf_name = get_id_or_current_buffer() + local buf_name = get_buf_name() local idx = get_index_of(buf_name) if not valid_index(idx) then @@ -123,7 +123,7 @@ M.clear_all = function() end M.promote = function(id) - local buf_name = get_id_or_current_buffer(id) + local buf_name = get_buf_name(id) local idx = get_index_of(buf_name) if not valid_index(idx) or idx == 1 then @@ -134,7 +134,7 @@ M.promote = function(id) end M.promote_to_front = function(id) - local buf_name = get_id_or_current_buffer(id) + local buf_name = get_buf_name(id) local idx = get_index_of(buf_name) if not valid_index(idx) or idx == 1 then @@ -158,7 +158,7 @@ end M.shorten_list = function(count) if not count then - local buf_name = get_id_or_current_buffer() + local buf_name = get_buf_name() local idx = get_index_of(buf_name) if not valid_index(idx) then