feat(wildcards)

This commit is contained in:
ThePrimeagen 2021-04-07 16:04:19 -06:00
parent 0eb1161288
commit e23b7107f2
2 changed files with 70 additions and 8 deletions

View File

@ -7,9 +7,45 @@ local cache_config = string.format("%s/harpoon.json", data_path)
local M = {}
local function get_project_config(config)
local projs = config.projects
local cwd = vim.loop.cwd()
local cwd_parts = string.gmatch(cwd, Path.sep)
for k, v in pairs(projs) do
local start = string.find(k, "{}", 1, true)
if start == nil and k == cwd then
return projs[k], k
end
local k_parts = string.gmatch(k, Path.sep)
if #k_parts == #cwd_parts then
local found = true
local wildcard = nil
for idx = 1, #k_parts do
local k_part = k_parts[idx]
found = found and (k_part == "{}" or k_part == cwd_parts[idx])
if k_part == "{}" then
wildcard = cwd_parts[idx]
end
end
if found then
return projs[k], k, wildcard
end
end
end
return nil, nil
end
--[[
{
projects = {
["/path/to/other/{}"] = {
// TODO: pattern matching
}
["/path/to/director"] = {
term = {
cmds = {
@ -23,6 +59,10 @@ local M = {}
}
}
},
menu = {
// TODO: Be filled in on settings...
// WE should also consider just having a help doc
}
... high level settings
}
--]]
@ -52,9 +92,11 @@ local function merge_tables(...)
end
local function ensure_correct_config(config)
local projects = config.projects
if projects[vim.loop.cwd()] == nil then
projects[vim.loop.cwd()] = {
local projects, cwd, wildcard = get_project_config(config)
cwd = cwd or vim.loop.cwd
if projects[cwd] == nil then
projects[cwd] = {
mark = {
marks = {}
},
@ -64,7 +106,8 @@ local function ensure_correct_config(config)
}
end
local proj = projects[vim.loop.cwd()]
local proj = projects[cwd]
proj.wildcard = wildcard
if proj.mark == nil then
proj.mark = {marks = {}}
end
@ -146,6 +189,10 @@ M.get_term_config = function()
return ensure_correct_config(HarpoonConfig).projects[vim.loop.cwd()].term
end
M.get_wildcard = function()
return ensure_correct_config(HarpoonConfig).projects.wildcard
end
M.get_mark_config = function()
return ensure_correct_config(HarpoonConfig).projects[vim.loop.cwd()].mark
end

View File

@ -1,5 +1,4 @@
local harpoon = require('harpoon')
local Path = require("plenary.path")
local M = {}
local terminals = {}
@ -25,11 +24,25 @@ local function create_terminal()
return buf_id, term_id
end
function getCmd(idx)
return
local function process_wildcards(cmd)
local wc = harpoon.get_wildcard()
if wc == nil then
return cmd
end
local cmd_parts = string.gmatch(cmd, "{}")
if #cmd_parts == 1 then
return cmd
end
local new_cmd = cmd[1]
for idx = 2, #cmd_parts do
new_cmd = new_cmd .. wc .. cmd_parts[idx]
end
return new_cmd
end
function find_terminal(idx)
local function find_terminal(idx)
local term_handle = terminals[idx]
if not term_handle or not vim.api.nvim_buf_is_valid(term_handle.buf_id) then
local buf_id, term_id = create_terminal()
@ -59,6 +72,8 @@ M.sendCommand = function(idx, cmd)
cmd = harpoon.get_term_config().cmds[cmd]
end
cmd = process_wildcards(cmd)
if cmd then
vim.fn.chansend(term_handle.term_id, cmd)
end