Skip to content

Commit

Permalink
a few optimizations and new features
Browse files Browse the repository at this point in the history
  • Loading branch information
VoxelPrismatic committed Jun 19, 2024
1 parent 5d4770d commit d088ff3
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 69 deletions.
19 changes: 0 additions & 19 deletions PLUGINS.md

This file was deleted.

17 changes: 16 additions & 1 deletion lua/rabbit/autocmd.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
local M = {
attached = {
"BufEnter",
"BufDelete",
"BufUnload",
"WinClosed",
"WinResized",
"WinEnter",
"RabbitEnter",
}
}


---@param rabbit Rabbit.Instance
return function(rabbit)
function M.attach(rabbit)
vim.api.nvim_create_autocmd("BufEnter", {
pattern = {"*"},
callback = rabbit.autocmd
Expand Down Expand Up @@ -61,3 +74,5 @@ return function(rabbit)
})
end


return M
97 changes: 57 additions & 40 deletions lua/rabbit/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ local screen = require("rabbit.screen")
local defaults = require("rabbit.defaults")
local set = require("rabbit.plugins.util")
local compat = require("rabbit.compat")
local autocmd = require("rabbit.autocmd")

-- Somehow Neovim 0.9.5 over alacritty doesn't have vim.uv
vim.uv = vim.uv or vim.loop

-- Replaces terminal codes and keycodes (<CR>, <Esc>, ...) in a string with
-- the internal representation.
Expand Down Expand Up @@ -36,7 +40,7 @@ local rabbit = {
default = "",
plugins = {},

compat = require("rabbit.compat"),
compat = compat,
}

-- Display a message in the buffer
Expand Down Expand Up @@ -193,42 +197,43 @@ function rabbit.ensure_listing(winid)

for k, _ in pairs(rabbit.plugins) do
local p = rabbit.plugins[k] ---@type Rabbit.Plugin

local handler = (p.evt.BufEnter or function(_, _) end)

if p.listing == nil then
p.listing = { [winid] = {} }
p.evt.BufEnter(evt, winid)
handler(evt, winid)
elseif p.listing[winid] == nil then
p.listing[winid] = {}
p.evt.BufEnter(evt, winid)
handler(evt, winid)
end
end
end


-- Highlight manager
function rabbit.BufHighlight()
vim.api.nvim_buf_clear_namespace(rabbit.rabbit.buf, rabbit.rabbit.ns, 0, -1)
local len = #rabbit.ctx.listing
local line = vim.fn.line(".") - 1
local e = rabbit.ctx.listing[math.max(1, line - 2)]

if type(e) == "number" then
local valid = vim.api.nvim_buf_is_valid(e or 0)
if not valid then
return rabbit.Redraw()
end
if type(e) == "number" and not vim.api.nvim_buf_is_valid(e) then
return rabbit.Redraw()
end

if line - 1 > 0 and line - 1 <= len then
local fullscreen = rabbit.rabbit.win == rabbit.user.win
local offset = fullscreen and 0 or #(rabbit.opts.window.box.vertical)
vim.api.nvim_buf_add_highlight(
rabbit.rabbit.buf, rabbit.rabbit.ns, "CursorLine",
line, offset, #(vim.api.nvim_get_current_line()) - offset
)
if line - 1 <= 0 or line - 1 > len then
return
end

local fullscreen = rabbit.rabbit.win == rabbit.user.win
local offset = fullscreen and 0 or #(rabbit.opts.window.box.vertical)
vim.api.nvim_buf_add_highlight(
rabbit.rabbit.buf, rabbit.rabbit.ns, "CursorLine",
line, offset, #(vim.api.nvim_get_current_line()) - offset
)
end


-- Creates the buffer for the given mode
---@param mode string
---@return Rabbit.Context.Buffer
Expand Down Expand Up @@ -331,11 +336,14 @@ function rabbit.MakeBuf(mode)
mode = b_mode,
}

local fs = screen.set_border(rabbit.rabbit.win, buf, b_kwargs)

return { ---@type Rabbit.Context.Buffer
nr = buf,
w = opts.width,
h = opts.height,
fs = screen.set_border(rabbit.rabbit.win, buf, b_kwargs)
fs = fs,
pos = fs and 0 or #(b_kwargs.box.vertical),
}
end

