diff --git a/src/features/run/ConfigForm.tsx b/src/features/run/ConfigForm.tsx index 26b73be7c..338e9cad8 100644 --- a/src/features/run/ConfigForm.tsx +++ b/src/features/run/ConfigForm.tsx @@ -34,6 +34,7 @@ import { import { useContributions, type RunConfigTypeContribution } from "@/workbench/contributions"; import { TASK_LABELS, taskLabel, type BeforeLaunchKind } from "./beforeLaunch"; import { ExpandableTextarea } from "./ExpandableTextarea"; +import { CWD_MACROS } from "./cwdMacros"; import { commandLine, compoundCycle, @@ -245,6 +246,65 @@ function BrowseButton({ ); } +const CWD_MACRO_LABELS: Record<(typeof CWD_MACROS)[number], string> = { + PROJECT_DIR: "Project root", + ContentRoot: "Content root", + MODULE_DIR: "Module directory", + MODULE_WORKING_DIR: "Module working directory", + USER_HOME: "User home directory", +}; + +/** Inserts a path macro at the caret of the working directory field. */ +function CwdMacroButton({ + input, + onInsert, +}: { + input: RefObject; + onInsert: (v: string) => void; +}) { + const tx = useTx(); + const ref = useRef(null); + const [open, setOpen] = useState(false); + const insert = (macro: string) => { + const el = input.current; + const value = el?.value ?? ""; + const from = el?.selectionStart ?? value.length; + const to = el?.selectionEnd ?? from; + onInsert(value.slice(0, from) + macro + value.slice(to)); + requestAnimationFrame(() => { + el?.focus(); + el?.setSelectionRange(from + macro.length, from + macro.length); + }); + }; + return ( + <> + setOpen((o) => !o)} + /> + setOpen(false)} + anchor={ref} + placement="bottom-end" + aria-label={tx("Macros")} + items={CWD_MACROS.filter((m) => m !== "USER_HOME" || !isRemoteWindow()).map((m) => ({ + id: m, + label: `$${m}$`, + shortcut: tx(CWD_MACRO_LABELS[m]), + run: () => insert(`$${m}$`), + }))} + /> + + ); +} + export function ConfigForm({ config, all, type, errors, template, onChange, modifyRequest }: ConfigFormProps) { const openByDefault = useWorkflowSettings((s) => s.run.showOnStart); const externalTools = useWorkflowSettings((s) => s.externalTools); @@ -253,6 +313,7 @@ export function ConfigForm({ config, all, type, errors, template, onChange, modi const tx = useTx(); const txTemplate = useTxTemplate(); const root = useProject((s) => s.project?.root ?? null); + const cwdInput = useRef(null); const gradle = useBuildModel((s) => s.model?.gradle); const runUsing = useProject((s) => s.settings?.languages?.java?.gradle?.runUsing); const delegated = @@ -566,12 +627,14 @@ export function ConfigForm({ config, all, type, errors, template, onChange, modi
update({ cwd: e.target.value.trim() ? e.target.value : null })} /> + update({ cwd })} /> { expect(change.mock.calls.at(-1)?.[0]).toMatchObject({ args: ["-Xmx1g", "-jar", "new.jar", "hello"] }); }); + it("inserts a path macro at the caret of the working directory (#4755)", () => { + const type = typeFor("jar-application")!; + const config = withParams(newConfigOfType(type, []), { jarPath: "app.jar" }); + const change = vi.fn(); + render( + , + ); + const field = screen.getByLabelText("Working directory"); + field.setSelectionRange(0, 0); + fireEvent.click(screen.getByRole("button", { name: "Insert Macro…" })); + fireEvent.click(screen.getByRole("menuitem", { name: /\$PROJECT_DIR\$/ })); + expect(change.mock.calls.at(-1)?.[0]).toMatchObject({ cwd: "$PROJECT_DIR$/web" }); + }); + it("runs an Application with the chosen JRE and the classpath of the chosen module", () => { const type = typeFor("application")!; const base = withParams(newConfigOfType(type, []), { ...newConfigOfType(type, []).params, mainClass: "a.Main" }); diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 5f9454870..c3d8147b1 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -2816,5 +2816,7 @@ "Not a thread count Maven accepts, so builds run serially. Use a number (4) or a core multiplier (2C, 2.5C).": "Not a thread count Maven accepts, so builds run serially. Use a number (4) or a core multiplier (2C, 2.5C).", "Unknown macro {all} in the working directory. Supported: {supported}.": "Unknown macro {all} in the working directory. Supported: {supported}.", "{all} is not available in a remote project.": "{all} is not available in a remote project.", - "Cannot start '{name}'": "Cannot start '{name}'" + "Cannot start '{name}'": "Cannot start '{name}'", + "Module working directory": "Module working directory", + "User home directory": "User home directory" } diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 8b4afeda8..35242512d 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -9175,5 +9175,7 @@ "Not a thread count Maven accepts, so builds run serially. Use a number (4) or a core multiplier (2C, 2.5C).": "Maven 不接受这个线程数,构建将串行执行。请填写数字(4)或核数倍数(2C、2.5C)。", "Unknown macro {all} in the working directory. Supported: {supported}.": "工作目录中有未知的宏 {all}。支持:{supported}。", "{all} is not available in a remote project.": "{all} 在远程项目中不可用。", - "Cannot start '{name}'": "无法启动“{name}”" + "Cannot start '{name}'": "无法启动“{name}”", + "Module working directory": "模块工作目录", + "User home directory": "用户主目录" }