Merge pull request #42 from ThePrimeagen/offsets

Offsets
This commit is contained in:
ThePrimeagen 2021-03-24 08:07:39 -06:00 committed by GitHub
commit a002b9cf17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 98 additions and 44 deletions

View File

@ -2,6 +2,8 @@
This is still beta. We are getting there, but its not as fast as I would like.
Therefore APIs are subject to change.
![Harpoon](harpoon.png)
# harpoon
The goal of Harpoon is to get you where you want with the fewest keystrokes.

View File

@ -1,6 +1,5 @@
### Manage A Mark 1.0
* Logo
* linexcol offset in saved file
* floating term / split term
* TODO: Fill me in, that one really important thing....
* README.md

BIN
harpoon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 MiB

View File

@ -76,7 +76,15 @@ local function ensure_correct_config(config)
local marks = proj.mark.marks
for idx = 1, #marks do
marks[idx] = utils.normalize_path(marks[idx])
local mark = marks[idx]
if type(mark) == "string" then
mark = {
filename = mark
}
marks[idx] = mark
end
marks[idx].filename = utils.normalize_path(mark.filename)
end
end

View File

@ -7,7 +7,7 @@ local function filter_empty_string(list)
local next = {}
for idx = 1, #list do
if list[idx] ~= "" then
table.insert(next, list[idx])
table.insert(next, list[idx].filename)
end
end
@ -23,7 +23,7 @@ local function get_buf_name(id)
local idx = M.get_index_of(id)
if M.valid_index(idx) then
return harpoon.get_mark_config().marks[idx]
return M.get_marked_file_name(idx)
end
--
-- not sure what to do here...
@ -31,11 +31,17 @@ local function get_buf_name(id)
return ""
end
local function mark_exists(buf_name)
local marks = harpoon.get_mark_config().marks
local function create_mark(filename)
return {
filename = filename,
row = 0,
col = 0,
}
end
local function mark_exists(buf_name)
for idx = 1, M.get_length() do
if marks[idx] == buf_name then
if M.get_marked_file_name(idx) == buf_name then
return true
end
end
@ -56,11 +62,10 @@ M.get_index_of = function(item)
return
end
local config = harpoon.get_mark_config()
if type(item) == 'string' then
local relative_item = utils.normalize_path(item)
for idx = 1, M.get_length() do
if config.marks[idx] == relative_item then
if M.get_marked_file_name(idx) == relative_item then
return idx
end
end
@ -89,8 +94,12 @@ M.status = function()
end
M.valid_index = function(idx)
local config = harpoon.get_mark_config()
return idx ~= nil and config.marks[idx] ~= nil and config.marks[idx] ~= ""
if idx == nil then
return false
end
local file_name = M.get_marked_file_name(idx)
return file_name ~= nil and file_name ~= ""
end
M.add_file = function(file_name_or_buf_id)
@ -103,16 +112,17 @@ M.add_file = function(file_name_or_buf_id)
validate_buf_name(buf_name)
local config = harpoon.get_mark_config()
for idx = 1, M.get_length() do
if config.marks[idx] == "" then
config.marks[idx] = buf_name
local filename = M.get_marked_file_name(idx)
if filename == "" then
harpoon.get_mark_config().marks[idx] = create_mark(filename)
M.remove_empty_tail()
return
end
end
table.insert(config.marks, buf_name)
table.insert(harpoon.get_mark_config().marks, create_mark(buf_name))
M.remove_empty_tail()
end
@ -120,24 +130,33 @@ M.remove_empty_tail = function()
local config = harpoon.get_mark_config()
for i = M.get_length(), 1, -1 do
if config.marks[i] ~= "" then
local filename = M.get_marked_file_name(i)
if filename ~= "" then
return
end
if config.marks[i] == "" then
if filename == "" then
table.remove(config.marks, i)
end
end
end
M.store_offset = function()
local buf_name = get_buf_name()
local idx = M.get_index_of(buf_name)
if not M.valid_index(idx) then
return
end
local ok, res = pcall(function()
local buf_name = get_buf_name()
local idx = M.get_index_of(buf_name)
if not M.valid_index(idx) then
return
end
local line = vim.api.nvim_eval("line('.')");
harpoon.get_mark_config().marks[idx].row =
vim.fn.line(".");
end)
if not ok then
-- TODO: Developer logs?
print("M.store_offset#pcall failed:", res)
end
end
M.rm_file = function(file_name_or_buf_id)
@ -148,7 +167,7 @@ M.rm_file = function(file_name_or_buf_id)
return
end
harpoon.get_mark_config().marks[idx] = ""
harpoon.get_mark_config().marks[idx] = create_mark("")
M.remove_empty_tail()
end
@ -156,8 +175,17 @@ M.clear_all = function()
harpoon.get_mark_config().marks = {}
end
M.get_marked_file = function(idx)
return harpoon.get_mark_config().marks[idx]
--- ENTERPRISE PROGRAMMING
M.get_marked_file = function(idxOrName)
if type(idxOrName) == "string" then
idxOrName = M.get_index_of(idxOrName)
end
return harpoon.get_mark_config().marks[idxOrName]
end
M.get_marked_file_name = function(idx)
local mark = harpoon.get_mark_config().marks[idx]
return mark and mark.filename
end
M.get_length = function()
@ -174,11 +202,11 @@ M.set_current_at = function(idx)
config.marks[current_idx] = ""
end
config.marks[idx] = buf_name
config.marks[idx] = create_mark(buf_name)
for i = 1, M.get_length() do
if not config.marks[i] then
config.marks[i] = ""
config.marks[i] = create_mark("")
end
end
end
@ -188,17 +216,32 @@ M.to_quickfix_list = function()
local file_list = filter_empty_string(config.marks)
local qf_list = {}
for idx = 1, #file_list do
local mark = M.get_marked_file(idx)
qf_list[idx] = {
text = string.format("%d: %s", idx, file_list[idx]),
filename = file_list[idx],
filename = mark.filename,
row = mark.row,
col = mark.col,
}
end
vim.fn.setqflist(qf_list)
end
M.set_mark_list = function(new_list)
local config = harpoon.get_mark_config()
for k, v in pairs(new_list) do
if type(v) == "string" then
local mark = M.get_marked_file(v)
if not mark then
mark = create_mark(v)
end
new_list[k] = mark
end
end
config.marks = new_list
end

