Skip to content

Commit

Permalink
feat(config)!: rename callbacks to hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Dec 30, 2024
1 parent 44cf0bb commit 738e16b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
8 changes: 4 additions & 4 deletions lua/flatten/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function M.edit_files(opts)
local force_block = opts.force_block
local argv = opts.argv
local config = require("flatten").config
local callbacks = config.callbacks
local hooks = config.hooks
local focus_first = config.window.focus == "first"
local open = config.window.open
local data = opts.data
Expand All @@ -166,7 +166,7 @@ function M.edit_files(opts)
return false
end

callbacks.pre_open({
hooks.pre_open({
data = data,
})

Expand Down Expand Up @@ -351,7 +351,7 @@ function M.edit_files(opts)
vim.api.nvim_exec2(cmd, {})
end

callbacks.post_open({
hooks.post_open({
bufnr = bufnr,
winnr = winnr,
filetype = ft,
Expand All @@ -363,7 +363,7 @@ function M.edit_files(opts)
if block then
M.augroup =
vim.api.nvim_create_augroup("flatten_notify", { clear = true })
notify_when_done(response_pipe, bufnr, callbacks.block_end, {
notify_when_done(response_pipe, bufnr, hooks.block_end, {
filetype = ft,
data = data,
})
Expand Down
12 changes: 6 additions & 6 deletions lua/flatten/guest.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ local function send_files(files, stdin, quickfix)
local config = require("flatten").config

local force_block = vim.g.flatten_wait ~= nil
or config.callbacks.should_block(vim.v.argv)
or config.hooks.should_block(vim.v.argv)

local server = vim.fn.fnameescape(vim.v.servername)
local cwd = vim.fn.fnameescape(vim.fn.getcwd(-1, -1))
Expand All @@ -53,8 +53,8 @@ local function send_files(files, stdin, quickfix)
force_block = force_block,
}

if config.callbacks.guest_data then
args.data = config.callbacks.guest_data()
if config.hooks.guest_data then
args.data = config.hooks.guest_data()
end

for _, buf in ipairs(vim.api.nvim_list_bufs()) do
Expand Down Expand Up @@ -105,7 +105,7 @@ function M.init(host_pipe)

local config = require("flatten").config

if config.callbacks.should_nest and config.callbacks.should_nest(host) then
if config.hooks.should_nest and config.hooks.should_nest(host) then
return
end

Expand Down Expand Up @@ -153,11 +153,11 @@ function M.init(host_pipe)
if nfiles < 1 and not quickfix then
local should_nest, should_block = config.nest_if_no_args, false

if config.callbacks.no_files then
if config.hooks.no_files then
local result = require("flatten.rpc").exec_on_host(
host,
function(argv)
return require("flatten").config.callbacks.no_files({
return require("flatten").config.hooks.no_files({
argv = argv,
})
end,
Expand Down
50 changes: 32 additions & 18 deletions lua/flatten/init.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
---@class Flatten
---@field callbacks Flatten.Callbacks
---@field hooks Flatten.Hooks
---@field config Flatten.Config
local Flatten = {}

---Top-level config table
---@class Flatten.Config
---@field callbacks Flatten.Callbacks
---@field hooks Flatten.Hooks
---@field window Flatten.WindowConfig
---@field integrations Flatten.Integrations
---@field block_for Flatten.BlockFor
---@field allow_cmd_passthrough Flatten.AllowCmdPassthrough
---@field nest_if_no_args Flatten.NestIfNoArgs

---@class Flatten.PartialConfig: Flatten.Config
---@field callbacks Flatten.Callbacks
---@field hooks Flatten.Hooks
---@field window Flatten.WindowConfig?
---@field integrations Flatten.Integrations?
---@field block_for Flatten.BlockFor?
Expand Down Expand Up @@ -80,7 +80,7 @@ local Flatten = {}
---Flatten nested instances in wezterm tabs / panes
---@field wezterm? boolean

---Passed to callbacks that handle opening files
---Passed to hooks that handle opening files
---@class Flatten.BufInfo
---@field fname string
---@field bufnr Flatten.BufferId
Expand Down Expand Up @@ -115,8 +115,8 @@ local Flatten = {}
---@class Flatten.NoFilesArgs
---@field argv string[]

---Callbacks to define custom behavior
---@class Flatten.Callbacks
---Hooks to define custom behavior
---@class Flatten.Hooks
---Called to determine if a nested session should wait for the host to close the file.
---@field should_block? fun(argv: string[]):boolean
---If this returns true, the nested session will be opened.
Expand All @@ -138,14 +138,14 @@ local Flatten = {}
---for communication between the host and guest, and to determine whether
---an nvim instance is a host or guest in the first place.
---@field pipe_path? fun():string?
local Callbacks = {}
local Hooks = {}

Flatten.callbacks = Callbacks
Flatten.hooks = Hooks

---Called to determine if a nested session should wait for the host to close the file.
---@param argv string[]
---@return boolean
function Callbacks.should_block(argv)
function Hooks.should_block(argv)
local _ = argv
return false
end
Expand All @@ -155,7 +155,7 @@ end
---config.nest_if_no_args is respected.
---@param host integer channel id
---@return boolean
function Callbacks.should_nest(host)
function Hooks.should_nest(host)
-- don't nest in a neovim terminal (unless nest_if_no_args is set)
if vim.env.NVIM ~= nil then
return false
Expand Down Expand Up @@ -193,27 +193,27 @@ end

---Called before a nested session is opened.
---@param opts Flatten.PreOpenContext
function Callbacks.pre_open(opts)
function Hooks.pre_open(opts)
local _ = opts
end

---Called after a nested session is opened.
---@param opts Flatten.PostOpenContext
function Callbacks.post_open(opts)
function Hooks.post_open(opts)
local _ = opts
end

---Called when a nested session is done waiting for the host.
---@param opts Flatten.BlockEndContext
function Callbacks.block_end(opts)
function Hooks.block_end(opts)
local _ = opts
end

---Executed when there are no files to open, to determine whether
---to nest or not. The default implementation returns config.nest_if_no_args.
---@param opts { argv: string[] }
---@return Flatten.NoFilesBehavior
function Callbacks.no_files(opts)
function Hooks.no_files(opts)
if not Flatten.config.nest_if_no_args then
return false
end
Expand All @@ -228,15 +228,15 @@ end

---Only executed on the guest, used to pass arbitrary data to the host.
---@return any
function Callbacks.guest_data()
function Hooks.guest_data()
return nil
end

---Executed on init on both host and guest. Used to determine the pipe path
---for communication between the host and guest, and to determine whether
---an nvim instance is a host or guest in the first place.
---@return string | integer | nil
function Callbacks.pipe_path()
function Hooks.pipe_path()
-- If running in a terminal inside Neovim:
if vim.env.NVIM then
return vim.env.NVIM
Expand Down Expand Up @@ -266,7 +266,7 @@ end

---@type Flatten.Config
Flatten.config = {
callbacks = Callbacks,
hooks = Hooks,
block_for = {
gitcommit = true,
gitrebase = true,
Expand Down Expand Up @@ -295,7 +295,21 @@ end
function Flatten.setup(opts)
Flatten.config = vim.tbl_deep_extend("keep", opts or {}, Flatten.config)

local pipe_path = Flatten.config.callbacks.pipe_path()
if vim.tbl_get(Flatten.config, "callbacks") ~= nil then
vim.schedule(function()
vim.notify_once(
"flatten.nvim: `callbacks` is deprecated, use `hooks` instead",
vim.log.levels.WARN,
{
title = "flatten.nvim",
filetype = "markdown",
}
)
end)
Flatten.config.hooks = vim.tbl_get(Flatten.config, "callbacks")
end

local pipe_path = Flatten.config.hooks.pipe_path()

if
pipe_path == nil
Expand Down

0 comments on commit 738e16b

Please sign in to comment.