fix(issue-number-4): Like love potion # 9

Yes, we did it
This commit is contained in:
ThePrimeagen 2021-02-22 20:36:14 -07:00
parent 322d07be58
commit 3ff6f1d6a0
3 changed files with 40 additions and 101 deletions

View File

@ -1,6 +1,4 @@
local Path = require("plenary.path") local Path = require("plenary.path")
local terminals = require("harpoon.term")
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")
@ -132,11 +130,19 @@ M.setup = function(config)
-- an object for cwd -- an object for cwd
ensure_correct_config(complete_config) ensure_correct_config(complete_config)
terminals.setup(complete_config.projects[cwd].term)
mark.setup(complete_config.projects[cwd].mark)
harpoon_config = complete_config harpoon_config = complete_config
end end
M.get_term_config = function()
ensure_correct_config(harpoon_config)
return harpoon_config.projects[cwd].term
end
M.get_mark_config = function()
ensure_correct_config(harpoon_config)
return harpoon_config.projects[cwd].mark
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(harpoon_config))

View File

@ -1,9 +1,7 @@
local Path = require('plenary.path') local Path = require('plenary.path')
local harpoon = require('harpoon')
local M = {} local M = {}
local cwd = cwd or vim.loop.cwd()
mark_config = mark_config or {}
function get_id_or_current_buffer(id) function get_id_or_current_buffer(id)
if id == nil then if id == nil then
@ -13,32 +11,15 @@ function get_id_or_current_buffer(id)
return id return id
end end
M.setup = function(config)
mark_config = config
if mark_config.marks == nil then
-- resetting the mark config if there is an issue loading the config
-- this can hide errors.
--
-- TODO: create a logging mechanism to get these values
mark_config = {
marks = {}
}
end
end
M.get_config = function()
return mark_config
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
local config = harpoon.get_mark_config()
if type(item) == 'string' then if type(item) == 'string' then
for idx = 1, #mark_config.marks do for idx = 1, #config.marks do
if mark_config.marks[idx] == item then if config.marks[idx] == item then
return idx return idx
end end
end end
@ -50,7 +31,7 @@ function get_index_of(item)
item = item + 1 item = item + 1
end end
if item <= #mark_config.marks and item >= 1 then if item <= #config.marks and item >= 1 then
return item return item
end end
@ -58,16 +39,17 @@ function get_index_of(item)
end end
function valid_index(idx) function valid_index(idx)
return idx ~= nil and mark_config.marks[idx] ~= nil return idx ~= nil and harpoon.get_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 = mark_config.marks[a_idx] local config = harpoon.get_mark_config()
mark_config.marks[a_idx] = mark_config.marks[b_idx] local tmp = config.marks[a_idx]
mark_config.marks[b_idx] = tmp config.marks[a_idx] = mark_config.marks[b_idx]
config.marks[b_idx] = tmp
end end
M.add_file = function() M.add_file = function()
@ -77,14 +59,15 @@ M.add_file = function()
return return
end end
for idx = 1, #mark_config.marks do local config = harpoon.get_mark_config()
if mark_config.marks[idx] == nil then for idx = 1, #config.marks do
mark_config.marks[idx] = buf_name if config.marks[idx] == nil then
config.marks[idx] = buf_name
return return
end end
end end
table.insert(mark_config.marks, buf_name) table.insert(config.marks, buf_name)
end end
M.store_offset = function() M.store_offset = function()
@ -116,7 +99,7 @@ M.rm_file = function()
return return
end end
mark_config.marks[idx] = nil harpoon.get_mark_config().marks[idx] = nil
end end
M.trim = function() M.trim = function()
@ -124,7 +107,7 @@ M.trim = function()
end end
M.clear_all = function() M.clear_all = function()
mark_config.marks = {} harpoon.get_mark_config().marks = {}
end end
M.promote = function(id) M.promote = function(id)
@ -151,13 +134,14 @@ end
M.remove_nils = function() M.remove_nils = function()
local next = {} local next = {}
for idx = 1, #mark_config.marks do local config = harpoon.get_mark_config()
if mark_config.marks[idx] ~= nil then for idx = 1, #config.marks do
table.insert(next, mark_config.marks[idx]) if config.marks[idx] ~= nil then
table.insert(next, config.marks[idx])
end end
end end
mark_config.marks = next config.marks = next
end end
M.shorten_list = function(count) M.shorten_list = function(count)
@ -173,19 +157,20 @@ M.shorten_list = function(count)
end end
local next = {} local next = {}
local up_to = math.min(count, #mark_config.marks) local config = harpoon.get_mark_config()
local up_to = math.min(count, #config.marks)
for idx = 1, up_to do for idx = 1, up_to do
table.insert(next, mark_config.marks[idx]) table.insert(next, config.marks[idx])
end end
mark_config.marks = next config.marks = next
end end
M.get_marked_file = function(idx) M.get_marked_file = function(idx)
return mark_config.marks[idx] return harpoon.get_mark_config().marks[idx]
end end
M.get_length = function() M.get_length = function()
return #mark_config.marks return #harpoon.get_mark_config().marks
end end
return M return M

View File

@ -1,5 +1,4 @@
local Path = require("plenary.path") local Path = require("plenary.path")
local cwd = cwd or vim.loop.cwd()
local M = {} local M = {}
@ -23,59 +22,8 @@ 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
lua require("harpoon").setup({
terminal: {
"/home/theprimeagen/work/netflix": {
"yarn build",
"yarn test",
"yarn dtest"
}
}
})
--]]
function getCmd(idx) function getCmd(idx)
local commandSet = terminal_config[cwd] return
if not commandSet then
return nil
end
return commandSet[idx]
end
--[[
{
projects: {
"/path/to/dir": {
term: {
cmds: string[],
... top level settings .. (we don't have)
}
mark: {
marks: string[], // very skept -- has odd behavior
... top level settings .. (we don't have)
}
}
}
}
--]]
M.setup = function(config)
terminal_config = config
if terminal_config.cmds == nil then
-- Resets terminal config if there is some missing values.
--
-- TODO: create a logging mechanism to get these values
terminal_config = {
cmds = {}
}
end
end end
M.gotoTerminal = function(idx) M.gotoTerminal = function(idx)
@ -105,7 +53,7 @@ M.sendCommand = function(idx, cmd)
end end
if type(cmd) == "number" then if type(cmd) == "number" then
cmd = getCmd(cmd) cmd = terminal_config.cmds[cmd]
end end
if cmd then if cmd then