diff --git a/lua/flatten/core.lua b/lua/flatten/core.lua index db275f7..5ff3b85 100644 --- a/lua/flatten/core.lua +++ b/lua/flatten/core.lua @@ -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 @@ -166,7 +166,7 @@ function M.edit_files(opts) return false end - callbacks.pre_open({ + hooks.pre_open({ data = data, }) @@ -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, @@ -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, }) diff --git a/lua/flatten/guest.lua b/lua/flatten/guest.lua index a77a128..78e6826 100644 --- a/lua/flatten/guest.lua +++ b/lua/flatten/guest.lua @@ -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)) @@ -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 @@ -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 @@ -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, diff --git a/lua/flatten/init.lua b/lua/flatten/init.lua index ea96c06..ba4e418 100644 --- a/lua/flatten/init.lua +++ b/lua/flatten/init.lua @@ -1,11 +1,11 @@ ---@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 @@ -13,7 +13,7 @@ local Flatten = {} ---@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? @@ -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 @@ -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. @@ -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 @@ -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 @@ -193,19 +193,19 @@ 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 @@ -213,7 +213,7 @@ end ---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 @@ -228,7 +228,7 @@ 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 @@ -236,7 +236,7 @@ end ---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 @@ -266,7 +266,7 @@ end ---@type Flatten.Config Flatten.config = { - callbacks = Callbacks, + hooks = Hooks, block_for = { gitcommit = true, gitrebase = true, @@ -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