mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
fix(search): Find Action hides actions that cannot run (#4327)
Find Action only checked an action's when clause, not its enabled() check, so Annotate with Git Blame on an untracked file, Show Current Revision, or Set NULL with no data grid focused were listed as enabled and did nothing. Matching now uses isActionEnabled, the same check menus and shortcuts use. When Include disabled actions is unchecked, unavailable actions are hidden, also from the recent list. The database cell actions are enabled only while a grid has focus (a popup opened from the grid keeps the grid it captured). An empty Actions tab says Nothing found. Closes #4223
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { installKeyboardDispatcher, useKeymap } from "@/platform/keymap";
|
||||
import { registerActions } from "@/platform/actions";
|
||||
import { executeAction, getAction, isActionEnabled, registerActions } from "@/platform/actions";
|
||||
import { getContext } from "@/platform/context";
|
||||
import { act, fireEvent, render, screen } from "@testing-library/react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { useState } from "react";
|
||||
@@ -699,6 +700,33 @@ describe("Set NULL / Set DEFAULT (issue #2331, phase 1)", () => {
|
||||
{ name: "c", type: "text", default: "now()" },
|
||||
];
|
||||
|
||||
it("keeps the invoking grid available to a popup using its captured focus context", async () => {
|
||||
const onEdit = vi.fn();
|
||||
render(
|
||||
<>
|
||||
<Grid columns={[columns[0]!]} rowCount={1} value={() => "old"} editable onEdit={onEdit} />
|
||||
<div role="dialog">
|
||||
<input aria-label="Action query" />
|
||||
</div>
|
||||
<button>Outside the grid</button>
|
||||
</>,
|
||||
);
|
||||
fireEvent.mouseDown(screen.getByRole("gridcell"), { button: 0 });
|
||||
const source = { ...getContext() };
|
||||
const action = getAction("db.setCellNull")!;
|
||||
expect(isActionEnabled(action, source)).toBe(true);
|
||||
|
||||
act(() => screen.getByLabelText("Action query").focus());
|
||||
expect(isActionEnabled(action)).toBe(false);
|
||||
expect(isActionEnabled(action, source)).toBe(true);
|
||||
act(() => screen.getByRole("grid").focus());
|
||||
await act(() => executeAction(action.id));
|
||||
expect(onEdit).toHaveBeenCalledWith(0, 0, null);
|
||||
|
||||
act(() => screen.getByRole("button", { name: "Outside the grid" }).focus());
|
||||
expect(isActionEnabled(action, source)).toBe(false);
|
||||
});
|
||||
|
||||
it("sets NULL on the selection from the context menu and ⌘⌫, skipping NOT NULL columns", async () => {
|
||||
const onEdit = vi.fn();
|
||||
render(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { registerActions, type ActionContext } from "@/platform/actions";
|
||||
import { setContext } from "@/platform/context";
|
||||
import { getContext, setContext } from "@/platform/context";
|
||||
|
||||
export type CellCopy = "tsv" | "csv" | "onerow" | "header" | "rowsTsv" | "rowsCsv";
|
||||
|
||||
@@ -26,7 +26,7 @@ let active: CellActions | null = null;
|
||||
|
||||
/** The focused grid (not while a text field in it has focus). */
|
||||
export function activeCellGrid(): CellActions | null {
|
||||
return active;
|
||||
return getContext().dbCellGridFocus ? active : null;
|
||||
}
|
||||
|
||||
const COPIES: Array<[CellCopy, string, string]> = [
|
||||
@@ -53,12 +53,18 @@ export function installCellActions(): () => void {
|
||||
const onFocus = (event: FocusEvent) => {
|
||||
const target = event.target instanceof Element ? event.target : null;
|
||||
const root = target?.closest("[data-db-cell-grid]");
|
||||
// A popup can query the grid that opened it with its captured context.
|
||||
if (!root && target?.closest("[data-popup], [role=dialog]")) {
|
||||
setContext("dbCellGridFocus", false);
|
||||
return;
|
||||
}
|
||||
active =
|
||||
root && !target?.closest("input, textarea, select, [contenteditable='true']") ? (grids.get(root) ?? null) : null;
|
||||
setContext("dbCellGridFocus", !!active);
|
||||
};
|
||||
document.addEventListener("focusin", onFocus);
|
||||
const handle = ({ arg }: ActionContext) => (arg as CellActions | undefined) ?? active;
|
||||
const handle = ({ arg, ctx }: ActionContext) =>
|
||||
(arg as CellActions | undefined) ?? (ctx.dbCellGridFocus ? active : null);
|
||||
const dispose = registerActions([
|
||||
{
|
||||
id: "db.toggleBooleanCell",
|
||||
|
||||
@@ -39,6 +39,28 @@ function setup(overrides: Partial<SeDeps> = {}) {
|
||||
const fm = (rel: string) => ({ path: `/p/${rel}`, relPath: rel, score: 1, indices: [] });
|
||||
|
||||
describe("SeController", () => {
|
||||
it("filters recent actions when disabled actions are excluded", async () => {
|
||||
const { c, last } = setup({
|
||||
actions: () => [
|
||||
{ id: "blocked", title: "Blocked", run: () => {} },
|
||||
{ id: "available", title: "Available", run: () => {} },
|
||||
],
|
||||
isEnabled: (a) => a.id === "available",
|
||||
recentActions: () => ["blocked", "available"],
|
||||
});
|
||||
const recent = () => last().sections.get("recentActions")!.items;
|
||||
c.run({ query: "", tab: "actions", includeNonProject: false, includeDisabled: false, extra: {} });
|
||||
await flush();
|
||||
expect(recent()).toMatchObject([{ id: "available", enabled: true }]);
|
||||
|
||||
c.run({ query: "", tab: "actions", includeNonProject: true, includeDisabled: true, extra: {} });
|
||||
await flush();
|
||||
expect(recent()).toMatchObject([
|
||||
{ id: "blocked", enabled: false },
|
||||
{ id: "available", enabled: true },
|
||||
]);
|
||||
});
|
||||
|
||||
it("streams each section independently", async () => {
|
||||
const files = deferred<ReturnType<typeof fm>[]>();
|
||||
const { c, last } = setup({
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { act, fireEvent, render, screen, within } from "@testing-library/react";
|
||||
import { registerAction } from "@/platform/actions";
|
||||
import { clearContext, getContext, setContext, type ContextKeys } from "@/platform/context";
|
||||
import type { SeDeps } from "../everywhere/controller";
|
||||
|
||||
const defaultFindFiles: SeDeps["findFiles"] = async (q) =>
|
||||
@@ -28,13 +29,16 @@ vi.mock("../common/open", async (orig) => ({
|
||||
openLocation: vi.fn(async () => {}),
|
||||
}));
|
||||
|
||||
vi.mock("../everywhere/deps", () => ({
|
||||
ipcDeps: () => fakeDeps(),
|
||||
loadLanguages: async () => [
|
||||
{ id: "kotlin", name: "Kotlin", extensions: ["kt"] },
|
||||
{ id: "markdown", name: "Markdown", extensions: ["md"] },
|
||||
],
|
||||
}));
|
||||
vi.mock("../everywhere/deps", async (orig) => {
|
||||
const { ipcDeps } = await orig<typeof import("../everywhere/deps")>();
|
||||
return {
|
||||
ipcDeps: (ctx: ContextKeys) => ({ ...fakeDeps(), isEnabled: ipcDeps(ctx).isEnabled }),
|
||||
loadLanguages: async () => [
|
||||
{ id: "kotlin", name: "Kotlin", extensions: ["kt"] },
|
||||
{ id: "markdown", name: "Markdown", extensions: ["md"] },
|
||||
],
|
||||
};
|
||||
});
|
||||
vi.mock("../find/windowStore", async (orig) => ({
|
||||
...(await orig<typeof import("../find/windowStore")>()),
|
||||
ipcFindDeps: () => ({
|
||||
@@ -219,6 +223,73 @@ describe("Search Everywhere dialog", () => {
|
||||
disposers.forEach((d) => d());
|
||||
});
|
||||
|
||||
it("hides unavailable commands until requested and uses the editor's captured context for available ones", async () => {
|
||||
const unavailable = vi.fn();
|
||||
const available = vi.fn();
|
||||
const disposers = [
|
||||
registerAction({
|
||||
id: "test.cellNull",
|
||||
title: "Set Test Cell NULL",
|
||||
when: "testEditorFocus",
|
||||
enabled: () => false,
|
||||
run: unavailable,
|
||||
}),
|
||||
registerAction({
|
||||
id: "test.cellDefault",
|
||||
title: "Set Test Cell Default",
|
||||
when: "testEditorFocus",
|
||||
enabled: ({ ctx }) => ctx.testEditorFocus === true && !ctx.popupOpen,
|
||||
run: available,
|
||||
}),
|
||||
];
|
||||
try {
|
||||
render(
|
||||
<>
|
||||
<button
|
||||
onFocus={() => setContext("testEditorFocus", true)}
|
||||
onBlur={() => setContext("testEditorFocus", false)}
|
||||
>
|
||||
Test editor
|
||||
</button>
|
||||
<SearchEverywhereOverlay />
|
||||
</>,
|
||||
);
|
||||
act(() => screen.getByRole("button", { name: "Test editor" }).focus());
|
||||
act(() => showSearchEverywhere("actions", { query: "Set Test Cell NULL" }));
|
||||
await tick();
|
||||
expect(getContext().testEditorFocus).toBe(false);
|
||||
const checkbox = screen.getByLabelText("Include disabled actions");
|
||||
const input = screen.getByLabelText("Search query");
|
||||
const nullAction = () => screen.queryByRole("option", { name: /Set Test Cell NULL/ });
|
||||
expect(checkbox).not.toBeChecked();
|
||||
expect(nullAction()).toBeNull();
|
||||
expect(screen.getByText("Nothing found", { exact: true })).toBeInTheDocument();
|
||||
|
||||
fireEvent.click(checkbox);
|
||||
await tick();
|
||||
expect(checkbox).toBeChecked();
|
||||
expect(nullAction()?.querySelector('[aria-disabled="true"]')).toBeInTheDocument();
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
expect(unavailable).not.toHaveBeenCalled();
|
||||
expect(useSearchEverywhere.getState().open).toBe(true);
|
||||
|
||||
fireEvent.click(checkbox);
|
||||
await tick();
|
||||
expect(nullAction()).toBeNull();
|
||||
fireEvent.change(input, { target: { value: "Set Test Cell Default" } });
|
||||
await tick();
|
||||
const action = screen.getByRole("option", { name: /Set Test Cell Default/ });
|
||||
expect(action.querySelector('[aria-disabled="true"]')).toBeNull();
|
||||
fireEvent.keyDown(input, { key: "Enter" });
|
||||
await tick();
|
||||
expect(available).toHaveBeenCalledOnce();
|
||||
expect(useSearchEverywhere.getState().open).toBe(false);
|
||||
} finally {
|
||||
disposers.forEach((dispose) => dispose());
|
||||
clearContext("testEditorFocus");
|
||||
}
|
||||
});
|
||||
|
||||
it("filters Files by file type from the funnel, remembering the choice", async () => {
|
||||
render(<SearchEverywhereOverlay />);
|
||||
act(() => showSearchEverywhere("files", { query: "order" }));
|
||||
|
||||
@@ -441,6 +441,7 @@ function SearchEverywhereDialog() {
|
||||
// Nothing in the project: retry everywhere (IDEA), unless the user picked the scope.
|
||||
const nothingInProject =
|
||||
!!showEmpty &&
|
||||
tab !== "actions" &&
|
||||
!emptyQuery &&
|
||||
!listing &&
|
||||
!includeNonProject &&
|
||||
@@ -493,7 +494,13 @@ function SearchEverywhereDialog() {
|
||||
)}
|
||||
{showEmpty && (
|
||||
<div className={styles.empty}>
|
||||
{tx(emptyQuery ? "Type to search" : includeNonProject ? "Nothing found" : "Nothing found in project files")}
|
||||
{tx(
|
||||
emptyQuery
|
||||
? "Type to search"
|
||||
: includeNonProject || tab === "actions"
|
||||
? "Nothing found"
|
||||
: "Nothing found in project files",
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -253,8 +253,9 @@ export class SeController {
|
||||
.recentActions()
|
||||
.map((aid) => byId.get(aid))
|
||||
.filter((a): a is Action => !!a && !a.hidden)
|
||||
.slice(0, limit)
|
||||
.map((a) => actionItem(a, this.deps.isEnabled(a)));
|
||||
.map((a) => actionItem(a, this.deps.isEnabled(a)))
|
||||
.filter((a) => a.enabled || req.includeDisabled !== false)
|
||||
.slice(0, limit);
|
||||
this.set(id, gen, items, false, false);
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { allActions, type Action } from "@/platform/actions";
|
||||
import { evaluateWhen, type ContextKeys } from "@/platform/context";
|
||||
import { allActions, isActionEnabled } from "@/platform/actions";
|
||||
import type { ContextKeys } from "@/platform/context";
|
||||
import { call, commands, nextRequestId, type LanguageInfo } from "@/platform/ipc";
|
||||
import { inFileSet } from "@/platform/projectRoots";
|
||||
import { searchEndpoints, searchableSettings, useProjectViewRoots } from "@/workbench/contributions";
|
||||
@@ -25,9 +25,9 @@ export function ipcDeps(ctx: ContextKeys): SeDeps {
|
||||
cancel: cancelRequest,
|
||||
nextRequestId,
|
||||
actions: () => allActions(),
|
||||
isEnabled: (a: Action) => {
|
||||
isEnabled: (a) => {
|
||||
try {
|
||||
return evaluateWhen(a.when, ctx);
|
||||
return isActionEnabled(a, ctx);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -311,8 +311,6 @@ export function matchActions(
|
||||
const recentRank = new Map(opts.recent.map((id, i) => [id, i]));
|
||||
for (const a of actions) {
|
||||
if (a.hidden) continue;
|
||||
const enabled = opts.isEnabled(a);
|
||||
if (!enabled && opts.includeDisabled === false) continue;
|
||||
let score: number;
|
||||
let indices: number[] = [];
|
||||
const translated = shown(a, a.title);
|
||||
@@ -333,6 +331,8 @@ export function matchActions(
|
||||
score = Math.max(best, idHit) - 400;
|
||||
if (!Number.isFinite(score)) continue;
|
||||
}
|
||||
const enabled = opts.isEnabled(a);
|
||||
if (!enabled && opts.includeDisabled === false) continue;
|
||||
if (!enabled) score -= 150;
|
||||
const r = recentRank.get(a.id);
|
||||
if (r !== undefined) score += 40 - Math.min(30, r * 3);
|
||||
|
||||
Reference in New Issue
Block a user