Skip to content

Commit 65a2407

Browse files
committed
fix(bridge): surface and flash edited files even when not yet loaded
flash_edit used to silently no-op when the edited buffer wasn't already open, which lost the surface-the-edit-and-flash behavior the LSP-based v1 had via workspace/applyEdit. Now flash_edit: 1. bufadd+bufload the path when no matching buffer is loaded; 2. surface it via highlight.ensure_buffer_visible (respects auto_focus); 3. flash the range as before. ensure_buffer_visible is now an exported method on the highlight module so the bridge can reuse it.
1 parent dd10fdd commit 65a2407

2 files changed

Lines changed: 20 additions & 6 deletions

File tree

lua/neocrush/bridge.lua

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ end
149149
---------------------------------------------------------------------------
150150

151151
---Briefly highlight the region [start_line, end_line) in the buffer for
152-
---path. Does nothing if the file isn't open in this Neovim. Best-effort:
153-
---never raises so a transient API change can't cascade into a Crush tool
154-
---failure.
152+
---path. If the file isn't open in this Neovim, load it and (when
153+
---auto_focus is on) surface it in a window first so the change is
154+
---visible — same behavior the LSP-based v1 had via workspace/applyEdit.
155+
---Best-effort: never raises so a transient API change can't cascade
156+
---into a Crush tool failure.
155157
---@param path string absolute file path
156158
---@param start_line integer 0-indexed
157159
---@param end_line integer 0-indexed exclusive
@@ -160,8 +162,20 @@ function M.flash_edit(path, start_line, end_line)
160162
local highlight = require 'neocrush.highlight'
161163
local bufnr = find_buffer(path)
162164
if bufnr == 0 then
163-
return
165+
-- Buffer not loaded. If the path exists on disk, create a buffer
166+
-- for it so the rest of the flow (visibility + flash) can run.
167+
if path == nil or path == '' or vim.fn.filereadable(path) ~= 1 then
168+
return
169+
end
170+
bufnr = vim.fn.bufadd(path)
171+
if bufnr == 0 then
172+
return
173+
end
174+
pcall(vim.fn.bufload, bufnr)
164175
end
176+
-- Surface the buffer in a window when auto_focus is on; this is what
177+
-- makes a Crush edit appear in the editor like the old LSP path did.
178+
pcall(highlight.ensure_buffer_visible, bufnr)
165179
-- Keep buffer in sync with disk before we paint extmarks; otherwise
166180
-- the line range may already be stale.
167181
vim.api.nvim_buf_call(bufnr, function()

lua/neocrush/highlight.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ end
7272
--- When auto_focus is disabled, only returns a window if the buffer is already visible.
7373
---@param bufnr integer Buffer handle
7474
---@return integer|nil win Window handle where buffer is displayed, or nil
75-
local function ensure_buffer_visible(bufnr)
75+
function M.ensure_buffer_visible(bufnr)
7676
if not vim.api.nvim_buf_is_valid(bufnr) then
7777
return nil
7878
end
@@ -158,7 +158,7 @@ function M.apply_edit_handler(err, result, ctx, conf)
158158
pcall(vim.fn.bufload, bufnr)
159159
end
160160

161-
local win = ensure_buffer_visible(bufnr)
161+
local win = M.ensure_buffer_visible(bufnr)
162162

163163
for _, edit in ipairs(edits) do
164164
local start_line = edit.range.start.line

0 commit comments

Comments
 (0)