findDefinitions

tcmts.studio.findDefinitions · #4qqfsoou · term · runs on the server

uses tcmts.studio.editableCodebase, which uses tcmts.share.servedCodebase, which uses process

The codebase's definitions named like q (its letters in order, in the name), the closest first: what a scratch file can pull in.

import { editableCodebase } from "#codebase/tcmts/studio";

export async function findDefinitions(
  q: string,
): Promise<{ path: string; kind: string; signature: string }[]> {
  const cb = editableCodebase();
  const want = q.trim().toLowerCase();
  if (!want) return [];
  const leafOf = (p: string) => p.slice(p.lastIndexOf(".") + 1).toLowerCase();
  const inOrder = (s: string) => {
    let i = 0;
    for (const ch of s) if (ch === want[i]) i++;
    return i === want.length;
  };
  return cb.store
    .names()
    .filter((n) => inOrder(n.path.toLowerCase()))
    .map((n) => {
      const leaf = leafOf(n.path);
      const score =
        leaf === want
          ? 0
          : leaf.startsWith(want)
            ? 1
            : leaf.includes(want)
              ? 2
              : n.path.toLowerCase().includes(want)
                ? 3
                : 4;
      return { n, score };
    })
    .sort(
      (x, y) =>
        x.score - y.score || x.n.path.length - y.n.path.length || x.n.path.localeCompare(y.n.path),
    )
    .slice(0, 30)
    .flatMap(({ n }) => {
      const rec = cb.store.getDef(n.hash);
      return rec && rec.kind !== "file"
        ? [{ path: n.path, kind: rec.kind, signature: cb.store.shownSignature(rec) }]
        : [];
    });
}

uses

used by