From 695424beb2c52dbf17e7fccf885dd266a09a919a Mon Sep 17 00:00:00 2001 From: LU XIANGRUI <1078666705@qq.com> Date: Mon, 30 Nov 2026 18:40:41 +0900 Subject: [PATCH] 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 <26499420+wildcatDownstairs@users.noreply.github.com> --- src/editor/api.ts | 5 +++++ src/editor/core/controller.ts | 15 +++++++++++++-- src/editor/core/model/session.test.ts | 12 ++++++++++++ src/editor/core/model/session.ts | 20 +++++++++++++++++++- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/editor/api.ts b/src/editor/api.ts index 0375494687..327c248f9b 100644 --- a/src/editor/api.ts +++ b/src/editor/api.ts @@ -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(); +/** 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 diff --git a/src/editor/core/controller.ts b/src/editor/core/controller.ts index 5a61b06091..58143168b3 100644 --- a/src/editor/core/controller.ts +++ b/src/editor/core/controller.ts @@ -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)) diff --git a/src/editor/core/model/session.test.ts b/src/editor/core/model/session.test.ts index 3e0be06c5d..8553cc87a5 100644 --- a/src/editor/core/model/session.test.ts +++ b/src/editor/core/model/session.test.ts @@ -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"); diff --git a/src/editor/core/model/session.ts b/src/editor/core/model/session.ts index ee0e142cf6..47ea943dce 100644 --- a/src/editor/core/model/session.ts +++ b/src/editor/core/model/session.ts @@ -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, 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}`;