at

tcmts.cli.main.at · #geanzide · term · runs on the server

uses node:path

tcm at <file>[:<line>]: the definition at a line of a file, or every definition in it with its lines. For scratch files, definitions' documents, generated modules (what stack traces name) and mirrored files. A module named by its component's hash (.tcm/cache/<hash>.ts, a packed package's modules/<hash>.js, from a stack trace or a profile, here or elsewhere) is its component's: the member at the line, or every member. A view reads as its branch has it now (rendered: one only read, --branch, has no files written); another branch's view is that branch's to say (--branch it), not this one's.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { cutSignature, definitionsAt } from "#codebase/tcmts/cli/main";
import { bold, dim } from "#codebase/tcmts/cli/style";
import { shortHash } from "#codebase/tcmts/core/hash";
import path from "node:path";

export function at(cb: Codebase, arg: string) {
  const m = /^(.*?)(?::(\d+))?(?::\d+)?$/.exec(arg)!;
  const file = path.resolve(process.cwd(), m[1]!);
  const line = m[2] ? Number(m[2]) : undefined;
  const shown = definitionsAt(cb, file, line, m[1]!);
  // a generated module's members: no lines of their own to say
  if (shown.length && shown[0]!.from === undefined) {
    for (const d of shown) {
      const rec = cb.store.getDef(d.hash!)!;
      console.log(
        `${bold(d.name)}  ${dim(shortHash(d.hash!))}  ${dim(cutSignature(rec.kind === "type" ? rec.signature : `: ${cb.store.shownSignature(rec)}`, 80))}`,
      );
    }
    return;
  }
  if (!shown.length)
    return console.log(
      dim(
        line === undefined
          ? `no definitions in ${m[1]}`
          : `line ${line} of ${m[1]} isn't in a definition`,
      ),
    );
  for (const d of shown) {
    const rec = d.hash ? cb.store.getDef(d.hash) : undefined;
    const what = rec
      ? `${dim(shortHash(rec.hash))}  ${dim(cutSignature(rec.kind === "type" ? rec.signature : `: ${cb.store.shownSignature(rec)}`, 80))}`
      : dim("(not in the codebase)");
    console.log(`${bold(d.name)}  ${what}  ${dim(`lines ${d.from}–${d.to}`)}`);
  }
}

uses

used by