Merge pull request #22 from ThePrimeagen/clean-up

fixes(paths): The paths were too unique...
This commit is contained in:
ThePrimeagen 2021-03-10 18:15:35 -07:00 committed by GitHub
commit 00529d3ec2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 51 deletions

View File

@ -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

View File

@ -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_buf_name(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]
@ -53,7 +64,8 @@ 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.
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_buf_name()
local idx = get_index_of(buf_name)
if not valid_index(idx) then
return
end
@ -82,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
@ -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_buf_name()
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_buf_name(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_buf_name(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_buf_name()
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

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 M = {
cwd = cwd,
data_path,
data_path = data_path,
normalize_path = function(item)
return Path:new(item):make_relative(cwd)
end
}
return M