View File

@ -4,7 +4,7 @@ local Path = require("plenary.path")
local M = {}
local terminals = {}
function create_terminal()
local function create_terminal()
local current_id = vim.fn.bufnr()
vim.cmd(":terminal")

View File

@ -9,7 +9,7 @@ local M = {}
win_id = nil
bufh = nil
function create_window()
local function create_window()
local win_info = float.percentage_range_window(
factorw,
factorh,
@ -20,7 +20,7 @@ function create_window()
return win_info
end
function get_menu_items()
local function get_menu_items()
local lines = vim.api.nvim_buf_get_lines(bufh, 0, -1, true)
local indices = {}
@ -56,7 +56,7 @@ M.toggle_quick_menu = function()
bufh = win_info.bufnr
for idx = 1, Marked.get_length() do
local file = Marked.get_marked_file(idx)
local file = Marked.get_marked_file_name(idx)
if file == "" then
file = "(empty)"
end
@ -77,21 +77,18 @@ M.on_menu_save = function()
end
M.nav_file = function(id)
idx = Marked.get_index_of(id)
local idx = Marked.get_index_of(id)
if not Marked.valid_index(idx) then
return
end
local buf_id = vim.fn.bufnr(Marked.get_marked_file(idx))
local mark = Marked.get_marked_file(idx)
local buf_id = vim.fn.bufnr(mark.filename)
local set_row = not vim.api.nvim_buf_is_loaded(buf_id)
if vim.api.nvim_win_is_valid(buf_id) then
vim.api.nvim_win_close(win_id)
end
if buf_id ~= nil and buf_id ~= -1 then
vim.api.nvim_set_current_buf(buf_id)
else
vim.cmd(string.format("e %s", Marked.get_marked_file(idx)))
vim.api.nvim_set_current_buf(buf_id)
if set_row and mark.row then
vim.cmd(string.format(":%d", mark.row))
end
end

View File

@ -1,5 +1,10 @@
fun! HarpoonLeave()
lua require('harpoon.mark').store_offset()
lua require('harpoon').save()
endfun
augroup THE_PRIMEAGEN_HARPOON
autocmd!
autocmd VimLeavePre * :lua require('harpoon').save()
autocmd VimLeavePre * :call HarpoonLeave()
autocmd BufLeave * :lua require('harpoon.mark').store_offset()
augroup END