|
12 | 12 | --- show_locations(t, items) -> opens the Telescope picker (or quickfix). |
13 | 13 | --- flash_edit(p, s, e) -> highlights a freshly-edited region. |
14 | 14 | --- 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). |
15 | 16 | ---@brief ]] |
16 | 17 |
|
17 | 18 | local M = {} |
@@ -199,4 +200,32 @@ function M.file_changed(path) |
199 | 200 | end) |
200 | 201 | end |
201 | 202 |
|
| 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 | + |
202 | 231 | return M |
0 commit comments