feat(yayayaya): Term and mark are now using configs

This commit is contained in:
ThePrimeagen 2021-02-19 16:24:07 -07:00
parent 4e16fc9026
commit 4dbd024b32
5 changed files with 90 additions and 86 deletions

View File

@ -1,24 +1,63 @@
local Path = require("plenary.path")
local terminals = require("harpoon.term") local terminals = require("harpoon.term")
local manage = require("harpoon.mark") local mark = require("harpoon.mark")
local cwd = cwd or vim.loop.cwd() local cwd = cwd or 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 user_terminal_config = string.format("%s/harpoon-terminal.json", config_path) local user_config = string.format("%s/harpoon.json", config_path)
local cache_terminal_config = string.format("%s/harpoon-terminal.json", data_path) local cache_config = string.format("%s/harpoon.json", data_path)
local M = {} local M = {}
function expand_dir(projects) --[[
{
projects = {
["/path/to/director"] = {
term = {
cmds = {
}
... is there antyhnig that could be options?
},
mark = {
marks = {
}
... is there antyhnig that could be options?
}
}
},
... high level settings
}
--]]
harpoon_config = harpoon_config or {}
function expand_dir(config)
local projects = config.projects or {}
local expanded_config = {} 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]
end end
return config
end
M.save = function()
local term_config = terminals.get_config()
local mark_config = mark.get_config()
if not harpoon_config.projects[cwd] then
harpoon_config.projects[cwd] = {}
end
harpoon_config.projects[cwd].term = term_config
harpoon_config.projects[cwd].mark = mark_config
Path:new(cache_config):write(vim.fn.json_encode(harpoon_config), 'w')
end end
-- 1. saved. Where do we save? -- 1. saved. Where do we save?
M.setup = function(config) M.setup = function(config)
function read_terminal_config(config) function read_config(config)
return vim.fn.json_decode(Path:new(config):read()) return vim.fn.json_decode(Path:new(config):read())
end end
@ -26,22 +65,27 @@ M.setup = function(config)
config = {} config = {}
end end
local ok, user_config = pcall(read_terminal_config, user_terminal_config) local ok, u_config = pcall(read_config, user_terminal_config)
local ok2, cache_config = pcall(read_terminal_config, data_terminal_config) local ok2, c_config = pcall(read_config, cache_config)
if not ok then if not ok then
user_config = {} u_config = {}
end end
if not ok2 then if not ok2 then
cache_config = {} c_config = {}
end end
local complete_config = local complete_config =
vim.tbl_deep_extend("force", {}, cache_config, user_config, config) vim.tbl_deep_extend("force",
{projects = {}},
expand_dir(c_config),
expand_dir(u_config),
expand_dir(config))
terminals.setup(complete_config) terminals.setup(complete_config)
manage.setup(complete_config) mark.setup(complete_config)
harpoon_config = complete_config
end end
return M return M

View File

