diff --git a/src/features/database/Grid.test.tsx b/src/features/database/Grid.test.tsx index 9d76c41af..3615906f3 100644 --- a/src/features/database/Grid.test.tsx +++ b/src/features/database/Grid.test.tsx @@ -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( + <> + "old"} editable onEdit={onEdit} /> +
+ +
+ + , + ); + 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( diff --git a/src/features/database/cellActions.ts b/src/features/database/cellActions.ts index 9cfc1984f..e6ce768e3 100644 --- a/src/features/database/cellActions.ts +++ b/src/features/database/cellActions.ts @@ -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", diff --git a/src/features/search/__tests__/controller.test.ts b/src/features/search/__tests__/controller.test.ts index 147687ec9..057012bb9 100644 --- a/src/features/search/__tests__/controller.test.ts +++ b/src/features/search/__tests__/controller.test.ts @@ -39,6 +39,28 @@ function setup(overrides: Partial = {}) { 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[]>(); const { c, last } = setup({ diff --git a/src/features/search/__tests__/dialogs.test.tsx b/src/features/search/__tests__/dialogs.test.tsx index 8835068d2..6e20a28c6 100644 --- a/src/features/search/__tests__/dialogs.test.tsx +++ b/src/features/search/__tests__/dialogs.test.tsx @@ -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(); + 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()), 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( + <> + + + , + ); + 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(); act(() => showSearchEverywhere("files", { query: "order" })); diff --git a/src/features/search/everywhere/SearchEverywhere.tsx b/src/features/search/everywhere/SearchEverywhere.tsx index 2bbcce96a..202b391b2 100644 --- a/src/features/search/everywhere/SearchEverywhere.tsx +++ b/src/features/search/everywhere/SearchEverywhere.tsx @@ -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 && (
- {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", + )}
)} diff --git a/src/features/search/everywhere/controller.ts b/src/features/search/everywhere/controller.ts index 0557566e3..f2689d2a5 100644 --- a/src/features/search/everywhere/controller.ts +++ b/src/features/search/everywhere/controller.ts @@ -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; } diff --git a/src/features/search/everywhere/deps.ts b/src/features/search/everywhere/deps.ts index b1f576bf2..2b44ad935 100644 --- a/src/features/search/everywhere/deps.ts +++ b/src/features/search/everywhere/deps.ts @@ -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; } diff --git a/src/features/search/everywhere/model.ts b/src/features/search/everywhere/model.ts index 269549e18..6cb8d1d5e 100644 --- a/src/features/search/everywhere/model.ts +++ b/src/features/search/everywhere/model.ts @@ -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);