Skip to content

Commit 945093b

Browse files
committed
Bugfix: Fix stale cursor index after file ops
- Move cursor/selection adjustment BEFORE `fetchEntryUnderCursor()` in the `directory-diff` handler - Previously, `fetchEntryUnderCursor()` fired with the old index against the new (shorter) listing, causing `get_file_at: index out of bounds` errors after delete/move operations - The adjustment was already correct, just ordered after the async IPC call instead of before it
1 parent 81e398c commit 945093b

1 file changed

Lines changed: 23 additions & 21 deletions

File tree

apps/desktop/src/lib/file-explorer/pane/FilePane.svelte

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1411,34 +1411,16 @@
14111411
}
14121412
}
14131413
1414-
void fetchEntryUnderCursor()
1415-
void fetchListingStats()
1416-
1417-
// Diff-driven selection adjustment: re-resolve selected names to new indices
1418-
if (operationSelectedNames !== null && operationSelectedNames !== 'all') {
1419-
diffGeneration++
1420-
const myGeneration = diffGeneration
1421-
void findFileIndices(listingId, operationSelectedNames, includeHidden).then((nameToIndexMap) => {
1422-
if (myGeneration !== diffGeneration) return
1423-
selection.setSelectedIndices(buildFrontendIndices(nameToIndexMap, hasParent))
1424-
})
1425-
}
1426-
1427-
// Adjust cursor and selection after structural diffs (adds/removes)
1414+
// Adjust cursor and selection BEFORE fetching entry under cursor,
1415+
// otherwise fetchEntryUnderCursor uses the old index against the
1416+
// new (shorter) listing, causing "index out of bounds" errors.
14281417
const hasStructuralChanges = diff.changes.some((c) => c.type === 'add' || c.type === 'remove')
14291418
if (hasStructuralChanges) {
14301419
const removeIndices = diff.changes.filter((c) => c.type === 'remove').map((c) => c.index)
14311420
const addIndices = diff.changes.filter((c) => c.type === 'add').map((c) => c.index)
14321421
14331422
const offset = hasParent ? 1 : 0
14341423
1435-
// Selection: only adjust outside operations (operations handle via findFileIndices)
1436-
if (operationSelectedNames === null && selection.selectedIndices.size > 0) {
1437-
const backendSelected = selection.getSelectedIndices().map((i) => i - offset)
1438-
const adjusted = adjustSelectionIndices(backendSelected, removeIndices, addIndices)
1439-
selection.setSelectedIndices(adjusted.map((i) => i + offset))
1440-
}
1441-
14421424
// Cursor: always adjust (no operation-specific cursor handling exists)
14431425
const backendCursor = cursorIndex - offset
14441426
const adjustedCursor = adjustSelectionIndices([backendCursor], removeIndices, addIndices)
@@ -1447,6 +1429,26 @@
14471429
} else {
14481430
cursorIndex = Math.max(0, Math.min(cursorIndex, count - 1 + offset))
14491431
}
1432+
1433+
// Selection: only adjust outside operations (operations handle via findFileIndices)
1434+
if (operationSelectedNames === null && selection.selectedIndices.size > 0) {
1435+
const backendSelected = selection.getSelectedIndices().map((i) => i - offset)
1436+
const adjusted = adjustSelectionIndices(backendSelected, removeIndices, addIndices)
1437+
selection.setSelectedIndices(adjusted.map((i) => i + offset))
1438+
}
1439+
}
1440+
1441+
void fetchEntryUnderCursor()
1442+
void fetchListingStats()
1443+
1444+
// Diff-driven selection adjustment: re-resolve selected names to new indices
1445+
if (operationSelectedNames !== null && operationSelectedNames !== 'all') {
1446+
diffGeneration++
1447+
const myGeneration = diffGeneration
1448+
void findFileIndices(listingId, operationSelectedNames, includeHidden).then((nameToIndexMap) => {
1449+
if (myGeneration !== diffGeneration) return
1450+
selection.setSelectedIndices(buildFrontendIndices(nameToIndexMap, hasParent))
1451+
})
14501452
}
14511453
})
14521454
})

0 commit comments

Comments
 (0)