diff --git a/README.md b/README.md index 80846ef..c101647 100644 --- a/README.md +++ b/README.md @@ -229,8 +229,9 @@ Defaults are in `flatten.hooks`. This is useful for customizing when files should be sent to a host instance and when they should be opened in a new one. -- `hooks.pre_open`: `fun(opts: Flatten.PreOpenContext)` +- `hooks.pre_open`: `fun(opts: Flatten.PreOpenContext): Flatten.Config?` - Called before opening files. + - Returned config, if any, will be merged with the global config (for this file only). - `Flatten.PreOpenContext`: - `data`: `any` diff --git a/lua/flatten/core.lua b/lua/flatten/core.lua index e7388d3..4f14132 100644 --- a/lua/flatten/core.lua +++ b/lua/flatten/core.lua @@ -71,6 +71,13 @@ local function parse_argv(argv) return pre_cmds, post_cmds end +local function mergeConfig(new, default) + if new == nil then + return default + end + return vim.tbl_deep_extend("keep", new, default) +end + ---@param opts { argv: string[], response_pipe: string, guest_cwd: string } ---@return boolean function M.run_commands(opts) @@ -142,9 +149,6 @@ function M.edit_files(opts) local force_block = opts.force_block local argv = opts.argv local config = require("flatten").config - local hooks = config.hooks - local focus_first = config.window.focus == "first" - local open = config.window.open local data = opts.data local quickfix = opts.quickfix @@ -166,9 +170,15 @@ function M.edit_files(opts) return false end - hooks.pre_open({ - data = data, - }) + config = mergeConfig( + config.hooks.pre_open({ + data = data, + }), + config + ) + local hooks = config.hooks + local focus_first = config.window.focus == "first" + local open = config.window.open for _, cmd in ipairs(pre_cmds) do vim.api.nvim_exec2(cmd, {}) diff --git a/lua/flatten/init.lua b/lua/flatten/init.lua index 078be84..cb419f5 100644 --- a/lua/flatten/init.lua +++ b/lua/flatten/init.lua @@ -124,7 +124,8 @@ local Flatten = {} ---config.nest_if_no_args is respected. ---@field should_nest? fun(host: integer):boolean ---Called before a nested session is opened. ----@field pre_open? fun(opts: Flatten.PreOpenContext) +---Retuened config is merged with global config for this file +---@field pre_open? fun(opts: Flatten.PreOpenContext): Flatten.Config? ---Called after a nested session is opened. ---@field post_open? fun(opts: Flatten.PostOpenContext) ---Called when a nested session is done waiting for the host.