fix(editor): update SQL search escaping and match counts
This commit is contained in:
4 files changed
+75
-7
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createEditorSearchQuery } from "@/lib/editor/editorSearchQuery";
|
||||
|
||||
function matchedText(search: string, useRegex: boolean): string[] {
|
||||
const state = EditorState.create({
|
||||
doc: String.raw`SELECT '\n' AS escaped;
|
||||
SELECT 1 AS actual_line_break;`,
|
||||
});
|
||||
const cursor = createEditorSearchQuery({ search, caseSensitive: false, useRegex }).getCursor(state);
|
||||
const matches: string[] = [];
|
||||
|
||||
for (let result = cursor.next(); !result.done; result = cursor.next()) {
|
||||
matches.push(state.sliceDoc(result.value.from, result.value.to));
|
||||
}
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
describe("editorSearchQuery", () => {
|
||||
it("treats escape sequences literally in normal search mode", () => {
|
||||
expect(matchedText(String.raw`\n`, false)).toEqual([String.raw`\n`]);
|
||||
});
|
||||
|
||||
it("allows regular expression mode to match actual line breaks", () => {
|
||||
expect(matchedText(String.raw`\n`, true)).toEqual(["\n"]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
import { SearchQuery } from "@codemirror/search";
|
||||
|
||||
export interface EditorSearchQueryOptions {
|
||||
search: string;
|
||||
replace?: string;
|
||||
caseSensitive: boolean;
|
||||
useRegex: boolean;
|
||||
}
|
||||
|
||||
export function createEditorSearchQuery(options: EditorSearchQueryOptions): SearchQuery {
|
||||
return new SearchQuery({
|
||||
search: options.search,
|
||||
replace: options.replace ?? "",
|
||||
caseSensitive: options.caseSensitive,
|
||||
regexp: options.useRegex,
|
||||
literal: !options.useRegex,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user