mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
A SNAPSHOT installed again replaces its jar in place, so class URIs and jdtls' class handles stay the same; the backend's virtual document cache, the editor's cache and jdtls' decompiler cache all kept the old contents. Cached library classes now record their archive's modification time and are read again once it changes; after a build tool sync jdtls refreshes external archives, closes the changed roots and drops its decompiled sources (openidea.java.refreshArchives, java-usages 1.14.0), and open classes whose jar changed are replaced. The editor drops cached virtual documents when project roots change.
150 lines
5.5 KiB
TypeScript
150 lines
5.5 KiB
TypeScript
import { useEffect, useState } from "react";
|
|
import { create } from "zustand";
|
|
import { registerActions } from "@/platform/actions";
|
|
import { call, commands, isCancelled, nextRequestId, onIdeEvent, type RequestId } from "@/platform/ipc";
|
|
import { tx } from "@/platform/i18n";
|
|
import { useProject } from "@/platform/project";
|
|
import { invalidateVirtualDocument, invalidateVirtualDocuments } from "@/platform/virtualDocs";
|
|
import { maybeEditorService, notifications } from "@/workbench/contributions";
|
|
|
|
interface SourceState {
|
|
request?: RequestId;
|
|
message?: string;
|
|
}
|
|
|
|
const useSourceState = create<Record<string, SourceState>>(() => ({}));
|
|
|
|
async function retrieve(path: string, choose: boolean) {
|
|
if (useSourceState.getState()[path]?.request !== undefined) return;
|
|
const editor = maybeEditorService();
|
|
const project = useProject.getState().project;
|
|
if (!editor || !project) return;
|
|
const cursor = editor.activeCursor();
|
|
const pos = cursor?.path === path ? { line: cursor.line, col: cursor.col } : { line: 0, col: 0 };
|
|
const request = nextRequestId();
|
|
useSourceState.setState({ [path]: { request } });
|
|
try {
|
|
let sources: string | null = null;
|
|
if (choose) {
|
|
const { open } = await import("@/platform/dialog");
|
|
const selected = await open({
|
|
title: tx("Choose Sources…"),
|
|
filters: [{ name: tx("Source archives"), extensions: ["jar", "zip"] }],
|
|
});
|
|
if (typeof selected !== "string") return;
|
|
sources = selected;
|
|
}
|
|
if (useProject.getState().project !== project) return;
|
|
const off = useProject.subscribe((state) => {
|
|
if (state.project !== project) void commands.cancelRequest(request);
|
|
});
|
|
try {
|
|
const location = await call(commands.lspLibrarySources(request, path, pos, sources));
|
|
if (useProject.getState().project !== project) return;
|
|
if (!location) {
|
|
useSourceState.setState({ [path]: { message: "Sources not found" } });
|
|
return;
|
|
}
|
|
invalidateVirtualDocument(path);
|
|
if (!editor.openPaths?.().includes(path)) return;
|
|
// Do not take focus back if the user navigated elsewhere during the download.
|
|
if (editor.activePath() !== path) return;
|
|
await editor.openFile(location.path, {
|
|
selection: { line: location.range.start.line, col: location.range.start.col },
|
|
});
|
|
if (editor.activePath() === location.path) await editor.closePath?.(path);
|
|
} finally {
|
|
off();
|
|
}
|
|
} catch (error) {
|
|
if (!isCancelled(error) && useProject.getState().project === project) {
|
|
useSourceState.setState({ [path]: { message: "Cannot load library sources" } });
|
|
notifications().notify({
|
|
level: "error",
|
|
title: "Cannot load library sources",
|
|
body: String(error),
|
|
group: "lsp",
|
|
});
|
|
}
|
|
} finally {
|
|
if (useSourceState.getState()[path]?.request === request) useSourceState.setState({ [path]: {} });
|
|
}
|
|
}
|
|
|
|
export function registerLibrarySourcesActions(): () => void {
|
|
const path = (arg: unknown) => (typeof arg === "string" ? arg : maybeEditorService()?.activePath());
|
|
const clear = () => {
|
|
for (const state of Object.values(useSourceState.getState())) {
|
|
if (state.request !== undefined) void commands.cancelRequest(state.request);
|
|
}
|
|
useSourceState.setState({}, true);
|
|
};
|
|
const offProject = useProject.subscribe((state, previous) => {
|
|
if (state.project !== previous.project) clear();
|
|
});
|
|
// A reload may have replaced a library jar in place (a SNAPSHOT installed again): reopened
|
|
// classes fetch their contents again (the backend refreshes the open ones).
|
|
const offRoots = onIdeEvent("projectRootsChanged", () => invalidateVirtualDocuments());
|
|
const offActions = registerActions([
|
|
{
|
|
id: "editor.downloadSources",
|
|
title: "Download Sources",
|
|
category: "Code",
|
|
hidden: true,
|
|
run: ({ arg }) => {
|
|
const p = path(arg);
|
|
return p ? retrieve(p, false) : undefined;
|
|
},
|
|
},
|
|
{
|
|
id: "editor.chooseSources",
|
|
title: "Choose Sources…",
|
|
category: "Code",
|
|
hidden: true,
|
|
run: ({ arg }) => {
|
|
const p = path(arg);
|
|
return p ? retrieve(p, true) : undefined;
|
|
},
|
|
},
|
|
{
|
|
id: "editor.cancelSourceDownload",
|
|
title: "Cancel",
|
|
category: "Code",
|
|
hidden: true,
|
|
run: async ({ arg }) => {
|
|
const p = path(arg);
|
|
const request = p ? useSourceState.getState()[p]?.request : undefined;
|
|
if (request !== undefined) await commands.cancelRequest(request);
|
|
},
|
|
},
|
|
]);
|
|
return () => {
|
|
offActions();
|
|
offProject();
|
|
offRoots();
|
|
clear();
|
|
};
|
|
}
|
|
|
|
export function useLibrarySources(path: string, decompiled: boolean) {
|
|
const state = useSourceState((s) => s[path]);
|
|
const project = useProject((s) => s.project);
|
|
const [support, setSupport] = useState<{ path: string; project: typeof project; download: boolean } | null>(null);
|
|
const choose = decompiled && !!project?.trusted && (path.startsWith("jdt:") || path.startsWith("jar:"));
|
|
useEffect(() => {
|
|
let current = true;
|
|
if (choose)
|
|
void call(commands.lspCanDownloadSources(path)).then(
|
|
(supported) => {
|
|
if (current) setSupport({ path, project, download: supported });
|
|
},
|
|
() => {},
|
|
);
|
|
return () => {
|
|
current = false;
|
|
};
|
|
}, [path, choose, project]);
|
|
const download = choose && support?.path === path && support.project === project && support.download;
|
|
return { download, choose, busy: state?.request !== undefined, message: state?.message };
|
|
}
|