Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

2.2 KB
Raw Blame
import type { EditorState, TransactionSpec } from "@codemirror/state";
import { SearchQuery } from "@codemirror/search";

export interface EditorSearchQueryOptions {
  search: string;
  replace?: string;
  caseSensitive: boolean;
  useRegex: boolean;
  wholeWord?: 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,
    wholeWord: options.wholeWord ?? false,
  });
}

export interface EditorSearchMatch {
  from: number;
  to: number;
}

export interface EditorSearchMatchCount {
  count: number;
  currentIndex: number;
}

export function countEditorSearchMatches(query: SearchQuery, state: EditorState, from: number, to: number, current?: EditorSearchMatch): EditorSearchMatchCount {
  let count = 0;
  let currentIndex = 0;
  const cursor = query.getCursor(state);

  for (let result = cursor.next(); !result.done; result = cursor.next()) {
    if (result.value.from < from || result.value.to > to) continue;
    count++;
    if (current && result.value.from === current.from && result.value.to === current.to) currentIndex = count;
  }

  return { count, currentIndex };
}

export function collectEditorSearchMatches(query: SearchQuery, state: EditorState, from: number, to: number, limit = Number.POSITIVE_INFINITY): EditorSearchMatch[] {
  const matches: EditorSearchMatch[] = [];
  const cursor = query.getCursor(state);

  for (let result = cursor.next(); !result.done; result = cursor.next()) {
    if (result.value.from >= from && result.value.to <= to) {
      matches.push({ from: result.value.from, to: result.value.to });
      if (matches.length >= limit) break;
    }
  }

  return matches;
}

export function replaceEditorSearchMatches(view: { dispatch(spec: TransactionSpec): void }, matches: readonly EditorSearchMatch[], replacement: (match: EditorSearchMatch) => string): boolean {
  if (matches.length === 0) return false;

  view.dispatch({
    changes: matches.map((match) => ({
      from: match.from,
      to: match.to,
      insert: replacement(match),
    })),
  });
  return true;
}