feat: moving things around so that i can start emit out more data

This commit is contained in:
mpaulson 2023-12-10 16:02:39 -07:00
parent aa071ce65b
commit e89299bc1a
6 changed files with 58 additions and 53 deletions

View File

@ -177,8 +177,8 @@ Settings can alter the experience of harpoon
``` ```
**Descriptions** **Descriptions**
* `save_on_toggle`: any time the ui menu is closed then we will sync the state back to the backing list * `save_on_toggle`: any time the ui menu is closed then we will save the state back to the backing list, not to the fs
* `border_chars`: the ui's border characters to be displayed * `sync_on_ui_close`: any time the ui menu is closed then the state of the list will be sync'd back to the fs
* `key` how the out list key is looked up. This can be useful when using worktrees and using git remote instead of file path * `key` how the out list key is looked up. This can be useful when using worktrees and using git remote instead of file path
**Defaults** **Defaults**
@ -186,7 +186,6 @@ Settings can alter the experience of harpoon
settings = { settings = {
save_on_toggle = false, save_on_toggle = false,
sync_on_ui_close = false, sync_on_ui_close = false,
border_chars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
key = function() key = function()
return vim.loop.cwd() return vim.loop.cwd()
end, end,
@ -194,8 +193,7 @@ settings = {
``` ```
### Highlight Groups ### Highlight Groups
Currently available highlight groups are TODO: Fill in the idea that we will emit out window information
`HarpoonWindow`, `HarpoonBorder`, and `HarpoonTitle`.
### Logger ### Logger
This can help debug issues on other's computer. To get your debug log please do the following. This can help debug issues on other's computer. To get your debug log please do the following.

View File

@ -27,9 +27,6 @@ function M.run_toggle_command(key)
harpoon.ui:toggle_quick_menu() harpoon.ui:toggle_quick_menu()
end 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 ---@param bufnr number
function M.setup_autocmds_and_keymaps(bufnr) function M.setup_autocmds_and_keymaps(bufnr)
local curr_file = vim.api.nvim_buf_get_name(0) local curr_file = vim.api.nvim_buf_get_name(0)

View File

@ -25,7 +25,6 @@ M.DEFAULT_LIST = DEFAULT_LIST
---@field get_root_dir? fun(): string ---@field get_root_dir? fun(): string
---@class HarpoonSettings ---@class HarpoonSettings
---@field border_chars string[] defaults to { "─", "│", "─", "│", "╭", "╮", "╯", "╰" }
---@field save_on_toggle boolean defaults to true ---@field save_on_toggle boolean defaults to true
---@field sync_on_ui_close? boolean ---@field sync_on_ui_close? boolean
---@field ui_fallback_width number defaults 69, nice ---@field ui_fallback_width number defaults 69, nice
@ -59,16 +58,6 @@ function M.get_default_config()
settings = { settings = {
save_on_toggle = false, save_on_toggle = false,
sync_on_ui_close = false, sync_on_ui_close = false,
border_chars = {
"",
"",
"",
"",
"",
"",
"",
"",
},
ui_fallback_width = 69, ui_fallback_width = 69,
ui_width_ratio = 0.62569, ui_width_ratio = 0.62569,
key = function() key = function()

View File

@ -1,6 +1,9 @@
local Logger = require("harpoon.logger") local Logger = require("harpoon.logger")
local Listeners = require("harpoon.listeners") local Listeners = require("harpoon.listeners")
--- @class HarpoonNavOptions
--- @field ui_nav_wrap? boolean
local function index_of(items, element, config) local function index_of(items, element, config)
local equals = config and config.equals local equals = config and config.equals
or function(a, b) or function(a, b)
@ -180,23 +183,35 @@ function HarpoonList:select(index, options)
end end
end end
function HarpoonList:next() ---
--- @param opts? HarpoonNavOptions
function HarpoonList:next(opts)
opts = opts or {}
self._index = self._index + 1 self._index = self._index + 1
if self._index > #self.items and self.config.ui_nav_wrap then if self._index > #self.items then
self._index = 1 if opts.ui_nav_wrap then
elseif self._index > #self.items and not self.config.ui_nav_wrap then self._index = 1
self._index = #self.items else
self._index = #self.items
end
end end
self:select(self._index) self:select(self._index)
end end
function HarpoonList:prev() ---
--- @param opts? HarpoonNavOptions
function HarpoonList:prev(opts)
opts = opts or {}
self._index = self._index - 1 self._index = self._index - 1
if self._index < 1 and self.config.ui_nav_wrap then if self._index < 1 then
self._index = #self.items if opts.ui_nav_wrap then
elseif self._index < 1 and not self.config.ui_nav_wrap then self._index = #self.items
self._index = 1 else
self._index = 1
end
end end
self:select(self._index) self:select(self._index)

View File

@ -1,5 +1,7 @@
---@alias HarpoonListener fun(type: string, args: any[] | any | nil): nil ---@alias HarpoonListener fun(type: string, args: any[] | any | nil): nil
--- TODO: Rename this... its an odd name "listeners"
---@class HarpoonListeners ---@class HarpoonListeners
---@field listeners (HarpoonListener)[] ---@field listeners (HarpoonListener)[]
---@field listenersByType (table<string, HarpoonListener>)[] ---@field listenersByType (table<string, HarpoonListener>)[]
@ -53,5 +55,6 @@ return {
SELECT = "SELECT", SELECT = "SELECT",
REMOVE = "REMOVE", REMOVE = "REMOVE",
REORDER = "REORDER", REORDER = "REORDER",
UI_CREATE = "UI_CREATE",
}, },
} }

View File

@ -1,10 +1,12 @@
local popup = require("plenary").popup
local Buffer = require("harpoon.buffer") local Buffer = require("harpoon.buffer")
local Logger = require("harpoon.logger") local Logger = require("harpoon.logger")
local Listeners = require("harpoon.listeners")
---@class HarpoonToggleOptions
---TODO: Finish.
---@class HarpoonUI ---@class HarpoonUI
---@field win_id number ---@field win_id number
---@field border_win_id number
---@field bufnr number ---@field bufnr number
---@field settings HarpoonSettings ---@field settings HarpoonSettings
---@field active_list HarpoonList ---@field active_list HarpoonList
@ -23,7 +25,6 @@ HarpoonUI.__index = HarpoonUI
function HarpoonUI:new(settings) function HarpoonUI:new(settings)
return setmetatable({ return setmetatable({
win_id = nil, win_id = nil,
border_win_id = nil,
bufnr = nil, bufnr = nil,
active_list = nil, active_list = nil,
settings = settings, settings = settings,
@ -54,23 +55,18 @@ function HarpoonUI:close_menu()
vim.api.nvim_win_close(self.win_id, true) vim.api.nvim_win_close(self.win_id, true)
end end
if
self.border_win_id ~= nil
and vim.api.nvim_win_is_valid(self.border_win_id)
then
vim.api.nvim_win_close(self.border_win_id, true)
end
self.active_list = nil self.active_list = nil
self.win_id = nil self.win_id = nil
self.border_win_id = nil
self.bufnr = nil self.bufnr = nil
self.closing = false self.closing = false
end end
--- TODO: Toggle_opts should be where we get extra style and border options
--- and we should create a nice minimum window
---@param toggle_opts HarpoonToggleOptions
---@return number,number ---@return number,number
function HarpoonUI:_create_window() function HarpoonUI:_create_window(toggle_opts)
local win = vim.api.nvim_list_uis() local win = vim.api.nvim_list_uis()
local width = self.settings.ui_fallback_width local width = self.settings.ui_fallback_width
@ -81,33 +77,40 @@ function HarpoonUI:_create_window()
end end
local height = 8 local height = 8
local borderchars = self.settings.border_chars
local bufnr = vim.api.nvim_create_buf(false, true) local bufnr = vim.api.nvim_create_buf(false, true)
-- TODO: Remove popup and just use nvim_open_win local win_id = vim.api.nvim_open_win(bufnr, true, {
local _, popup_info = popup.create(bufnr, { relative = "editor",
title = "Harpoon", title = "Harpoon",
highlight = "HarpoonWindow", row = math.floor(((vim.o.lines - height) / 2) - 1),
borderhighlight = "HarpoonBorder",
titlehighlight = "HarpoonTitle",
line = math.floor(((vim.o.lines - height) / 2) - 1),
col = math.floor((vim.o.columns - width) / 2), col = math.floor((vim.o.columns - width) / 2),
minwidth = width, width = width,
minheight = height, height = height,
borderchars = borderchars, style = "minimal",
border = "single",
}) })
local win_id = popup_info.win_id
if win_id == 0 then
Logger:log("ui#_create_window failed to create window, win_id returned 0")
error("Failed to create window")
end
Buffer.setup_autocmds_and_keymaps(bufnr) Buffer.setup_autocmds_and_keymaps(bufnr)
self.win_id = win_id self.win_id = win_id
self.border_win_id = popup_info.border.win_id
vim.api.nvim_win_set_option(win_id, "number", true) vim.api.nvim_win_set_option(win_id, "number", true)
Listeners.listeners:emit(Listeners.event_names.UI_CREATE, {
win_id = win_id,
bufnr = bufnr,
})
return win_id, bufnr return win_id, bufnr
end end
---@param list? HarpoonList ---@param list? HarpoonList
---TODO: @param opts? HarpoonToggleOptions
function HarpoonUI:toggle_quick_menu(list) function HarpoonUI:toggle_quick_menu(list)
opts = opts or {}
if list == nil or self.win_id ~= nil then if list == nil or self.win_id ~= nil then
Logger:log("ui#toggle_quick_menu#closing", list and list.name) Logger:log("ui#toggle_quick_menu#closing", list and list.name)
if self.settings.save_on_toggle then if self.settings.save_on_toggle then