Expand All @@ -353,7 +361,7 @@ function rabbit.Window(mode)
rabbit.ctx.buffer = rabbit.MakeBuf(mode)
screen.draw_top()
if rabbit.Redraw() then
vim.api.nvim_win_set_cursor(rabbit.rabbit.win, { 3, rabbit.ctx.buffer.fs and 0 or #(rabbit.opts.window.box.vertical) })
vim.api.nvim_win_set_cursor(rabbit.rabbit.win, { 3, rabbit.ctx.buffer.pos })
end
end

Expand All @@ -369,11 +377,15 @@ function rabbit.Redraw()
end

-- Copy listing; remove first entry if needed
rabbit.ctx.listing = vim.deepcopy(rabbit.ctx.plugin.listing[0] or rabbit.ctx.plugin.listing[rabbit.user.win])
rabbit.ctx.listing = vim.deepcopy(
rabbit.ctx.plugin.listing[0] or
rabbit.ctx.plugin.listing[rabbit.user.win]
)

if #rabbit.ctx.listing > 0 and rabbit.ctx.plugin.skip_same then
local first_entry = rabbit.ctx.listing[1]
local name = vim.api.nvim_buf_get_name(rabbit.user.buf)
if rabbit.ctx.listing[1] == rabbit.user.buf or rabbit.ctx.listing[1] == name then
if first_entry == rabbit.user.buf or first_entry == name then
table.remove(rabbit.ctx.listing, 1)
end
end
Expand All @@ -383,8 +395,6 @@ function rabbit.Redraw()
buf_path = vim.fn.getcwd() .. "/rabbit.txt" -- Relative to CWD if no name set
end

screen.draw_bottom(2)

local i = 1
while i <= #rabbit.ctx.listing do
local target = ""
Expand All @@ -403,7 +413,7 @@ function rabbit.Redraw()
goto continue
end
else
break
goto skip
end

if target == "" then
Expand All @@ -430,6 +440,7 @@ function rabbit.Redraw()
}, i + 1)
end

::skip::
i = i + 1
::continue::
end
Expand All @@ -439,7 +450,7 @@ function rabbit.Redraw()
return false
end

rabbit.Legend(mode, math.max(i + 2, buf.h))
rabbit.Legend(mode, screen.draw_bottom(i + 1))
return true
end

Expand All @@ -454,21 +465,16 @@ function rabbit.Legend(mode, line)

mode = mode or rabbit.ctx.plugin.name

vim.api.nvim_buf_set_keymap(rabbit.rabbit.buf, "n", "i", "", {
callback = function() vim.fn.feed_termcodes("<esc>i", "t") end
})

vim.api.nvim_buf_set_keymap(rabbit.rabbit.buf, "n", "I", "", {
callback = function() vim.fn.feed_termcodes("<esc>I", "t") end
})

vim.api.nvim_buf_set_keymap(rabbit.rabbit.buf, "n", "A", "", {
callback = function() vim.fn.feed_termcodes("<esc>A", "t") end
})

vim.api.nvim_buf_set_keymap(rabbit.rabbit.buf, "n", "a", "", {
callback = function() vim.fn.feed_termcodes("<esc>a", "t") end
})
local keys = "iIaArR"
for key in string.gmatch(keys, ".") do
vim.api.nvim_buf_set_keymap(rabbit.rabbit.buf, "n", key, "", {
noremap = true, silent = true,
callback = function()
(rabbit.ctx.plugin.func.close or rabbit.func.close)()
vim.fn.feedkeys(key, "t")
end
})
end

