find

tcmts.cli.pages.find · #a2udsleg · term · runs on the server

uses tcmts.cli.search.searchByType, which uses node:path

The finder: definitions by name (the letters in order, best matches first), by words in their docs, and, when the query looks like a type ((xs: T[]) => T, or anything starting with :), by type.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { docSummary, looksLikeType, nameScore } from "#codebase/tcmts/cli/pages";
import { searchByType } from "#codebase/tcmts/cli/search";

export function find(cb: Codebase, query: string, viewUri: (hash: string) => string) {
  const q = query.trim();
  if (!q) return [];
  const item = (p: string, hash: string, detail?: string) => {
    const rec = cb.store.getDef(hash)!;
    return {
      path: p,
      hash,
      kind: rec.kind,
      signature: cb.store.shownSignature(rec),
      detail: detail ?? docSummary(rec),
      uri: viewUri(hash),
    };
  };
  if (looksLikeType(q)) {
    try {
      return searchByType(cb, q.replace(/^:/, "")).hits.map((h) => ({
        ...item(h.path, h.hash, h.notes.join(", ") || "same type"),
        match: "type",
      }));
    } catch (e) {
      return [
        {
          path: "",
          hash: "",
          kind: "error",
          signature: "",
          detail: (e as Error).message,
          uri: "",
          match: "error",
        },
      ];
    }
  }
  const lower = q.toLowerCase();
  const names = cb.store.names();
  const scored = names.map((n) => {
    const name = nameScore(lower, n.path);
    const doc = name ? 0 : cb.store.getDef(n.hash)?.doc?.toLowerCase().includes(lower) ? 20 : 0;
    return { n, score: Math.max(name, doc), match: name ? "name" : "doc" };
  });
  return scored
    .filter((s) => s.score > 0)
    .sort((a, b) => b.score - a.score || (a.n.path < b.n.path ? -1 : 1))
    .slice(0, 60)
    .map((s) => ({ ...item(s.n.path, s.n.hash), match: s.match }));
}

uses

used by