Merge pull request #912 from UNscientific-9/fix/native-tab-path-reconcile
fix(tabs): 改名/删除的页签调解覆盖右侧栏原生页签,并让改名不再重载文档
This commit is contained in:
8 files changed
+554
-44
No files matched your search
@@ -22,7 +22,7 @@
|
||||
*/
|
||||
import type { Context } from '../../context-types.ts'
|
||||
import { fileAddressFor } from '../resource-address.ts'
|
||||
import type { NativeTabParams, SidebarSurface } from '../service.ts'
|
||||
import type { NativePathTab, NativeTabParams, SidebarSurface } from '../service.ts'
|
||||
import type { NativeTabRecords } from './tab-adapter.tsx'
|
||||
|
||||
/** One open the surface could not place yet. */
|
||||
@@ -67,6 +67,8 @@ export interface NativeSurface extends SidebarSurface {
|
||||
flushPending(): void
|
||||
/** Stop observing the session list and the mounted-seat feed. */
|
||||
dispose(): void
|
||||
/** Every native tab bound to a path (see {@link SidebarSurface.pathTabs}). */
|
||||
pathTabs(): NativePathTab[]
|
||||
}
|
||||
|
||||
/** The native controller, probed at call time (the service can arrive late). */
|
||||
@@ -295,6 +297,7 @@ export function createNativeSurface(ctx: Context, records: NativeTabRecords): Na
|
||||
return true
|
||||
},
|
||||
has: (tabId, sessionId) => sessionOf(tabId, sessionId) !== undefined,
|
||||
pathTabs: () => records.pathTabs(),
|
||||
flushPending,
|
||||
dispose: () => {
|
||||
unsubscribeList()
|
||||
|
||||
@@ -31,7 +31,7 @@ import { RenderBoundary } from '../RenderBoundary.tsx'
|
||||
import { OrphanedTab } from '../OrphanedTab.tsx'
|
||||
import { referenceInChat } from '../reference-in-chat.ts'
|
||||
import { useSessionRoot } from '../use-session-root.ts'
|
||||
import type { BetterSidebarService } from '../service.ts'
|
||||
import type { BetterSidebarService, NativePathTab } from '../service.ts'
|
||||
import { t } from '../locales.ts'
|
||||
import { isUnread, toggleExpanded } from '../state.ts'
|
||||
import type { SidebarStore, SidebarTab, TabType } from '../state.ts'
|
||||
@@ -82,6 +82,15 @@ export interface NativeTabInfo {
|
||||
/** One native tab's plugin-side view state. */
|
||||
interface View {
|
||||
tab: SidebarTab
|
||||
/**
|
||||
* The SEAT session this record belongs to: the session the tab is drawn in,
|
||||
* which is NOT `scope.sessionId` (a file address carries its own content
|
||||
* session, and the two differ whenever a tab shows another session's file).
|
||||
* Recorded rather than re-derived from the view key: `retain()` and
|
||||
* `pathTabs()` both need it, the key encoding is an internal detail, and a
|
||||
* session id may itself contain the separator.
|
||||
*/
|
||||
seat: string
|
||||
scope: SessionScope
|
||||
/**
|
||||
* A PROJECTION of the session's expansion set (never an authoritative copy):
|
||||
@@ -149,6 +158,14 @@ export interface NativeTabRecords {
|
||||
}): View
|
||||
/** One record by its seat session and native tab id. */
|
||||
get(sessionId: string, id: string): View | undefined
|
||||
/**
|
||||
* Every record that carries a file path, across every live session. The file
|
||||
* tree's rename/delete reconciliation walks BOTH tab spaces: the bottom
|
||||
* workbench's splits come from the store, and the right column — where files
|
||||
* actually open by default — only exists here. Without this the reconciliation
|
||||
* silently skipped every native editor tab (its next save wrote the OLD name).
|
||||
*/
|
||||
pathTabs(): NativePathTab[]
|
||||
/** Whether this seat session has a record for the id. */
|
||||
has(sessionId: string, id: string): boolean
|
||||
/** Merge a patch into the synthetic record (the `updateTab` path). */
|
||||
@@ -267,6 +284,7 @@ export function createNativeTabRecords(): NativeTabRecords {
|
||||
...(meta === undefined ? {} : { meta }),
|
||||
},
|
||||
scope,
|
||||
seat: sessionId,
|
||||
expanded: expandedOf(scope.sessionId),
|
||||
revealed: [],
|
||||
version: 0,
|
||||
@@ -281,12 +299,28 @@ export function createNativeTabRecords(): NativeTabRecords {
|
||||
// A navigation may carry new seed fields (the editor's in-place switch,
|
||||
// a browser tab pointed at another URL, a side chat switching threads);
|
||||
// the record's identity and any plugin-side mutation (title from
|
||||
// `updateTab`) stay. `meta` is the exception — it also has a plugin-side
|
||||
// writer, so only a navigation this record has not adopted yet may seed
|
||||
// it (see {@link navigationRevisions}).
|
||||
// `updateTab`) stay. `meta` and `path` are the exceptions — both also
|
||||
// have a plugin-side writer, so only a navigation this record has not
|
||||
// adopted yet may seed them (see {@link navigationRevisions}).
|
||||
const patch: Partial<SidebarTab> = {}
|
||||
// Whether the host is addressing this record with a navigation it has not
|
||||
// delivered yet. A revision-less caller (the plugin's own layout, an
|
||||
// older caller) counts as new: it has no ledger to be compared against.
|
||||
const newNavigation = revision === undefined || navigationRevisions.get(key) !== revision
|
||||
if (revision !== undefined && newNavigation) {
|
||||
navigationRevisions.set(key, revision)
|
||||
if (params?.meta !== undefined) patch.meta = params.meta
|
||||
}
|
||||
// `path` rides the same gate as `meta`: the file tree retargets an open
|
||||
// editor onto a renamed file through `updateTab`, and while the host's
|
||||
// address keeps naming the OLD path, honoring it on every render undid
|
||||
// that retarget — the next save wrote a name the user had just renamed
|
||||
// away, recreating it. Only a real navigation (a new revision) may move
|
||||
// the record back onto its address.
|
||||
const nextPath = params?.path ?? params?.url
|
||||
if (nextPath !== undefined && nextPath !== existing.tab.path) patch.path = nextPath
|
||||
if (nextPath !== undefined && nextPath !== existing.tab.path && newNavigation) {
|
||||
patch.path = nextPath
|
||||
}
|
||||
// The landing line clears as well as sets: an in-place switch from
|
||||
// `a.c:131` to a bare `a.c` must not leave the old line behind and jump
|
||||
// the reader back there. Compared against the current value so the
|
||||
@@ -294,10 +328,6 @@ export function createNativeTabRecords(): NativeTabRecords {
|
||||
// keeps its identity.
|
||||
if (params?.line !== existing.tab.line) patch.line = params?.line
|
||||
if (params?.diff !== undefined) patch.diff = params.diff
|
||||
if (revision !== undefined && navigationRevisions.get(key) !== revision) {
|
||||
navigationRevisions.set(key, revision)
|
||||
if (params?.meta !== undefined) patch.meta = params.meta
|
||||
}
|
||||
// The expansion set always mirrors the CURRENT session state (a record
|
||||
// reused for another session must not keep the previous one's set).
|
||||
const expanded = expandedOf(scope.sessionId)
|
||||
@@ -315,6 +345,24 @@ export function createNativeTabRecords(): NativeTabRecords {
|
||||
return next
|
||||
},
|
||||
get: (sessionId, id) => views.get(viewKey(sessionId, id)),
|
||||
pathTabs() {
|
||||
const tabs: NativePathTab[] = []
|
||||
for (const entry of views.values()) {
|
||||
const path = entry.tab.path
|
||||
// A browser tab's address lands on `path` too (a url seed is stored
|
||||
// there), so this is every tab bound to a path or an address.
|
||||
if (path !== undefined) {
|
||||
tabs.push({
|
||||
sessionId: entry.seat,
|
||||
scopeSessionId: entry.scope.sessionId,
|
||||
cwd: entry.scope.cwd,
|
||||
id: entry.tab.id,
|
||||
path,
|
||||
})
|
||||
}
|
||||
}
|
||||
return tabs
|
||||
},
|
||||
has: (sessionId, id) => views.has(viewKey(sessionId, id)),
|
||||
update(sessionId, id, patch) {
|
||||
const key = viewKey(sessionId, id)
|
||||
@@ -352,12 +400,8 @@ export function createNativeTabRecords(): NativeTabRecords {
|
||||
},
|
||||
retain(sessions) {
|
||||
let dropped = false
|
||||
for (const key of [...views.keys()]) {
|
||||
// The key's prefix is the seat session; a session id may itself carry
|
||||
// the separator only if the host allowed one, so split on the first.
|
||||
const separator = key.indexOf('::')
|
||||
const owner = separator === -1 ? key : key.slice(0, separator)
|
||||
if (!sessions.has(owner)) {
|
||||
for (const [key, entry] of [...views]) {
|
||||
if (!sessions.has(entry.seat)) {
|
||||
views.delete(key)
|
||||
// The ledger is part of the record (see `drop`): a session the user
|
||||
// deleted must not leave its revisions behind.
|
||||
|
||||
Reference in new issue
Block a user