Skip to content

Commit

Permalink
checkpoint: i must sleep with my beautiful wife, but f... its borked
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePrimeagen committed Nov 27, 2023
1 parent 2fdac4f commit 5d7ee0d
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 221 deletions.
121 changes: 121 additions & 0 deletions lua/harpoon2/buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
local utils = require("harpoon2.utils")
local M = {}

local HARPOON_MENU = "__harpoon-menu__"

-- simple reason here is that if we are deving harpoon, we will create several
-- ui objects, each with their own buffer, which will cause the name to be duplicated and then we will get a vim error on nvim_buf_set_name
local harpoon_menu_id = 0

local function get_harpoon_menu_name()
harpoon_menu_id = harpoon_menu_id + 1
return HARPOON_MENU .. harpoon_menu_id
end

---TODO: I don't know how to do what i want to do, but i want to be able to
---make this so we use callbacks for these buffer actions instead of using
---strings back into the ui. it feels gross and it puts odd coupling
---@param bufnr number
function M.setup_autocmds_and_keymaps(bufnr)
--[[
-- TODO: Do the highlighting better
local curr_file = vim.api.nvim_buf_get_name(0)
local cmd =
string.format(
"autocmd Filetype harpoon "
.. "let path = '%s' | call clearmatches() | "
-- move the cursor to the line containing the current filename
.. "call search('\\V'.path.'\\$') | "
-- add a hl group to that line
.. "call matchadd('HarpoonCurrentFile', '\\V'.path.'\\$')",
curr_file:gsub("\\", "\\\\")
)
print(cmd)
vim.cmd(cmd)
--]]

if vim.api.nvim_buf_get_name(bufnr) == "" then
vim.api.nvim_buf_set_name(bufnr, get_harpoon_menu_name())
end

vim.api.nvim_buf_set_option(bufnr, "filetype", "harpoon")
vim.api.nvim_buf_set_option(bufnr, "buftype", "acwrite")
vim.api.nvim_buf_set_option(bufnr, "bufhidden", "delete")

--[[
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"z",
"<Cmd>lua print('WTF')<CR>",
{ silent = true }
)
--]]

vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"q",
"<Cmd>lua require('harpoon2').ui:toggle_quick_menu()<CR>",
{ silent = true }
)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"<ESC>",
"<Cmd>lua require('harpoon2').ui:toggle_quick_menu()<CR>",
{ silent = true }
)
vim.api.nvim_buf_set_keymap(
bufnr,
"n",
"<CR>",
"<Cmd>lua require('harpoon2').ui:select_menu_item()<CR>",
{}
)
-- TODO: Update these to use the new autocmd api
vim.cmd(
string.format(
"autocmd BufWriteCmd <buffer=%s> lua require('harpoon2').ui:on_menu_save()",
bufnr
)
)
-- TODO: Do we want this? is this a thing?
-- its odd... why save on text change? shouldn't we wait until close / w / esc?
--[[
if global_config.save_on_change then
vim.cmd(
string.format(
"autocmd TextChanged,TextChangedI <buffer=%s> lua require('harpoon2').ui:on_menu_save()",
bufnr
)
)
end
--]]
vim.cmd(
string.format(
"autocmd BufModifiedSet <buffer=%s> set nomodified",
bufnr
)
)
vim.cmd(
"autocmd BufLeave <buffer> ++nested ++once silent lua require('harpoon2').ui:toggle_quick_menu()"
)

end

---@param bufnr number
function M.get_contents(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, true)
local indices = {}

for _, line in pairs(lines) do
if not utils.is_white_space(line) then
table.insert(indices, line)
end
end

return indices
end

return M
20 changes: 19 additions & 1 deletion lua/harpoon2/config.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local utils = require("harpoon2.utils")
local M = {}

---@alias HarpoonListItem {value: any, context: any}
Expand All @@ -12,6 +13,12 @@ local M = {}
---@field add? fun(item: any?): HarpoonListItem
---@field BufLeave? fun(evt: any, list: HarpoonList): nil
---@field VimLeavePre? fun(evt: any, list: HarpoonList): nil
---@field get_root_dir? fun(): string

---@class HarpoonWindowSettings
---@field width number
---@field height number


---notehunthoeunthoeunthoeunthoeunthoeunth
---@class HarpoonSettings
Expand Down Expand Up @@ -108,10 +115,21 @@ function M.get_default_config()
return list_item_a.value == list_item_b.value
end,

get_root_dir = function()
return vim.loop.cwd()
end,

---@param name any
---@return HarpoonListItem
add = function(name)
name = name or vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())
name = name or
-- TODO: should we do path normalization???
-- i know i have seen sometimes it becoming an absolute
-- path, if that is the case we can use the context to
-- store the bufname and then have value be the normalized
-- value
vim.api.nvim_buf_get_name(vim.api.nvim_get_current_buf())

