Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 9b18bafe12 fix(editor): Enter in an empty find bar no longer opens a second one
CodeMirror's findNext / replaceNext / replaceAll open its own search panel when the
query is empty or invalid. The find bar's Enter, F3, arrows and Replace buttons call
them, so pressing Enter before typing showed CodeMirror's panel under ours. The
wrappers in cm/find.ts now do nothing without a usable query, as findPrevious
already did.

Fixes #5800
2026-11-25 15:40:21 +08:00
2 змінених файлів з 23 додано та 4 видалено

No files matched your search

+13 -1
Переглянути файл
@@ -1,7 +1,7 @@
import { afterEach, describe, expect, it } from "vitest";
import { EditorSelection, EditorState, Prec, StateEffect } from "@codemirror/state";
import { Decoration, EditorView, WidgetType } from "@codemirror/view";
import { setSearchQuery } from "@codemirror/search";
import { searchPanelOpen, setSearchQuery } from "@codemirror/search";
import { StreamLanguage } from "@codemirror/language";
import { literalKindAt } from "./literals";
import { selectionMarks } from "./selectionMarks";
@@ -116,6 +116,18 @@ describe("find backgrounds", () => {
});
});
describe("without a usable query", () => {
it("next, previous and replace do nothing instead of opening CodeMirror's search panel", () => {
for (const o of [{ search: "" }, { search: "(", regexp: true }]) {
const v = make("abc abc", { ...o, replace: "x" });
expect([findNext(v), findPrevious(v), replaceNext(v), replaceAll(v)]).toEqual([false, false, false, false]);
expect(searchPanelOpen(v.state)).toBe(false);
expect(v.dom.querySelector(".cm-search")).toBeNull();
expect(v.state.doc.toString()).toBe("abc abc");
}
});
});
describe("find in selection", () => {
const doc = "a1 a2\na3 a4\na5 a6\n";
// Scope: the second line only.
+10 -3
Переглянути файл
@@ -359,8 +359,15 @@ function step(view: EditorView, forward: boolean): boolean {
return true;
}
/**
* CodeMirror's search commands open its own search panel when the query is empty or invalid;
* ours run from the find bar, so without a usable query they do nothing.
*/
const usableQuery = (view: EditorView) => getSearchQuery(view.state).valid;
export function findNext(view: EditorView): boolean {
return view.state.field(findScope, false) ? step(view, true) : cmFindNext(view);
if (view.state.field(findScope, false)) return step(view, true);
return usableQuery(view) && cmFindNext(view);
}
/** The last match in `from`–`to` other than `skip`. */
@@ -466,7 +473,7 @@ const replacePreview = StateField.define<{ preserve: boolean | null; tooltip: To
/** Replaces the selected match (if it is one) and selects the next match. */
export function replaceNext(view: EditorView, preserve = false): boolean {
const { state } = view;
if (!state.field(findScope, false) && !preserve) return cmReplaceNext(view);
if (!state.field(findScope, false) && !preserve) return usableQuery(view) && cmReplaceNext(view);
if (state.readOnly) return false;
const query = getSearchQuery(state);
const ms = matchesIn(state);
@@ -487,7 +494,7 @@ export function replaceNext(view: EditorView, preserve = false): boolean {
export function replaceAll(view: EditorView, preserve = false): boolean {
const { state } = view;
if (!state.field(findScope, false) && !preserve) return cmReplaceAll(view);
if (!state.field(findScope, false) && !preserve) return usableQuery(view) && cmReplaceAll(view);
if (state.readOnly) return false;
const query = getSearchQuery(state);
const changes = matchesIn(state).map((m) => ({