definitionLines

tcmts.share.definitionLines · #ipxrz632 · term · runs on the server

uses tcmts.cli.files.fileContent, which uses Buffer

A definition's code as lines, as a diff compares it, its references read with nameOf; text reads one of its text versions (its comments, layout and doc), undefined the one it was first stored with.

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import { fileContent, formatOf, isBytes } from "#codebase/tcmts/cli/files";
import { render } from "#codebase/tcmts/loader";

export function definitionLines(
  cb: Codebase,
  hash: string,
  nameOf: (h: string) => string | undefined,
  text?: { version: string | undefined },
): string[] {
  const src = cb.source();
  const rec = text ? cb.store.defWithText(hash, text.version) : src.get(hash);
  if (!rec) return [];
  if (rec.kind === "file") {
    const at = nameOf(hash) ?? "";
    const content = fileContent(cb, rec, at.slice(0, Math.max(0, at.lastIndexOf("."))));
    return isBytes(rec)
      ? [`(${formatOf(rec) || "bytes"}, ${content.length} bytes)`]
      : content.toString("utf8").trimEnd().split("\n");
  }
  return render([rec], src, cb.lay, {
    style: "scratch",
    omitImports: true,
    nameOf,
    exportDeclarations: true,
  })
    .text.trimEnd()
    .split("\n");
}

uses

used by