Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

1.1 KB
Raw Blame
/* Copyright 2026 Marimo. All rights reserved. */

import type { SearchCursor, SearchQuery } from "@codemirror/search";
import type { EditorState } from "@codemirror/state";
import { invariant } from "@/utils/invariant";

type SearchResult = typeof SearchCursor.prototype.value;

// Copied from https://github.com/codemirror/search/blob/6.5.2/src/search.ts#L164
export interface QueryType<Result extends SearchResult = SearchResult> {
  nextMatch(state: EditorState, curFrom: number, curTo: number): Result | null;
  prevMatch(state: EditorState, curFrom: number, curTo: number): Result | null;
  getReplacement(result: Result): string;
  matchAll(state: EditorState, limit: number): readonly Result[] | null;
  highlight(
    state: EditorState,
    from: number,
    to: number,
    add: (from: number, to: number) => void,
  ): void;
  spec: SearchQuery;
}

export interface QueryCreator {
  create(): QueryType;
}

export function asQueryCreator(query: SearchQuery): QueryCreator {
  invariant("create" in query, 'Expected query to have a "create" method');
  return query as unknown as QueryCreator;
}