referencesInScratch

tcmts.studio.referencesInScratch · #iriqkqe2 · term · runs on the server

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

Where the name at offset in a scratch file is used (text being what it holds): its lines here, and, for a name the codebase has, what in the codebase uses it (the named definitions that refer to it).

import { analyzedScratch, editableCodebase, nameInScratch, scratchPath } from "#codebase/tcmts/studio";

export async function referencesInScratch(
  file: string,
  text: string,
  offset: number,
): Promise<{
  name: string;
  here: { from: number; to: number; line: number; text: string }[];
  codebase?: { name: string; usedBy: string[] };
} | null> {
  const cb = editableCodebase();
  const abs = scratchPath(cb, file, text);
  const a = analyzedScratch(cb, abs, text);
  const n = nameInScratch(cb, a, abs, offset);
  if (!n) return null;
  const lines = text.split("\n");
  const here = n.uses.map((u) => {
    const line = text.slice(0, u.from).split("\n").length;
    return { ...u, line, text: lines[line - 1]!.trim() };
  });
  const usedBy = n.hash
    ? [...new Set(cb.store.dependents(n.hash).flatMap((d) => cb.store.pathsOf(d).slice(0, 1)))]
        .sort()
        .slice(0, 300)
    : [];
  return {
    name: n.id.text,
    here,
    ...(n.name ? { codebase: { name: n.name, usedBy } } : {}),
  };
}

uses

used by