дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
fix(run): gutter run for Java 25 compact source files (#7814)
A Java 25 compact source file (JEP 512: a top-level void main, no class) showed no gutter Run icon, because the run-target scanner only accepted main inside a class. A top-level main is now the main of the file's implicit class, named after the file, and the Java single-file producer launches such files with the source launcher (java File.java) instead of taking the first declared type as the main class. Co-authored-by: kjeldahl <[email protected]>
Цей коміт міститься в:
1 джерело
22db3ab9c3
коміт
45dd96cdeb
4 files changed
+30
-4
No files matched your search
@@ -69,6 +69,16 @@ describe("scanJvmTargets (Java)", () => {
|
||||
expect(pick("class B {\n void main() {}\n}", "B.java")).toEqual([[1, "main", "B", null, false]]);
|
||||
});
|
||||
|
||||
it("a Java 25 compact source file's top-level main runs the file's implicit class", () => {
|
||||
const src = "// Run: java Hi.java\nenum Day { MONDAY }\n\nvoid main() {\n IO.println(Day.MONDAY);\n}\n";
|
||||
expect(pick(src, "examples/Hi.java")).toEqual([[3, "main", "Hi", null, false]]);
|
||||
expect(scanJvmTargets(src, "examples/Hi.java", false)[0]).toMatchObject({ instance: true, compact: true });
|
||||
expect(pick("static void main(String[] args) {}\nvoid helper() {}", "Tool.java")).toEqual([
|
||||
[0, "main", "Tool", null, false],
|
||||
]);
|
||||
expect(pick("void helper() {}\nint main(String[] a) { return 0; }", "Tool.java")).toEqual([]);
|
||||
});
|
||||
|
||||
it("finds JUnit tests, nested @Nested classes and multi-line annotations", () => {
|
||||
const src = [
|
||||
"package a.b;", // 0
|
||||
|
||||
@@ -19,6 +19,8 @@ export interface JvmTarget {
|
||||
spring?: boolean;
|
||||
/** Java 25 instance `main` (JEP 512): a subclass cannot inherit it on older JDKs. */
|
||||
instance?: boolean;
|
||||
/** Java 25 compact source file: a top-level `main` of the file's implicit class (JEP 512). */
|
||||
compact?: boolean;
|
||||
}
|
||||
|
||||
const BOOT_ANNOTATIONS = ["SpringBootApplication", "SpringCloudApplication"];
|
||||
@@ -310,6 +312,16 @@ function scan(src: string, relPath: string, kotlin: boolean) {
|
||||
instance: !isStatic || undefined,
|
||||
});
|
||||
}
|
||||
} else if (!kotlin && !owner && prev === "void" && stringArgs) {
|
||||
// Compact source file: the implicit class is named after the file, in the unnamed package.
|
||||
const stem = (relPath.split("/").pop() ?? relPath).replace(/\.java$/, "");
|
||||
out.push({
|
||||
line: tokens[i]!.line,
|
||||
kind: "main",
|
||||
className: stem,
|
||||
instance: !modifiers.includes("static") || undefined,
|
||||
compact: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
annotations = [];
|
||||
|
||||
@@ -655,6 +655,10 @@ describe("single-file producers (scratches)", () => {
|
||||
command: "/bin/java",
|
||||
args: ["Hi.java"],
|
||||
});
|
||||
// A type declared before the top-level main is a member of the implicit class, not the launch class.
|
||||
const compact = scratch("Hi.java", "enum Day { MONDAY }\n\nvoid main() {\n IO.println(Day.MONDAY);\n}\n");
|
||||
expect(run(compact)).toMatchObject({ command: "/bin/java", args: ["Hi.java"] });
|
||||
expect(run({ ...compact, line: 2 })).toMatchObject({ command: "/bin/java", args: ["Hi.java"] });
|
||||
});
|
||||
|
||||
it("Java: the gutter runs the main class it marks, nested ones and those of a JDK on Windows too", () => {
|
||||
|
||||
@@ -747,11 +747,11 @@ function javaFile(input: ContextInput, env: ToolEnv, s: FileSite, mainClass?: st
|
||||
config: fileConfig(s, "java-file", java.command, [s.arg], "java-debug"),
|
||||
};
|
||||
if (input.content === undefined) return source;
|
||||
const first = firstTopLevelType(input.content);
|
||||
const mains = scanJvmTargets(input.content, s.idPath, false)
|
||||
.filter((t) => t.kind === "main")
|
||||
.map((t) => t.className);
|
||||
const targets = scanJvmTargets(input.content, s.idPath, false).filter((t) => t.kind === "main");
|
||||
const mains = targets.map((t) => t.className);
|
||||
const stem = stemOf(s.file);
|
||||
// A compact source file's implicit class is the one the source launcher runs.
|
||||
const first = targets.some((t) => t.compact) ? stem : firstTopLevelType(input.content);
|
||||
const fileMain =
|
||||
mains.find((c) => c === first) ?? mains.find((c) => c.slice(c.lastIndexOf(".") + 1) === stem) ?? mains[0];
|
||||
const launch = mainClass ?? fileMain;
|
||||
|
||||
Reference in new issue
Заблокувати користувача