diff --git a/README.md b/README.md index e70b78b..d601c7a 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,15 @@ # harpoon -Harpoon is a terminal navigator for Neovim (currently, but I want vim to work -as well). Harpoon itself does not provide much for functionality other than -goto a terminal, set a terminal, and sending commands. Where it shines is in -local configs. +The goal of Harpoon is to get you where you want with the fewest keystrokes. + +## The Problem +You work on code. The code base is medium, large, tiny, whatever. You find +yourself frequenting a small set of files (maybe it depends on task) and you +are tired of using a fuzzy finder, :bnext/prev, alternate file doesn't quite +cut it, etc etc. + +## The Solution +The ability to specify, or on the fly, mark and create persisting key strokes +to go to the files you want. This includes terminal navigation. ## Installation Simply install via your favorite plugin manager. diff --git a/TODO.md b/TODO.md index c2125df..8ef50a4 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,7 @@ ### Manage A Mark 1.0 * Logo * linexcol offset in saved file +* (bug) When new files are added, they do start off as absolute paths (their expand("%")) ### Harpoon (upon requests) * Add hooks for vim so that someone can make it for me diff --git a/autoload/harpoon.vim b/autoload/harpoon.vim deleted file mode 100644 index 82ae350..0000000 --- a/autoload/harpoon.vim +++ /dev/null @@ -1,7 +0,0 @@ -fun harpoon#nav(id) - call luaeval("require('harpoon.ui').nav(_A[1])", [a:id]) -endfun - -fun harpoon#cmd(cmd) - " TODO: I am sure I'll use this -endfun diff --git a/lua/harpoon/init.lua b/lua/harpoon/init.lua new file mode 100644 index 0000000..69c6379 --- /dev/null +++ b/lua/harpoon/init.lua @@ -0,0 +1,29 @@ +local terminals = require("harpoon.terminal") +local manage = require("harpoon.manage-a-mark") +local cwd = cwd or vim.loop.cwd() +local config_path = vim.fn.stdpath("config") +local terminal_config = string.format("%s/harpoon-terminal.json", config_path) + +local M = {} + +M.setup = function(config) + function read_terminal_config() + return vim.fn.json_decode(Path:new(terminal_config):read()) + end + + -- TODO: Merge the configs instead of falling back + if not config then + local ok, res = pcall(read_terminal_config) + if ok then + config = res + else + config = {} + end + end + + terminals.setup(config) + manage.setup(config) +end + +return M + diff --git a/lua/harpoon/manage-a-mark.lua b/lua/harpoon/manage-a-mark.lua index 9244f6d..71dd571 100644 --- a/lua/harpoon/manage-a-mark.lua +++ b/lua/harpoon/manage-a-mark.lua @@ -185,5 +185,8 @@ M.get_length = function() return #marked_files end +M.setup = function() +end + return M diff --git a/lua/harpoon/terminal.lua b/lua/harpoon/terminal.lua new file mode 100644 index 0000000..dddab93 --- /dev/null +++ b/lua/harpoon/terminal.lua @@ -0,0 +1,87 @@ +local Path = require("plenary.path") +local cwd = cwd or vim.loop.cwd() + +local M = {} + +harpoon_terminal_config = { } +harpoon_terminals = {} +harpoon_init = false + +function create_terminal() + local current_id = vim.fn.bufnr() + + vim.cmd(":terminal") + local buf_id = vim.fn.bufnr() + local term_id = vim.b.terminal_job_id + + if term_id == nil then + -- TODO: Throw an erro? + return nil + end + + -- Resets the buffer back to the old one + vim.api.nvim_set_current_buf(current_id) + return buf_id, term_id +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) + local commandSet = harpoon_terminal_config[cwd] + if not commandSet then + return nil + end + return commandSet[idx] +end + +M.setup = function(config) + harpoon_terminal_config = config.terminal or {} +end + +M.gotoTerminal = function(idx) + local term_handle = harpoon_terminals[idx] + + if not term_handle then + local buf_id, term_id = create_terminal() + if buf_id == nil then + return + end + term_handle = { + buf_id = buf_id, + term_id = term_id + } + harpoon_terminals[idx] = term_handle + end + + vim.api.nvim_set_current_buf(term_handle.buf_id) +end + +M.sendCommand = function(idx, cmd) + local term_handle = harpoon_terminals[idx] + + if not term_handle then + M.gotoTerminal(idx) + term_handle = harpoon_terminals[idx] + end + + if type(cmd) == "number" then + cmd = getCmd(cmd) + end + + if cmd then + vim.fn.chansend(term_handle.term_id, cmd) + end +end + +return M diff --git a/lua/harpoon/utils.lua b/lua/harpoon/utils.lua new file mode 100644 index 0000000..74e9650 --- /dev/null +++ b/lua/harpoon/utils.lua @@ -0,0 +1,8 @@ +local cwd = cwd or vim.loop.cwd() +local data_path = vim.fn.stdpath("data") + +local M = { + cwd = cwd, + data_path, +} + diff --git a/plugin/manage-a-mark.vim b/plugin/manage-a-mark.vim index 796f1fc..8a51dde 100644 --- a/plugin/manage-a-mark.vim +++ b/plugin/manage-a-mark.vim @@ -1,5 +1,3 @@ -com! -nargs=1 Harpoon call harpoon#cmd() - augroup THE_PRIMEAGEN_HARPOON autocmd! autocmd VimLeave * :lua require('harpoon.manage-a-mark').save()