Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
22 changes: 16 additions & 6 deletions lua/flatten/core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand All @@ -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, {})
Expand Down
3 changes: 2 additions & 1 deletion lua/flatten/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down