viewText

tcmts.cli.views.viewText · #ul5k4o7l · term · runs on the server

uses tcmts.cli.placement.Placement, which uses node:path

The view of these definitions (one name's: a type and a value can share it), as the editor shows it.

import type { CodebaseCore } from "#codebase/tcmts/cli/codebase";
import { Placement } from "#codebase/tcmts/cli/placement";
import type { View } from "#codebase/tcmts/cli/views";
import { shortHash } from "#codebase/tcmts/core/hash";
import type { DefRecord } from "#codebase/tcmts/core/types";
import { render } from "#codebase/tcmts/loader";

export function viewText(
  cb: CodebaseCore,
  hashes: string[],
  placement = new Placement(cb),
  nameAt?: (hash: string) => string | undefined,
): View | undefined {
  const recs = hashes.map((h) => cb.store.getDef(h)).filter((r): r is DefRecord => !!r);
  if (!recs.length) return undefined;
  const header =
    recs
      .map((r) => {
        const where = r.kind === "term" ? placement.label(r.hash) : "";
        const names = nameAt
          ? [nameAt(r.hash)].filter((n): n is string => !!n)
          : cb.store.pathsOf(r.hash);
        return `// ${names.join(", ") || "(unnamed)"} ${shortHash(r.hash)}${where ? ` · ${where}` : ""}`;
      })
      .join("\n") + "\n";
  const r = render(recs, cb.source([], nameAt), cb.lay, {
    style: "scratch",
    exportDeclarations: true,
  });
  const shift = header.length;
  return {
    text: header + r.text,
    refs: r.refs.map((x) => ({ ...x, start: x.start + shift, end: x.end + shift })),
    starts: new Map([...r.starts].map(([h, o]) => [h, o + shift])),
    inserts: r.inserts,
    hashes: recs.map((r) => r.hash),
  };
}

uses

used by