textChanged

tcmts.cli.main.textChanged · #ogrpxt2f · term · runs anywhere

Whether def, the same code as what's stored under its hash, reads differently: its comments or layout changed, or the name it imports something from outside the codebase as (which it's shown with). References to the codebase are left out of the comparison: the stored text has each name as first written, a scratch file the names as they are now (and either is shown with its name now).

import type { Codebase } from "#codebase/tcmts/cli/codebase";
import type { DefRecord } from "#codebase/tcmts/core/types";

export function textChanged(cb: Codebase, def: DefRecord): boolean {
  const stored = cb.store.getDef(def.hash);
  if (!stored || stored.kind === "file") return false;
  const masked = (r: DefRecord) => {
    let text = r.sourceText;
    for (const s of [...r.spans].sort((a, b) => b.start - a.start))
      if (!s.ext) text = `${text.slice(0, s.start)}\0${text.slice(s.end)}`;
    return text;
  };
  return masked(stored) !== masked(def);
}

uses

used by