@ -1,34 +1,9 @@
local Path = require('plenary.path') local Path = require('plenary.path')
local M = {} local M = {}
local cwd = cwd or vim.loop.cwd() local cwd = cwd or vim.loop.cwd()
local data_path = vim.fn.stdpath("data")
local mark_config = {}
cwd = cwd:gsub("/", "_") mark_config = mark_config or {}
local file_name = string.format("%s/%s.cache", data_path, cwd)
--[[
{
projects = {
["/path/to/director"] = {
term = {
cmds = {
}
... is there antyhnig that could be options?
},
mark = {
marks = {
}
... is there antyhnig that could be options?
}
}
},
... high level settings
}
--]]
function get_id_or_current_buffer(id) function get_id_or_current_buffer(id)
if id == nil then if id == nil then
@ -38,25 +13,9 @@ function get_id_or_current_buffer(id)
return id return id
end end
function hydrate_from_cache()
ok, res = pcall(function()
local results = Path:new(file_name):read()
if results == nil then
return {}, {}
end
return vim.fn.json_decode(results), {}
end)
if ok then
return res, {}
end
return {}, {}
end
M.setup = function(config) M.setup = function(config)
if not config.projects[cwd] then if not config.projects[cwd] then
mark_config = {} mark_config = {marks = {}}
else else
mark_config = config.projects[cwd].mark mark_config = config.projects[cwd].mark
end end
@ -66,22 +25,14 @@ M.get_config = function()
return mark_config return mark_config
end end
M.save = function()
Path:new(file_name):write(vim.fn.json_encode(marked_files), 'w')
end
if not marked_files then
marked_files, marked_offsets = hydrate_from_cache()
end
function get_index_of(item) 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
if type(item) == 'string' then if type(item) == 'string' then
for idx = 1, #marked_files do for idx = 1, #mark_config.marks do
if marked_files[idx] == item then if mark_config.marks[idx] == item then
return idx return idx
end end
end end
@ -93,7 +44,7 @@ function get_index_of(item)
item = item + 1 item = item + 1
end end
if item <= #marked_files and item >= 1 then if item <= #mark_config.marks and item >= 1 then
return item return item
end end
@ -101,16 +52,16 @@ function get_index_of(item)
end end
function valid_index(idx) function valid_index(idx)
return idx ~= nil and marked_files[idx] ~= nil return idx ~= nil and mark_config.marks[idx] ~= nil
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) function swap(a_idx, b_idx)
local tmp = marked_files[a_idx] local tmp = mark_config.marks[a_idx]
marked_files[a_idx] = marked_files[b_idx] mark_config.marks[a_idx] = mark_config.marks[b_idx]
marked_files[b_idx] = tmp mark_config.marks[b_idx] = tmp
end end
M.add_file = function() M.add_file = function()
@ -120,14 +71,14 @@ M.add_file = function()
return return
end end
for idx = 1, #marked_files do for idx = 1, #mark_config.marks do
if marked_files[idx] == nil then if mark_config.marks[idx] == nil then
marked_files[idx] = buf_name mark_config.marks[idx] = buf_name
return return
end end
end end
table.insert(marked_files, buf_name) table.insert(mark_config.marks, buf_name)
end end
M.store_offset = function() M.store_offset = function()
@ -159,7 +110,7 @@ M.rm_file = function()
return return
end end
marked_files[idx] = nil mark_config.marks[idx] = nil
end end
M.trim = function() M.trim = function()
@ -167,7 +118,7 @@ M.trim = function()
end end
M.clear_all = function() M.clear_all = function()
marked_files = {} mark_config.marks = {}
end end
M.promote = function(id) M.promote = function(id)
@ -194,13 +145,13 @@ end
M.remove_nils = function() M.remove_nils = function()
local next = {} local next = {}
for idx = 1, #marked_files do for idx = 1, #mark_config.marks do
if marked_files[idx] ~= nil then if mark_config.marks[idx] ~= nil then
table.insert(next, marked_files[idx]) table.insert(next, mark_config.marks[idx])
end end
end end
marked_files = next mark_config.marks = next
end end
M.shorten_list = function(count) M.shorten_list = function(count)
@ -216,19 +167,19 @@ M.shorten_list = function(count)
end end
local next = {} local next = {}
local up_to = math.min(count, #marked_files) local up_to = math.min(count, #mark_config.marks)
for idx = 1, up_to do for idx = 1, up_to do
table.insert(next, marked_files[idx]) table.insert(next, mark_config.marks[idx])
end end
marked_files = next mark_config.marks = next
end end
M.get_marked_file = function(idx) M.get_marked_file = function(idx)
return marked_files[idx] return mark_config.marks[idx]
end end
M.get_length = function() M.get_length = function()
return #marked_files return #mark_config.marks
end end
return M return M

View File

@ -3,7 +3,7 @@ local cwd = cwd or vim.loop.cwd()
local M = {} local M = {}
local terminal_config = { } terminal_config = terminal_config or { }
local terminals = {} local terminals = {}
function create_terminal() function create_terminal()
@ -23,6 +23,10 @@ function create_terminal()
return buf_id, term_id return buf_id, term_id
end end
M.get_config = function()
return terminal_config
end
--[[ --[[
-- First iteration of the setup script -- First iteration of the setup script
lua require("harpoon").setup({ lua require("harpoon").setup({

View File

@ -1,5 +1,5 @@
augroup THE_PRIMEAGEN_HARPOON augroup THE_PRIMEAGEN_HARPOON
autocmd! autocmd!
autocmd VimLeave * :lua require('harpoon.mark').save() autocmd VimLeave * :lua require('harpoon').save()
autocmd BufLeave * :lua require('harpoon.mark').store_offset() autocmd BufLeave * :lua require('harpoon.mark').store_offset()
augroup END augroup END

5
plugin/mark.vim Normal file
View File

@ -0,0 +1,5 @@
augroup THE_PRIMEAGEN_HARPOON
autocmd!
autocmd VimLeave * :lua require('harpoon').save()
autocmd BufLeave * :lua require('harpoon.mark').store_offset()
augroup END