feat(build): add Build › Recompile for a single Java file (#5587)

Build › Recompile '<file>.java' (Shift+Cmd+F9 / Ctrl+Shift+F9, moved from Build Module, which keeps no default shortcut) compiles only the active Java file with javac and streams its output and diagnostics to the Build tool window. The menu, editor, tab and Project view entries name the file and are enabled only for local .java files.

- Maven modules resolve the classpath of the file's scope (compile for main sources, test for test sources) through dependency:build-classpath; the probe is now cancellable.
- Gradle projects get the source set's output, compile classpath, source folders and release from an init script.
- Projects without a build tool compile against their Project Structure roots, outputs and libraries.
- In a workspace the file's member project is used.
- javac gets -g, omits an empty -cp, and uses -source/-target on JDK 8; localized javac error and warning prefixes are parsed.

Co-authored-by: RuyimgByCN <[email protected]>
Цей коміт міститься в:
RuyimgByCN authored and GitHub committed 2026-11-25 00:59:59 +08:00
1 джерело d729ae8122
коміт 748e5cee89
25 files changed
+1038 -150

No files matched your search

+39
Переглянути файл
@@ -1,6 +1,7 @@
import { act, fireEvent, render, screen, within } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { commands, type Settings } from "@/platform/ipc";
import { clearContext, setContext } from "@/platform/context";
import { useProject } from "@/platform/project";
import { getAction, registerActions } from "@/platform/actions";
import { usePlatform } from "@/platform/platformInfo";
@@ -362,3 +363,41 @@ it.each(["windows", "linux"] as const)("renders toggle and widget checks in the
usePlatform.setState(platform);
}
});
it("shows Recompile '<file>.java' when a Java file is active and disabled Recompile otherwise", async () => {
const off = registerActions([
{
id: "build.compileFile",
title: "Recompile",
when: "projectOpen",
enabled: ({ ctx }) => typeof ctx.editorPath === "string" && ctx.editorPath.endsWith(".java"),
run: () => {},
},
]);
vi.mocked(commands.menuModel).mockResolvedValue([
{
kind: "submenu",
id: "build",
label: "Build",
children: [{ kind: "action", id: "build.compileFile", label: "Recompile", hint: null, arg: null, checked: null }],
},
]);
setContext("projectOpen", true);
setContext("editorPath", "/p/src/Main.java");
setStyle("merged");
try {
render(<MainMenu />);
await screen.findByRole("menubar");
fireEvent.click(titles().getByRole("menuitem", { name: "Build" }));
const activeItem = await screen.findByRole("menuitem", { name: /Recompile 'Main\.java'/ });
expect(activeItem).not.toHaveAttribute("aria-disabled", "true");
act(() => setContext("editorPath", "/p/pom.xml"));
const disabledItem = await screen.findByRole("menuitem", { name: /^Recompile/ });
expect(disabledItem).toHaveAttribute("aria-disabled", "true");
} finally {
off();
clearContext("projectOpen");
clearContext("editorPath");
}
});
+4 -7
Переглянути файл
@@ -1,6 +1,7 @@
/** Main menu for the frameless title bar, with hamburger and persistent title modes. */
import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent } from "react";
import clsx from "clsx";
import { useContextKeys } from "@/platform/context";
import { useProject } from "@/platform/project";
import { lookup, t, useI18n } from "@/platform/i18n";
import { commands, type MenuNode } from "@/platform/ipc";
@@ -8,18 +9,13 @@ import { usePlatform } from "@/platform/platformInfo";
import { Icon, MenuList, Popup, type MenuItem } from "@/ui";
import { useChromeIconSize } from "@/ui/density";
import menuStyles from "@/ui/Menu.module.css";
import { isDistractionFree } from "./distractionFree";
import { toolWindowsRestorable } from "./toolwindows/store";
import { javaFileNameOf, menuLabelParams } from "./menuLabels";
import styles from "./TitleBar.module.css";
function label(id: string, fallback: string): string {
const key = `menu.${id}`;
if (lookup(key) === undefined) return fallback;
return t(key, {
os: usePlatform.getState().os,
distractionFree: isDistractionFree() ? "on" : "off",
toolWindows: toolWindowsRestorable() ? "hidden" : "shown",
});
return t(key, menuLabelParams(usePlatform.getState().os));
}
type Submenu = Extract<MenuNode, { kind: "submenu" }>;
@@ -102,6 +98,7 @@ export function MainMenu({ row = false, ...props }: MainMenuProps) {
function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged: boolean; row: boolean }) {
useI18n((s) => s.locale);
useContextKeys((s) => javaFileNameOf(s.editorPath));
const iconSize = useChromeIconSize();
const [menus, setMenus] = useState<Submenu[]>([]);
const [expanded, setExpanded] = useState(false);
+23
Переглянути файл
@@ -0,0 +1,23 @@
/** Parameters shared between the native menu bridge and the custom title bar menu. */
import { getContext } from "@/platform/context";
import { basename } from "@/platform/project";
import { isVirtualUri } from "@/platform/virtualDocs";
import { maybeEditorService } from "./contributions";
import { isDistractionFree } from "./distractionFree";
import { toolWindowsRestorable } from "./toolwindows/store";
export function javaFileNameOf(path: unknown): string | null {
if (typeof path !== "string" || !path || isVirtualUri(path) || !/\.java$/i.test(path)) return null;
return basename(path) || null;
}
export function menuLabelParams(os: string) {
const compileFileName = javaFileNameOf(getContext().editorPath ?? maybeEditorService()?.activePath());
return {
os,
distractionFree: isDistractionFree() ? "on" : "off",
toolWindows: toolWindowsRestorable() ? "hidden" : "shown",
compileFile: compileFileName ? "yes" : "no",
compileFileName: compileFileName ?? "",
};
}