fix(search): start Find in Files from the selection in its preview

Find in Files and Search Everywhere took the selection only from the active
editor tab, so text selected in the Find in Files preview (or another
focused editor, such as a diff pane) was ignored when the action ran again.
The focused editor's selection now comes first, then the active editor's.

Fixes #6992
Цей коміт міститься в:
l0ng-ai committed 2026-11-30 20:02:17 +08:00
1 джерело f47a83828c
коміт 1982f22005
2 files changed
+38 -2

No files matched your search

+30
Переглянути файл
@@ -0,0 +1,30 @@
import { afterEach, expect, it } from "vitest";
import { EditorSelection, EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { _setActiveEditor, type TextEditorHandle } from "@/editor/api";
import { editorSelectionText } from "../index";
const views: EditorView[] = [];
const view = (doc: string, from: number, to: number) => {
const v = new EditorView({
state: EditorState.create({ doc, selection: EditorSelection.single(from, to) }),
parent: document.body,
});
views.push(v);
return v;
};
afterEach(() => {
_setActiveEditor(null);
views.splice(0).forEach((v) => v.destroy());
});
it("takes the selection of the focused editor, such as the Find in Files preview, over the active one", () => {
const main = view("val first = 1", 4, 9);
_setActiveEditor({ view: main } as unknown as TextEditorHandle);
expect(editorSelectionText()).toBe("first");
const preview = view("fun second() {}", 4, 10);
preview.contentDOM.focus();
expect(editorSelectionText()).toBe("second");
preview.contentDOM.blur();
expect(editorSelectionText()).toBe("first");
});
+8 -2
Переглянути файл
@@ -3,6 +3,7 @@
* Recent Files / Recent Locations, Find / Replace in Files + the Find tool
* window, the Structure tool window + File Structure popup, and bookmarks.
*/
import { EditorView } from "@codemirror/view";
import { lazyComponent, lazyWhen } from "@/ui";
import { activeEditor, focusedCellEditor, registerEditorContribution } from "@/editor/api";
import { executeAction, registerActions } from "@/platform/actions";
@@ -82,8 +83,13 @@ async function removeMissingBookmarks(removed: readonly string[]) {
}
/** Single-line editor selection, used to prefill search fields. */
function editorSelectionText(): string | null {
const view = activeEditor()?.view;
/**
* The single-line selection a search starts from: the focused editor's (a Find in Files preview,
* a diff pane), else the active editor's, as IDEA reads it from the editor in the action's context.
*/
export function editorSelectionText(): string | null {
const focused = document.activeElement?.closest(".cm-editor");
const view = (focused instanceof HTMLElement ? EditorView.findFromDOM(focused) : null) ?? activeEditor()?.view;
if (!view) return null;
const { from, to } = view.state.selection.main;
if (from === to || to - from > 200) return null;