mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
feat(run): insert path macros into a run configuration's working directory (#4816)
A + button next to the working directory field lists the supported path macros ($USER_HOME$ only on a local project) and inserts the chosen one at the caret.
This commit is contained in:
@@ -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<HTMLInputElement | null>;
|
||||
onInsert: (v: string) => void;
|
||||
}) {
|
||||
const tx = useTx();
|
||||
const ref = useRef<HTMLButtonElement>(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 (
|
||||
<>
|
||||
<IconButton
|
||||
ref={ref}
|
||||
icon="plus"
|
||||
size="small"
|
||||
iconSize={14}
|
||||
title={tx("Insert Macro…")}
|
||||
aria-haspopup="menu"
|
||||
aria-expanded={open}
|
||||
onClick={() => setOpen((o) => !o)}
|
||||
/>
|
||||
<Menu
|
||||
open={open}
|
||||
onClose={() => 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<HTMLInputElement>(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
|
||||
<Field label="Working directory" error={errors.cwd}>
|
||||
<div className={styles.inputWithButton}>
|
||||
<Input
|
||||
ref={cwdInput}
|
||||
mono
|
||||
aria-label="Working directory"
|
||||
value={config.cwd ?? ""}
|
||||
placeholder="Project root"
|
||||
onChange={(e) => update({ cwd: e.target.value.trim() ? e.target.value : null })}
|
||||
/>
|
||||
<CwdMacroButton input={cwdInput} onInsert={(cwd) => update({ cwd })} />
|
||||
<BrowseButton
|
||||
directory
|
||||
title="Browse…"
|
||||
|
||||
@@ -116,6 +116,27 @@ describe("configuration types", () => {
|
||||
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(
|
||||
<ConfigForm
|
||||
config={{ ...config, cwd: "/web" }}
|
||||
all={[]}
|
||||
type={type}
|
||||
errors={{}}
|
||||
onChange={change}
|
||||
modifyRequest={0}
|
||||
/>,
|
||||
);
|
||||
const field = screen.getByLabelText<HTMLInputElement>("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" });
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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": "用户主目录"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user