From ff4b19db2ea1308b470c64de96dd957b065aa533 Mon Sep 17 00:00:00 2001 From: dk949 <56653556+dk949@users.noreply.github.com> Date: Sun, 1 Jun 2025 15:05:48 +0100 Subject: [PATCH 1/2] feat(config): pre_open can return updated config The `pre_open` hook can now return a table which will be merged with the user's default config for the current file being edited. This allows per file name/type configuration provided appropriate data is passed via the `guest_data` hook. If `pre_open` returns `nil`, behaviour is unchanged. --- lua/flatten/core.lua | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) 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, {}) From 0fd34e45490ed7f97c9ed28d0a1598591e9de8e7 Mon Sep 17 00:00:00 2001 From: dk949 <56653556+dk949@users.noreply.github.com> Date: Sun, 1 Jun 2025 15:13:23 +0100 Subject: [PATCH 2/2] docs(config): added new pre_open feature to docs --- README.md | 3 ++- lua/flatten/init.lua | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) 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/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.