Skip to content

Commit 1eb19bc

Browse files
committed
feat(bridge): add set_cwd for worktree switch follow
1 parent c29cc16 commit 1eb19bc

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

lua/neocrush/bridge.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
--- show_locations(t, items) -> opens the Telescope picker (or quickfix).
1313
--- flash_edit(p, s, e) -> highlights a freshly-edited region.
1414
--- file_changed(p) -> reloads a buffer Crush wrote on disk; kills the W11 prompt.
15+
--- set_cwd(p) -> changes the editor's working dir (worktree switch).
1516
---@brief ]]
1617

1718
local M = {}
@@ -199,4 +200,32 @@ function M.file_changed(path)
199200
end)
200201
end
201202

203+
---------------------------------------------------------------------------
204+
-- API: set_cwd(path)
205+
---------------------------------------------------------------------------
206+
207+
---Change Neovim's working directory to path, e.g. when Crush switches
208+
---the active worktree so the editor follows the agent into the new
209+
---tree. Uses tab-local :tcd so a switch only affects the current tab
210+
---(leaving other tabs' cwd intact), falling back to global :cd if tcd
211+
---is unavailable. Best-effort: never raises, and silently ignores a
212+
---missing/empty path or a directory that no longer exists.
213+
---@param path string absolute directory path
214+
function M.set_cwd(path)
215+
if path == nil or path == '' then
216+
return
217+
end
218+
vim.schedule(function()
219+
local dir = vim.fn.fnamemodify(path, ':p')
220+
if vim.fn.isdirectory(dir) == 0 then
221+
return
222+
end
223+
-- Quote against spaces/specials in the path.
224+
local quoted = vim.fn.fnameescape(dir)
225+
if not pcall(vim.cmd, 'tcd ' .. quoted) then
226+
pcall(vim.cmd, 'cd ' .. quoted)
227+
end
228+
end)
229+
end
230+
202231
return M

0 commit comments

Comments
 (0)