fix(editor): reopen restored library source tabs read-only (#6950)

The library-source mark lived only in memory, so a tab restored after a reload opened the same file editable. The editor session now saves the mark and restores it before the documents open.

Closes #6947

Co-authored-by: wildcatDownstairs <[email protected]>
Цей коміт міститься в:
LU XIANGRUIтаwildcatDownstairs authored and GitHub committed 2026-11-30 17:40:41 +08:00
1 джерело 9943d71d02
коміт 695424beb2
4 files changed
+49 -3

No files matched your search

+5
Переглянути файл
@@ -918,6 +918,11 @@ function outsideProject(path: string): boolean {
/** Files outside the project that code navigation opened: library sources, shown read-only. */
const navigatedLibraryFiles = new Set<string>();
/** Marks `path` as a library source again, for a tab restored from a saved session. */
export function markLibraryFile(path: string): void {
navigatedLibraryFiles.add(path);
}
/**
* Opens a code location a language server or the index answered (a declaration, an
* implementation, a documentation link) and selects `selection`. A file outside the project is a
+13 -2
Переглянути файл
@@ -21,6 +21,8 @@ import {
_provideOpenEditorPaths,
_setActiveEditor,
focusedCellEditor,
isLibraryPath,
markLibraryFile,
shownLine,
type NavigationLocation,
} from "@/editor/api";
@@ -114,7 +116,14 @@ import {
import { EditorSlot, type SlotHost } from "./model/slot";
import { NavigationHistory } from "./model/navHistory";
import { EditHistory } from "./model/editHistory";
import { loadSession, restoreTabsState, serializeSession, storeSession, type FileViewState } from "./model/session";
import {
loadSession,
restoreTabsState,
serializeSession,
sessionLibraryFiles,
storeSession,
type FileViewState,
} from "./model/session";
import { docBackend, type DocBackend } from "./backend";
import { isClassFile } from "@/platform/classFiles";
import { isVirtualUri, virtualDocument } from "@/platform/virtualDocs";
@@ -663,7 +672,7 @@ export class EditorController implements SlotHost {
const kinds = useContributions.getState().editorKinds;
storeSession(
root,
serializeSession(useTabs.getState(), files, (input) => !!kinds.get(input.kind)?.restorable),
serializeSession(useTabs.getState(), files, (input) => !!kinds.get(input.kind)?.restorable, isLibraryPath),
);
}
@@ -700,6 +709,8 @@ export class EditorController implements SlotHost {
if (this.forgottenSessions.delete(root)) return;
const session = loadSession(root);
if (!session) return;
// Before the documents open: a library source opens read-only.
for (const path of sessionLibraryFiles(session)) markLibraryFile(path);
const state = restoreTabsState(session, fileKey);
const kinds = useContributions.getState().editorKinds;
for (const pane of Object.values(state.panes))
+12
Переглянути файл
@@ -14,6 +14,7 @@ import {
parseSession,
restoreTabsState,
serializeSession,
sessionLibraryFiles,
slashPath,
storeSession,
type EditorSession,
@@ -59,6 +60,17 @@ describe("editor session", () => {
expect(r.layout).toMatchObject({ kind: "split", dir: "column" });
});
it("remembers which file tabs are library sources", () => {
let s = initialTabsState();
s = open(s, "/p/main.swift");
s = open(s, "/tmp/sk/Swift.String.swiftinterface");
const library = (path: string) => path.endsWith(".swiftinterface");
const session = serializeSession(s, {}, undefined, library)!;
const parsed = parseSession(JSON.parse(JSON.stringify(session)))!;
expect(sessionLibraryFiles(parsed)).toEqual(["/tmp/sk/Swift.String.swiftinterface"]);
expect(sessionLibraryFiles(parseSession(JSON.parse(JSON.stringify(serializeSession(s, {})!)))!)).toEqual([]);
});
it("saves tabs of restorable editor kinds with their input", () => {
let s = initialTabsState();
s = open(s, "/p/a.ts");
+19 -1
Переглянути файл
@@ -18,6 +18,8 @@ export interface SessionTab {
input?: EditorInput;
pinned?: boolean;
preview?: boolean;
/** A library source (read-only), e.g. an SDK interface code navigation opened outside the project. */
library?: boolean;
}
export type SessionLayout =
@@ -43,11 +45,15 @@ const STORAGE_PREFIX = "openidea.editorSession.v1:";
const isFileTab = (t: Tab) => t.input.kind === "file" && !!t.input.path;
/** `restorable`: whether a non-file tab is saved (its kind asked for it). */
/**
* `restorable`: whether a non-file tab is saved (its kind asked for it). `library`: whether a
* file tab shows a library source, so it opens read-only again.
*/
export function serializeSession(
s: TabsState,
files: Record<string, FileViewState>,
restorable: (input: EditorInput) => boolean = () => false,
library: (path: string) => boolean = () => false,
): EditorSession | null {
const saved = (t: Tab) => isFileTab(t) || (t.input.kind !== "file" && restorable(t.input));
const walk = (n: LayoutNode): SessionLayout | null => {
@@ -66,6 +72,7 @@ export function serializeSession(
...(isFileTab(t) ? { path: t.input.path! } : { input: { ...t.input } }),
...(t.pinned ? { pinned: true } : {}),
...(t.preview ? { preview: true } : {}),
...(isFileTab(t) && library(t.input.path!) ? { library: true } : {}),
})),
active,
};
@@ -151,6 +158,17 @@ export function parseSession(raw: unknown): EditorSession | null {
};
}
/** Paths of the file tabs saved as library sources. */
export function sessionLibraryFiles(session: EditorSession): string[] {
const out: string[] = [];
const walk = (l: SessionLayout) =>
l.kind === "pane"
? l.tabs.forEach((t) => t.library === true && t.path && out.push(t.path))
: l.children.forEach(walk);
walk(session.layout);
return out;
}
let seq = 0;
const id = (p: string) => `${p}r${++seq}`;