From f79ae0273e0ac4db868e53a741d730e00803edca Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Fri, 27 Nov 2026 03:36:26 +0800 Subject: [PATCH] refactor(editor): provide the Copy Path chooser to the editor API instead of importing it Keeps @/editor/api free of the popup store and keymap imports, so modules that load it (and the language smoke's inputs) don't grow. --- src/editor/api.ts | 34 +++++++++++++++++-- src/editor/core/copyPathChooser.ts | 13 ++----- src/editor/core/index.ts | 4 ++- src/features/project-tree/contextMenu.test.ts | 4 +++ 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/editor/api.ts b/src/editor/api.ts index 7ea6608c7e..87d81a4025 100644 --- a/src/editor/api.ts +++ b/src/editor/api.ts @@ -34,7 +34,7 @@ import type { LanguageDescription } from "@codemirror/language"; import type { HunkKind } from "@/bindings"; import type { LineEnding } from "@/platform/ipc"; import { create } from "zustand"; -import { maybeEditorService, type EditorInput } from "@/workbench/contributions"; +import { maybeEditorService, type EditorInput, type MenuGroupContext } from "@/workbench/contributions"; import { isSameOrInside } from "@/platform/paths"; import { useProject } from "@/platform/project"; import { rootOfPath } from "@/platform/projectRoots"; @@ -49,7 +49,6 @@ export { type NavigationMatch, } from "@/editor/navigationProviders"; export { registerRelatedProvider, type RelatedContext, type RelatedItem } from "@/editor/relatedProviders"; -export { openCopyPathChooser, type CopyPathRow } from "@/editor/core/copyPathChooser"; export { offsetsToRange, offsetToPos, posToOffset, rangeToOffsets, textEditsToChanges } from "@/editor/lsp/positions"; /** Documents above this size (UTF-16 units) skip per-line decorations. */ @@ -1018,6 +1017,37 @@ export function _provideCodePreview(provider: typeof codePreviewProvider) { codePreviewProvider = provider; } +/** A row of the Copy Path/Reference… chooser. */ +export interface CopyPathRow { + /** The action whose shortcut the row shows. */ + id: string; + /** English label, translated by the chooser. */ + label: string; + /** The copied text; a row without one is left out. */ + preview: string | null; + run: () => void; +} + +type CopyPathChooser = ( + anchor: { x: number; y: number } | "center", + rows: readonly CopyPathRow[], + ctx: MenuGroupContext, +) => void; +let copyPathChooser: CopyPathChooser | null = null; + +/** + * The editor's Copy Path/Reference… chooser for files outside an editor (the Project view's + * selection): `rows`, then the rows contributed to the `copyPath` menu for `ctx`, with previews. + */ +export function openCopyPathChooser(...args: Parameters): void { + copyPathChooser?.(...args); +} + +/** @internal editor/core only. */ +export function _provideCopyPathChooser(open: CopyPathChooser) { + copyPathChooser = open; +} + let workspaceHistory: ((id: number, redo: boolean) => Promise) | null = null; /** Undo/redo a refactoring through the same history used by editor commands. */ diff --git a/src/editor/core/copyPathChooser.ts b/src/editor/core/copyPathChooser.ts index e565fce001..4ace325eae 100644 --- a/src/editor/core/copyPathChooser.ts +++ b/src/editor/core/copyPathChooser.ts @@ -3,16 +3,7 @@ import { tx } from "@/platform/i18n"; import { shortcutLabel } from "@/platform/keymap"; import { menuGroupItems, type MenuGroupContext } from "@/workbench/contributions"; import { openPopup, type ListPopupItem, type LspPopup, type PopupPoint } from "@/editor/lsp/popupStore"; - -export interface CopyPathRow { - /** The action whose shortcut the row shows. */ - id: string; - /** English label, translated here. */ - label: string; - /** The copied text; a row without one is left out. */ - preview: string | null; - run: () => void; -} +import type { CopyPathRow } from "@/editor/api"; /** The chooser's popup: `rows`, then the rows contributed to the `copyPath` menu for `ctx`. */ export function copyPathPopup( @@ -51,7 +42,7 @@ export function copyPathPopup( }; } -/** Opens the chooser for files outside an editor (the Project view's selection). */ +/** Opens the chooser for files outside an editor (`openCopyPathChooser` of `@/editor/api`). */ export function openCopyPathChooser( anchor: PopupPoint | "center", rows: readonly CopyPathRow[], diff --git a/src/editor/core/index.ts b/src/editor/core/index.ts index 0e9508125c..128a280e69 100644 --- a/src/editor/core/index.ts +++ b/src/editor/core/index.ts @@ -10,7 +10,8 @@ import { registerOverlay, registerStatusItem, } from "@/workbench/contributions"; -import { _provideCodePreview, _provideDocumentAttacher, editorsFor } from "@/editor/api"; +import { _provideCodePreview, _provideCopyPathChooser, _provideDocumentAttacher, editorsFor } from "@/editor/api"; +import { openCopyPathChooser } from "./copyPathChooser"; import { isUntitledUri } from "@/platform/virtualDocs"; import { formatDocument } from "@/editor/lsp/format"; import { chooserTitle, locationItems, pinToFind } from "@/editor/lsp/navigation"; @@ -82,6 +83,7 @@ export function activate(): void | (() => void) { }, }); _provideDocumentAttacher((path, options) => attachDocument(ctrl.manager, path, options)); + _provideCopyPathChooser(openCopyPathChooser); _provideCodePreview((path, editable) => previewExtensions(path, ctrl.manager.settings()?.editor, ctrl.manager.backend, editable), ); diff --git a/src/features/project-tree/contextMenu.test.ts b/src/features/project-tree/contextMenu.test.ts index 3a8959fe20..2cf0c7bd05 100644 --- a/src/features/project-tree/contextMenu.test.ts +++ b/src/features/project-tree/contextMenu.test.ts @@ -9,6 +9,10 @@ import { contextMenuItems, keyLabel, revealLabel } from "./contextMenu"; import type { ProjectNode } from "./model"; import { useProjectTree } from "./store"; import { closePopup, useLspPopup } from "@/editor/lsp/popupStore"; +import { _provideCopyPathChooser } from "@/editor/api"; +import { openCopyPathChooser } from "@/editor/core/copyPathChooser"; + +_provideCopyPathChooser(openCopyPathChooser); vi.mock("@/platform/ipc", async (importOriginal) => ({ ...(await importOriginal()),