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**
* `save_on_toggle`: any time the ui menu is closed then we will sync the state back to the backing list
* `border_chars`: the ui's border characters to be displayed
* `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
* `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
**Defaults**
@ -186,7 +186,6 @@ Settings can alter the experience of harpoon
settings = {
save_on_toggle = false,
sync_on_ui_close = false,
border_chars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" },
key = function()
return vim.loop.cwd()
end,
@ -194,8 +193,7 @@ settings = {
```
### Highlight Groups
Currently available highlight groups are
`HarpoonWindow`, `HarpoonBorder`, and `HarpoonTitle`.
TODO: Fill in the idea that we will emit out window information
### Logger
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()
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)
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
---@class HarpoonSettings
---@field border_chars string[] defaults to { "─", "│", "─", "│", "╭", "╮", "╯", "╰" }
---@field save_on_toggle boolean defaults to true
---@field sync_on_ui_close? boolean
---@field ui_fallback_width number defaults 69, nice
@ -59,16 +58,6 @@ function M.get_default_config()
settings = {
save_on_toggle = false,
sync_on_ui_close = false,
border_chars = {
"",
"",
"",
"",
"",
"",
"",
"",
},
ui_fallback_width = 69,
ui_width_ratio = 0.62569,
key = function()

View File

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

View File

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

View File

@ -1,10 +1,12 @@
local popup = require("plenary").popup
local Buffer = require("harpoon.buffer")
local Logger = require("harpoon.logger")
local Listeners = require("harpoon.listeners")
---@class HarpoonToggleOptions
---TODO: Finish.
---@class HarpoonUI
---@field win_id number
---@field border_win_id number
---@field bufnr number
---@field settings HarpoonSettings
---@field active_list HarpoonList
@ -23,7 +25,6 @@ HarpoonUI.__index = HarpoonUI
function HarpoonUI:new(settings)
return setmetatable({
win_id = nil,
border_win_id = nil,
bufnr = nil,
active_list = nil,
settings = settings,
@ -54,23 +55,18 @@ function HarpoonUI:close_menu()
vim.api.nvim_win_close(self.win_id, true)
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.win_id = nil
self.border_win_id = nil
self.bufnr = nil
self.closing = false
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
function HarpoonUI:_create_window()
function HarpoonUI:_create_window(toggle_opts)
local win = vim.api.nvim_list_uis()
local width = self.settings.ui_fallback_width
@ -81,33 +77,40 @@ function HarpoonUI:_create_window()
end
local height = 8
local borderchars = self.settings.border_chars
local bufnr = vim.api.nvim_create_buf(false, true)
-- TODO: Remove popup and just use nvim_open_win
local _, popup_info = popup.create(bufnr, {
local win_id = vim.api.nvim_open_win(bufnr, true, {
relative = "editor",
title = "Harpoon",
highlight = "HarpoonWindow",
borderhighlight = "HarpoonBorder",
titlehighlight = "HarpoonTitle",
line = math.floor(((vim.o.lines - height) / 2) - 1),
row = math.floor(((vim.o.lines - height) / 2) - 1),
col = math.floor((vim.o.columns - width) / 2),
minwidth = width,
minheight = height,
borderchars = borderchars,
width = width,
height = height,
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)
self.win_id = win_id
self.border_win_id = popup_info.border.win_id
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
end
---@param list? HarpoonList
---TODO: @param opts? HarpoonToggleOptions
function HarpoonUI:toggle_quick_menu(list)
opts = opts or {}
if list == nil or self.win_id ~= nil then
Logger:log("ui#toggle_quick_menu#closing", list and list.name)
if self.settings.save_on_toggle then