line = screen.newline({
{ color = "RabbitTitle", text = " Switch:" },
Expand Down Expand Up @@ -619,9 +625,20 @@ function rabbit.attach(plugin)
end
rabbit.plugins[plugin.name] = plugin
plugin.init(plugin)

for evt, _ in pairs(plugin.evt) do
if not set.index(autocmd.attached, evt) then
table.insert(autocmd.attached, evt)
vim.api.nvim_create_autocmd(evt, {
pattern = {"*"},
callback = rabbit.autocmd
})
end
end
end


---@param name string Plugin name
function rabbit.make_mem(name)
local parts = vim.split(debug.getinfo(1).source:sub(2), rabbit.compat.path)
table.remove(parts, #parts)
Expand All @@ -640,5 +657,5 @@ function rabbit.make_mem(name)
return vim.fn.fnamemodify(file, ":p")
end

require("rabbit.autocmd")(rabbit)
autocmd.attach(rabbit)
return rabbit
1 change: 1 addition & 0 deletions lua/rabbit/luadoc/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
---@field w integer Window width
---@field h integer Window height
---@field fs boolean | Rabbit.Screen.Spec Fullscreen
---@field pos integer Cursor position; 0 if fullscreen; length of vertical wall otherwise


---@class Rabbit.Plugin_Table
Expand Down
6 changes: 3 additions & 3 deletions lua/rabbit/plugins/VERSION.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"history": "v2",
"reopen": "v2",
"history": "v3",
"reopen": "v2a",
"oxide": "v3",
"harpoon": "v1.1"
"harpoon": "v1a"
}
16 changes: 13 additions & 3 deletions lua/rabbit/plugins/history.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ local M = { ---@type Rabbit.Plugin
---@param p Rabbit.Plugin.History
init = function(p)
p.listing.last_closed = {}
p.listing.opened = {}
end,

---@type Rabbit.Plugin.History.Options
Expand All @@ -36,23 +37,32 @@ function M.evt.BufEnter(evt, winid)
return
end
set.add(M.listing[winid], evt.buf)
set.add(M.listing.opened, evt.buf)
end


---@param evt NvimEvent
---@param winid integer
function M.evt.BufDelete(evt, winid)
set.sub(M.listing[winid], evt.buf)
set.sub(M.listing.opened, evt.buf)
end


---@param winid integer
function M.evt.RabbitEnter(winid)
if #M.listing[winid] <= 1 and #M.listing.last_closed > 0 then
M.listing[0] = nil
if #M.listing[winid] > 1 then
return
end

if #M.listing.last_closed > 1 then
M.listing[0] = vim.deepcopy(M.listing.last_closed)
table.insert(M.listing[0], 1, "rabbitmsg://Restore full history")
else
M.listing[0] = nil
M.listing.last_closed = {}
elseif #M.listing.opened > 1 then
M.listing[0] = vim.deepcopy(M.listing.opened)
table.insert(M.listing[0], 1, "rabbitmsg://Open all buffers")
end
end

Expand Down
2 changes: 2 additions & 0 deletions lua/rabbit/plugins/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local M = {}
-- Find the first index of an entry in the table
---@param t table
---@param e any
---@return integer | nil The index, or nil if not found
function M.index(t, e)
for i, v in ipairs(t) do
if v == e then
Expand All @@ -16,6 +17,7 @@ end
-- Treats table like a set; removes all instances of the entry
---@param t table
---@param e any
---@return boolean If anything was removed
function M.sub(t, e)
local i = M.index(t, e)
local ret = i ~= nil
Expand Down
11 changes: 8 additions & 3 deletions lua/rabbit/screen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ end
-- Renders to the screen
---@param line number
---@param specs Rabbit.Screen.Spec[]
---@return integer the next available line
---@return integer integer The next available line
function screen.render(line, specs)
local win = screen.ctx.winnr or 0
local buf = screen.ctx.bufnr or 0
Expand All @@ -95,7 +95,7 @@ end
-- Places a newline before the specs
---@param spec Rabbit.Screen.Spec[]
---@param line? integer the line to start at
---@return integer the next available line
---@return integer integer The next available line
function screen.newline(spec, line)
if line == nil then
line = -1
Expand Down Expand Up @@ -217,6 +217,7 @@ function screen.set_border(win, buf, kwargs)
end

-- Draw the header and first empty line
---@param clear? boolean Whether to clear the screen (default = true)
function screen.draw_top(clear)
if #screen.ctx.title == 0 then
return false
Expand All @@ -232,11 +233,15 @@ end

-- Fill the rest of the screen with empty lines
---@param line? integer Which line to use (default = -1)
---@return integer integer The next empty line
function screen.draw_bottom(line)
local max = vim.api.nvim_buf_line_count(screen.ctx.bufnr)

if line == nil then
line = max
elseif line > screen.ctx.height - 2 then
screen.render(-1, screen.ctx.middle)
return screen.render(-1, screen.ctx.footer)
else
line = math.max(0, math.min(max + 1, line))
end
Expand All @@ -245,7 +250,7 @@ function screen.draw_bottom(line)
screen.render(i, screen.ctx.middle)
end

screen.render(screen.ctx.height - 1, screen.ctx.footer)
return screen.render(screen.ctx.height - 1, screen.ctx.footer)
end


Expand Down

0 comments on commit d088ff3

Please sign in to comment.