Fix diff tab reselect after opening file tab (#6311)

This commit is contained in:
Neil 2026-06-25 01:28:59 -07:00 committed by GitHub
parent 35c3f86c82
commit 75af84bf92
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 2 deletions

View File

@ -579,6 +579,46 @@ describe('createEditorSlice openDiff', () => {
expect(store.getState().activeGroupIdByWorktree['wt-1']).toBe(targetGroupId)
})
it('keeps a diff tab selectable after opening its target file tab', () => {
const store = createEditorTabsStore()
store.getState().openDiff('wt-1', '/repo/file.ts', 'file.ts', 'typescript', false)
const diffFileId = 'wt-1::diff::unstaged::file.ts'
const diffTab = store
.getState()
.unifiedTabsByWorktree['wt-1']?.find((tab) => tab.contentType === 'diff')
if (!diffTab) {
throw new Error('expected diff tab')
}
store.getState().openFile({
filePath: '/repo/file.ts',
relativePath: 'file.ts',
worktreeId: 'wt-1',
language: 'typescript',
mode: 'edit'
})
const stateAfterOpen = store.getState()
const editFile = stateAfterOpen.openFiles.find((file) => file.mode === 'edit')
expect(stateAfterOpen.openFiles.find((file) => file.id === diffFileId)).toEqual(
expect.objectContaining({ mode: 'diff' })
)
expect(editFile).toEqual(expect.objectContaining({ id: '/repo/file.ts', mode: 'edit' }))
expect(
stateAfterOpen.unifiedTabsByWorktree['wt-1']?.find((tab) => tab.contentType === 'editor')
?.entityId
).toBe('/repo/file.ts')
store.getState().activateTab(diffTab.id)
store.getState().setActiveFile(diffFileId)
const stateAfterReselect = store.getState()
expect(stateAfterReselect.groupsByWorktree['wt-1']?.[0]?.activeTabId).toBe(diffTab.id)
expect(stateAfterReselect.activeFileId).toBe(diffFileId)
expect(stateAfterReselect.openFiles.find((file) => file.id === diffFileId)?.mode).toBe('diff')
})
it('reuses a preview editor tab when opening a preview diff', () => {
const store = createEditorTabsStore()

View File

@ -978,6 +978,12 @@ function matchesEditorMode(
return !modes || modes.includes(file.mode)
}
function getReusableOpenFileModes(mode: OpenFile['mode']): readonly OpenFile['mode'][] {
// Why: the same path can be open as both a diff and an editable file; matching
// by path alone collapses those distinct visible tabs onto one OpenFile.
return [mode]
}
function resolveEditorFileIdForOwner(
state: Pick<EditorSlice, 'openFiles'>,
filePath: string,
@ -1507,11 +1513,20 @@ export const createEditorSlice: StateCreator<AppState, [], [], EditorSlice> = (s
(options?.suppressActiveRuntimeFallback
? null
: (s.settings?.activeRuntimeEnvironmentId?.trim() ?? undefined)))
const reusableOpenFileModes = getReusableOpenFileModes(file.mode)
const existing = s.openFiles.find(
(f) =>
f.filePath === file.filePath && isSameEditorOwner(f, worktreeId, runtimeEnvironmentId)
f.filePath === file.filePath &&
matchesEditorMode(f, reusableOpenFileModes) &&
isSameEditorOwner(f, worktreeId, runtimeEnvironmentId)
)
const id = resolveEditorFileIdForOwner(
s,
file.filePath,
worktreeId,
runtimeEnvironmentId,
reusableOpenFileModes
)
const id = resolveEditorFileIdForOwner(s, file.filePath, worktreeId, runtimeEnvironmentId)
editorItemFileId = id
const isPreview = options?.preview ?? false
const recordReplacedPreview = options?.recordReplacedPreview ?? false