дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 09:11:10 +02:00
feat(plugins): a shortcut-run command gets the focused view's selected item (#6160)
A plugin command run without an argument (its shortcut, Find Action) while one of the plugin's views has the keyboard focus gets the selected item's key, the same argument as from that item's viewItem.context menu. Only commands the item's menu offers (with its when clause) get it; nothing selected or focus elsewhere gives none. Fixes #4759
Цей коміт міститься в:
1 джерело
b30716e22c
коміт
aa1a027162
6 files changed
+118
-7
No files matched your search
@@ -152,7 +152,9 @@ double click and Enter with `event.table_activate()` (API 1.3, absolute 0-based
|
||||
Bodies, fields and events added by later APIs travel as `Other { kind, data }`: an IDE that
|
||||
doesn't know a body says it needs updating, and leaves out a field type it doesn't know.
|
||||
Item right-click menus use `viewItem.context` (see
|
||||
[Commands and actions](/en/plugins/guides/commands#menus)).
|
||||
[Commands and actions](/en/plugins/guides/commands#menus)). A command that menu offers for the selected item,
|
||||
run from its shortcut (or Find Action) while the view has the keyboard focus, gets that
|
||||
item's key as its argument, as from the menu; none when nothing is selected.
|
||||
|
||||
## Custom editors
|
||||
|
||||
|
||||
@@ -136,7 +136,7 @@ register_plugin!(TodoFinder, views);
|
||||
双击和回车用 `event.table_activate()`(API 1.3,从 0 开始的绝对行号)。
|
||||
|
||||
后续 API 新增的主体、字段和事件以 `Other { kind, data }` 传递:IDE 不认识某种主体时会提示需要更新,
|
||||
不认识的字段类型则直接省略。条目右键菜单使用 `viewItem.context`(见 [命令与动作](/zh/plugins/guides/commands) 中的“菜单”一节)。
|
||||
不认识的字段类型则直接省略。条目右键菜单使用 `viewItem.context`(见 [命令与动作](/zh/plugins/guides/commands) 中的“菜单”一节)。视图有键盘焦点时,用快捷键(或查找动作)运行选中条目的右键菜单里有的命令,命令参数是该条目的 key,与从菜单运行相同;未选中条目时没有参数。
|
||||
|
||||
## 自定义编辑器
|
||||
|
||||
|
||||
@@ -170,7 +170,11 @@ its first `:`, so `pod` for `pod:default/nginx`; the whole key when it has no
|
||||
`:`). Only the menu's `when` sees them, not the command's own `when`; a command
|
||||
whose `when` is false shows greyed out. Other menus refuse `view`, a web view
|
||||
has no item menu, and no menu opens when no declaration matches the item.
|
||||
The menu is new in API 1.2, so a plugin using it declares
|
||||
A command run without an argument (its shortcut, Find Action) while one of the
|
||||
plugin's views has the keyboard focus gets the selected item's key too, when
|
||||
that item's `viewItem.context` menu offers the command: a shortcut acts on the
|
||||
selected item like the menu entry. With nothing selected, the focus elsewhere or
|
||||
the command not in the item's menu, it gets none. The menu is new in API 1.2, so a plugin using it declares
|
||||
`engines.openidea = "^1.2"`. For example:
|
||||
|
||||
```toml
|
||||
|
||||
@@ -3,7 +3,7 @@ import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
||||
import userEvent from "@testing-library/user-event";
|
||||
import { ContextMenuHost, hideContextMenu } from "@/ui";
|
||||
import type { View, ViewEvent } from "@/bindings";
|
||||
import { registerAction } from "@/platform/actions";
|
||||
import { executeAction, registerAction } from "@/platform/actions";
|
||||
import { bindingsFor, effectiveBindings, registerExtensionBindings, useKeymap } from "@/platform/keymap";
|
||||
import { createMemoryPluginsBackend, setPluginsBackend, type PluginsBackend } from "../backend";
|
||||
import { commandArgs, defineBuiltinUiPoints } from "../points";
|
||||
@@ -11,7 +11,7 @@ import { clearUiContributions, syncUiContributions } from "../projection";
|
||||
import { pluginMenuItems, pluginViewItemMenuItems, withPluginSetting } from "../uiPoints";
|
||||
import { matchesPattern } from "../views/customEditor";
|
||||
import { FRAME_CSP, FRAME_TAG, frameDocument, frameTheme, type FrameTheme } from "../views/frame";
|
||||
import { PluginView, treeNodes, wantedViewport } from "../views/PluginView";
|
||||
import { focusedViewItem, PluginView, treeNodes, wantedViewport } from "../views/PluginView";
|
||||
import { bumpView } from "../views/viewStore";
|
||||
|
||||
let undefinePoints: () => void = () => {};
|
||||
@@ -341,6 +341,70 @@ describe("PluginView", () => {
|
||||
await waitFor(() => expect(events).toContainEqual({ kind: "button", id: "refresh" }));
|
||||
});
|
||||
|
||||
it("gives a shortcut-run command the selected item of the plugin's focused view", async () => {
|
||||
const item = (key: string, label: string) => ({
|
||||
key,
|
||||
parent: null,
|
||||
label,
|
||||
icon: null,
|
||||
detail: null,
|
||||
expandable: false,
|
||||
expanded: false,
|
||||
});
|
||||
const tree: View = {
|
||||
toolbar: [],
|
||||
body: { kind: "tree", value: [item("task:build", "build"), item("task:test", "test")] },
|
||||
status: null,
|
||||
};
|
||||
const backend = backendWith(() => tree);
|
||||
const runCommand = vi.fn(async () => null);
|
||||
setPluginsBackend({ ...backend, runCommand });
|
||||
syncUiContributions(
|
||||
[
|
||||
{ pluginId: "jobs", point: "commands", key: "open", decl: { id: "open", title: "Open Source" } },
|
||||
{ pluginId: "jobs", point: "commands", key: "sync", decl: { id: "sync", title: "Sync" } },
|
||||
{
|
||||
pluginId: "jobs",
|
||||
point: "menus",
|
||||
key: "viewItem.context/tasks/open",
|
||||
decl: { menu: "viewItem.context", view: "tasks", command: "open", when: "viewItemKind == task" },
|
||||
},
|
||||
],
|
||||
[
|
||||
{ id: "jobs", name: "Jobs", status: "active", enabled: true } as unknown as Parameters<
|
||||
typeof syncUiContributions
|
||||
>[1][number],
|
||||
],
|
||||
);
|
||||
const user = userEvent.setup();
|
||||
render(
|
||||
<PluginView
|
||||
pluginId="jobs"
|
||||
view="tasks"
|
||||
label="Tasks"
|
||||
itemMenu={(key) => pluginViewItemMenuItems("jobs", "tasks", key)}
|
||||
/>,
|
||||
);
|
||||
expect(focusedViewItem("jobs", "plugin.jobs.open")).toBeNull();
|
||||
await user.click(await screen.findByText("test"));
|
||||
await waitFor(() => expect(focusedViewItem("jobs", "plugin.jobs.open")).toBe("task:test"));
|
||||
expect(focusedViewItem("other", "plugin.other.open")).toBeNull();
|
||||
// Only a command the item's menu offers gets the item.
|
||||
expect(focusedViewItem("jobs", "plugin.jobs.sync")).toBeNull();
|
||||
await executeAction("plugin.jobs.sync");
|
||||
expect(runCommand).toHaveBeenLastCalledWith("jobs", "sync", null, null, []);
|
||||
await executeAction("plugin.jobs.open");
|
||||
expect(runCommand).toHaveBeenLastCalledWith("jobs", "open", "task:test", null, []);
|
||||
// A menu's own argument wins over the focused view.
|
||||
await executeAction("plugin.jobs.open", "task:build");
|
||||
expect(runCommand).toHaveBeenLastCalledWith("jobs", "open", "task:build", null, []);
|
||||
// Focus elsewhere: nothing selected for the command.
|
||||
(document.activeElement as HTMLElement | null)?.blur();
|
||||
expect(focusedViewItem("jobs", "plugin.jobs.open")).toBeNull();
|
||||
await executeAction("plugin.jobs.open");
|
||||
expect(runCommand).toHaveBeenLastCalledWith("jobs", "open", null, null, []);
|
||||
});
|
||||
|
||||
it("opens the item menu on right-click", async () => {
|
||||
const tree: View = {
|
||||
toolbar: [],
|
||||
|
||||
@@ -18,6 +18,7 @@ import { SecretsDetails, type SecretDecl } from "./secrets";
|
||||
import { pluginSettingsPageId } from "./settings";
|
||||
import { pluginsBackend } from "./backend";
|
||||
import { isLive, runPluginCommand, usePlugins } from "./store";
|
||||
import { focusedViewItem } from "./views/PluginView";
|
||||
import styles from "./PluginsPage.module.css";
|
||||
|
||||
/** Unpacks an action argument: a bare path, or a menu's `{ path, paths }`. */
|
||||
@@ -244,7 +245,8 @@ export function defineCoreUiPoints(): () => void {
|
||||
keywords: ["plugin", plugin.name, pluginId],
|
||||
description: tx`From plugin ${plugin.name}`,
|
||||
run: async ({ arg }: { arg?: unknown }) => {
|
||||
const [path, paths] = commandArgs(arg);
|
||||
// A shortcut passes nothing: the plugin's focused view supplies its selected item.
|
||||
const [path, paths] = commandArgs(arg ?? focusedViewItem(pluginId, action.id));
|
||||
await runPluginCommand(pluginId, decl.id, path, paths);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -62,6 +62,34 @@ export interface PluginViewProps {
|
||||
const OVERSCAN = 50;
|
||||
const ROW_HEIGHT = 24;
|
||||
|
||||
/** Each rendered view root → its item menu, read when a command runs from a shortcut. */
|
||||
const viewMenus = new WeakMap<Element, (key: string) => MenuItem[]>();
|
||||
|
||||
const offers = (items: MenuItem[], action: string): boolean =>
|
||||
items.some((i) =>
|
||||
i.kind === "action"
|
||||
? i.action === action
|
||||
: (i.kind ?? "item") === "item" && offers(("submenu" in i && i.submenu) || [], action),
|
||||
);
|
||||
|
||||
/**
|
||||
* The argument a plugin command run without one (a shortcut, Find Action) gets from the plugin's
|
||||
* view that has the keyboard focus: the selected item's key, when that item's context menu offers
|
||||
* the command — the same argument as from the menu. Null when the focus is elsewhere, in another
|
||||
* plugin's view, nothing is selected, or the menu doesn't offer the command there.
|
||||
*/
|
||||
export function focusedViewItem(
|
||||
pluginId: string,
|
||||
action: string,
|
||||
focused: Element | null = document.activeElement,
|
||||
): string | null {
|
||||
const view = focused?.closest<HTMLElement>("[data-plugin-view]");
|
||||
const key = view?.dataset.selectedKey;
|
||||
if (!view || view.dataset.pluginView !== pluginId || key === undefined) return null;
|
||||
const menu = viewMenus.get(view);
|
||||
return menu && offers(menu(key), action) ? key : null;
|
||||
}
|
||||
|
||||
/** Plugin text is shown as written: the plugin translates it through its own catalog. */
|
||||
export function PluginView(props: PluginViewProps) {
|
||||
const location = useViewLocation();
|
||||
@@ -154,8 +182,19 @@ function PluginViewContent({
|
||||
</div>
|
||||
);
|
||||
|
||||
// The item a keyboard-invoked command of this plugin gets, as from the item's context menu
|
||||
// (`focusedViewItem`); only a key the view still shows.
|
||||
const currentKey = selection && items?.some((i) => i.key === selection.key) ? selection.key : undefined;
|
||||
return (
|
||||
<div className={styles.view} aria-label={label}>
|
||||
<div
|
||||
ref={(el) => {
|
||||
if (el && itemMenu) viewMenus.set(el, itemMenu);
|
||||
}}
|
||||
className={styles.view}
|
||||
aria-label={label}
|
||||
data-plugin-view={pluginId}
|
||||
data-selected-key={currentKey}
|
||||
>
|
||||
{content.toolbar.length > 0 && (
|
||||
<div className={styles.toolbar} role="toolbar">
|
||||
{content.toolbar.map((b) =>
|
||||
|
||||
Reference in new issue
Заблокувати користувача