mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-19 00:10:00 +00:00
Library sources in a non-UTF-8 charset (e.g. GBK) opened garbled, and Reload with Encoding failed on them with a read-only error: library and virtual documents bypassed the editor's encoding pipeline (archive entries read as strict UTF-8, jdt: documents labelled utf-8, set_encoding rejecting every read-only virtual document). jar: and jdt: sources are now decoded through the same VFS pipeline as files on disk (detection, BOM, UTF-16, explicit reload) and report the charset used. Reload re-decodes virtual documents while keeping them read-only: jar: entries are re-read; for jdt: sources the jdtls source attachment encoding is updated (waiting for Maven's background refresh, retrying once) and the attached entry re-read. Reload is disabled for decompiled and server-generated content and for read-only containers such as the JDK. Co-authored-by: usongon <45442232+usongon@users.noreply.github.com>
201 lines
8.7 KiB
TypeScript
201 lines
8.7 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import type { VirtualDocument } from "@/platform/ipc";
|
|
|
|
const JDT = "jdt://contents/lib-1.0.jar/org.example/Lib.class?=demo/%5C/m2%5C/lib-1.0.jar%3Corg.example%28Lib.class";
|
|
const TEXT =
|
|
"// Source code is unavailable, and was generated by the Fernflower decompiler.\npackage org.example;\n\npublic class Lib {\n void run() {}\n}\n";
|
|
const CSHARP = "csharp:/D:/My%20Game/Game.csproj/decompiled/UnityEngine.MonoBehaviour.cs";
|
|
const CSHARP_TEXT = "namespace UnityEngine;\npublic class MonoBehaviour {\n public void CancelInvoke() {}\n}\n";
|
|
|
|
vi.mock("@/platform/virtualDocs", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("@/platform/virtualDocs")>();
|
|
return {
|
|
...actual,
|
|
virtualDocument: vi.fn(async (uri: string): Promise<VirtualDocument> => {
|
|
if (uri === CSHARP)
|
|
return {
|
|
uri,
|
|
text: CSHARP_TEXT,
|
|
languageId: "csharp",
|
|
title: "UnityEngine.MonoBehaviour.cs",
|
|
description: "UnityEngine.CoreModule",
|
|
decompiled: true,
|
|
encoding: "utf-8",
|
|
hasBom: false,
|
|
canReloadEncoding: false,
|
|
};
|
|
if (uri !== JDT && uri !== "file:///p/target/classes/Lib.class") throw new Error(`no contents for ${uri}`);
|
|
return {
|
|
uri,
|
|
text: TEXT,
|
|
languageId: "java",
|
|
title: "Lib.class",
|
|
description: "/m2/lib-1.0.jar",
|
|
decompiled: true,
|
|
encoding: "utf-8",
|
|
hasBom: false,
|
|
canReloadEncoding: false,
|
|
};
|
|
}),
|
|
};
|
|
});
|
|
|
|
const { EditorController, isTextInput, useTabs, virtualInput } = await import("./controller");
|
|
const { createMemoryBackend } = await import("./backend");
|
|
const { initialTabsState } = await import("./model/tabs");
|
|
const { serializeSession } = await import("./model/session");
|
|
const { useDocMeta } = await import("./model/document");
|
|
const { registerMenuGroup } = await import("@/workbench/contributions");
|
|
const { registerActions } = await import("@/platform/actions");
|
|
const { commands } = await import("@/platform/ipc");
|
|
const { resolveMenuItems } = await import("@/ui");
|
|
|
|
let ctrl: InstanceType<typeof EditorController> | null = null;
|
|
|
|
afterEach(() => {
|
|
ctrl?.stop();
|
|
ctrl = null;
|
|
useTabs.setState(initialTabsState(), true);
|
|
});
|
|
|
|
const activeTab = () => {
|
|
const s = useTabs.getState();
|
|
const pane = s.panes[s.activePane]!;
|
|
return pane.tabs.find((t) => t.id === pane.active)!;
|
|
};
|
|
|
|
describe("virtual editor input", () => {
|
|
it("describes a library class as a text input keyed by its URI", () => {
|
|
const input = virtualInput({ uri: JDT, title: "Lib.class", description: "/m2/lib-1.0.jar", decompiled: true });
|
|
expect(input).toMatchObject({ kind: "virtual", key: `virtual:${JDT}`, path: JDT });
|
|
expect(input.data).toEqual({ title: "Lib.class", description: "/m2/lib-1.0.jar", decompiled: true });
|
|
expect(isTextInput(input)).toBe(true);
|
|
expect(isTextInput({ kind: "diff", key: "d" })).toBe(false);
|
|
});
|
|
|
|
it.each([
|
|
["/m2/lib.jar!/org/example/Lib.class", JDT, "virtual"],
|
|
["/p/target/classes/Lib.class", "/p/Lib.java", "file"],
|
|
["/p/build/classes/Lib.class", "file:///p/target/classes/Lib.class", "virtual"],
|
|
])("routes %s before choosing a binary viewer", async (path, target, kind) => {
|
|
const resolve = vi.spyOn(commands, "lspResolveClassFile").mockResolvedValue({ status: "ok", data: target });
|
|
ctrl = new EditorController(createMemoryBackend({ [target]: TEXT }));
|
|
ctrl.start();
|
|
try {
|
|
await ctrl.openFile(path!, { focus: false });
|
|
expect(resolve).toHaveBeenCalledWith(path);
|
|
expect(activeTab().input).toMatchObject({ path: target, kind });
|
|
expect(ctrl.slotFor(activeTab().id)!.model.ctx().readOnly).toBe(kind === "virtual");
|
|
} finally {
|
|
resolve.mockRestore();
|
|
}
|
|
});
|
|
|
|
it("opens a URI read-only at the target and keeps it out of the saved session", async () => {
|
|
ctrl = new EditorController(createMemoryBackend({ "/p/App.java": "class App {}\n", [JDT]: TEXT }));
|
|
ctrl.start();
|
|
await ctrl.openFile("/p/App.java", { focus: false });
|
|
await ctrl.openFile(JDT, { selection: { line: 3, col: 13, endLine: 3, endCol: 16 }, focus: false });
|
|
|
|
const tab = activeTab();
|
|
expect(tab.input.kind).toBe("virtual");
|
|
expect(tab.input.key).toBe(`virtual:${JDT}`);
|
|
const slot = ctrl.slotFor(tab.id)!;
|
|
expect(slot.model.virtual).toBe(true);
|
|
expect(slot.model.ctx()).toMatchObject({ path: JDT, language: "java", readOnly: true, virtual: true });
|
|
expect(useDocMeta.getState()[JDT]).toMatchObject({ readOnly: true, banner: null });
|
|
const sel = slot.currentState().selection.main;
|
|
expect(slot.currentState().sliceDoc(sel.from, sel.to)).toBe("Lib");
|
|
|
|
// The same URI reuses its tab.
|
|
await ctrl.openFile(JDT, { focus: false });
|
|
expect(Object.values(useTabs.getState().panes).flatMap((p) => p.tabs)).toHaveLength(2);
|
|
|
|
// Navigation history goes back to the file and forward into the library class.
|
|
await ctrl.back();
|
|
expect(activeTab().input.path).toBe("/p/App.java");
|
|
await ctrl.forward();
|
|
expect(activeTab().input.path).toBe(JDT);
|
|
|
|
const session = serializeSession(useTabs.getState(), {});
|
|
expect(JSON.stringify(session)).not.toContain("jdt:");
|
|
expect(
|
|
ctrl
|
|
.tabMenu(useTabs.getState().activePane, activeTab())
|
|
.some((i) => "label" in i && i.label === "Reveal in Project"),
|
|
).toBe(false);
|
|
|
|
// Pathless tabs still get contributed groups, with the tab's input in the context.
|
|
const disposeAction = registerActions([{ id: "test.tab", title: "Test Item", run: () => undefined }]);
|
|
const disposeGroup = registerMenuGroup({
|
|
id: "test.tab",
|
|
menus: ["editorTab"],
|
|
order: 50,
|
|
items: (_menu, { paths, input }) =>
|
|
paths.length || input?.kind !== "virtual"
|
|
? []
|
|
: [{ kind: "action", action: "test.tab", label: "Test Item", arg: input.key }],
|
|
});
|
|
try {
|
|
const items = resolveMenuItems(ctrl.tabMenu(useTabs.getState().activePane, activeTab()));
|
|
expect(items.some((i) => "label" in i && i.label === "Test Item")).toBe(true);
|
|
} finally {
|
|
disposeGroup();
|
|
disposeAction();
|
|
}
|
|
});
|
|
|
|
it("navigates C# types and members in one read-only assembly tab", async () => {
|
|
const backend = createMemoryBackend({ "/p/Player.cs": "class Player {}\n", [CSHARP]: CSHARP_TEXT });
|
|
const save = vi.spyOn(backend, "save");
|
|
ctrl = new EditorController(backend);
|
|
ctrl.start();
|
|
await ctrl.openFile("/p/Player.cs", { focus: false });
|
|
await ctrl.openFile(CSHARP, { selection: { line: 1, col: 13, endLine: 1, endCol: 26 }, focus: false });
|
|
const tab = activeTab();
|
|
const slot = ctrl.slotFor(tab.id)!;
|
|
expect(tab.input).toMatchObject({
|
|
kind: "virtual",
|
|
path: CSHARP,
|
|
data: { title: "UnityEngine.MonoBehaviour.cs", description: "UnityEngine.CoreModule", decompiled: true },
|
|
});
|
|
expect(slot.model.ctx()).toMatchObject({ language: "csharp", readOnly: true, virtual: true });
|
|
const selected = () => {
|
|
const state = slot.currentState();
|
|
return state.sliceDoc(state.selection.main.from, state.selection.main.to);
|
|
};
|
|
expect(selected()).toBe("MonoBehaviour");
|
|
await ctrl.openFile(CSHARP, { selection: { line: 2, col: 16, endLine: 2, endCol: 28 }, focus: false });
|
|
expect(activeTab().id).toBe(tab.id);
|
|
expect(selected()).toBe("CancelInvoke");
|
|
expect(Object.values(useTabs.getState().panes).flatMap((p) => p.tabs)).toHaveLength(2);
|
|
const doc = [...backend.docs.values()].find((d) => d.path === CSHARP)!;
|
|
await expect(backend.edit(doc.id, doc.version, [{ from: 0, to: 0, insert: "x" }])).rejects.toMatchObject({
|
|
kind: "permissionDenied",
|
|
});
|
|
await slot.model.save("explicit");
|
|
expect(save).not.toHaveBeenCalled();
|
|
expect((await backend.snapshot(doc.id)).text).toBe(CSHARP_TEXT);
|
|
await ctrl.back();
|
|
expect(activeTab().input.path).toBe(CSHARP);
|
|
expect(slot.currentState().selection.main.head).toBe(slot.currentState().doc.line(2).from + 13);
|
|
await ctrl.back();
|
|
expect(activeTab().input.path).toBe("/p/Player.cs");
|
|
await ctrl.forward();
|
|
expect(activeTab().input.path).toBe(CSHARP);
|
|
await ctrl.forward();
|
|
expect(slot.currentState().selection.main.head).toBe(slot.currentState().doc.line(3).from + 16);
|
|
expect(JSON.stringify(serializeSession(useTabs.getState(), {}))).not.toContain("csharp:");
|
|
});
|
|
|
|
it.each(["jdt://contents/missing.jar/p/X.class", "csharp:/p/App.csproj/decompiled/Missing.cs"])(
|
|
"does not open a tab when the server has no contents for %s",
|
|
async (uri) => {
|
|
ctrl = new EditorController(createMemoryBackend());
|
|
ctrl.start();
|
|
await ctrl.openFile(uri, { focus: false });
|
|
expect(Object.values(useTabs.getState().panes).flatMap((p) => p.tabs)).toHaveLength(0);
|
|
},
|
|
);
|
|
});
|