local bufnr = vim.fn.bufnr(name, false)

local pos = {1, 0}
Expand Down
24 changes: 22 additions & 2 deletions lua/harpoon2/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local Ui = require("harpoon2.ui")
local Data = require("harpoon2.data")
local Config = require("harpoon2.config")
local List = require("harpoon2.list")
Expand All @@ -12,6 +13,7 @@ local DEFAULT_LIST = "__harpoon_files"

---@class Harpoon
---@field config HarpoonConfig
---@field ui HarpoonUI
---@field data HarpoonData
---@field lists {[string]: {[string]: HarpoonList}}
---@field hooks_setup boolean
Expand All @@ -23,18 +25,24 @@ Harpoon.__index = Harpoon
function Harpoon:new()
local config = Config.get_default_config()

return setmetatable({
local harpoon = setmetatable({
config = config,
data = Data.Data:new(),
ui = Ui:new(config.settings),
lists = {},
hooks_setup = false,
}, self)

return harpoon
end

---@param partial_config HarpoonPartialConfig
---@return Harpoon
function Harpoon:setup(partial_config)
self.config = Config.merge_config(partial_config, self.config)
self.ui:configure(self.config.settings)

---TODO: should we go through every seen list and update its config?

if self.hooks_setup == false then
local augroup = vim.api.nvim_create_augroup
Expand All @@ -44,6 +52,7 @@ function Harpoon:setup(partial_config)
group = HarpoonGroup,
pattern = '*',
callback = function(ev)
--[[
self:_for_each_list(function(list, config)
local fn = config[ev.event]
Expand All @@ -55,6 +64,7 @@ function Harpoon:setup(partial_config)
self:sync()
end
end)
--]]
end,
})

Expand Down Expand Up @@ -129,6 +139,16 @@ function Harpoon:dump()
return self.data._data
end

return Harpoon:new()
function Harpoon:__debug_reset()
require("plenary.reload").reload_module("harpoon2")
end

local harpoon = Harpoon:new()
HARPOON_DEBUG_VAR = HARPOON_DEBUG_VAR or 0
if HARPOON_DEBUG_VAR == 0 then
harpoon.ui:toggle_quick_menu(harpoon:list())
HARPOON_DEBUG_VAR = 1
end
-- leave this undone, i sometimes use this for debugging

return harpoon
25 changes: 25 additions & 0 deletions lua/harpoon2/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ end
--- @class HarpoonList
--- @field config HarpoonPartialConfigItem
--- @field name string
--- @field _index number
--- @field items HarpoonItem[]
local HarpoonList = {}

Expand All @@ -27,9 +28,15 @@ function HarpoonList:new(config, name, items)
items = items,
config = config,
name = name,
_index = 1,
}, self)
end

---@return number
function HarpoonList:length()
return #self.items
end

---@return HarpoonList
function HarpoonList:append(item)
item = item or self.config.add()
Expand Down Expand Up @@ -113,6 +120,24 @@ function HarpoonList:select(index, options)
end
end

function HarpoonList:next()
self._index = self._index + 1
if self._index > #self.items then
self._index = 1
end

self:select(self._index)
end

function HarpoonList:prev()
self._index = self._index - 1
if self._index < 1 then
self._index = #self.items
end

self:select(self._index)
end

--- @return string[]
function HarpoonList:display()
local out = {}
Expand Down
18 changes: 18 additions & 0 deletions lua/harpoon2/test/ui_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
local utils = require("harpoon2.test.utils")

local eq = assert.are.same

describe("harpoon", function()

before_each(utils.before_each)

it("open the ui without any items in the list", function()
local harpoon = require("harpoon2")
harpoon.ui:toggle_quick_menu(harpoon:list())

-- no test, just wanted it to run without error'ing
end)

end)


19 changes: 19 additions & 0 deletions lua/harpoon2/test/utils.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
local Data = require("harpoon2.data")

local M = {}

M.created_files = {}

function M.before_each()
Data.set_data_path("/tmp/harpoon2.json")
Data.__dangerously_clear_data()
require("plenary.reload").reload_module("harpoon2")
Data = require("harpoon2.data")
Data.set_data_path("/tmp/harpoon2.json")
local harpoon = require("harpoon2")
M.clean_files()

harpoon:setup({
settings = {
key = function()
return "testies"
end
}
})
end

function M.clean_files()
for _, bufnr in ipairs(M.created_files) do
vim.api.nvim_buf_delete(bufnr, {force = true})
Expand Down
Loading

0 comments on commit 5d7ee0d

Please sign in to comment.