// Merge twinkleplop tokens with twoslash nodes into a single HTML string. // // The algorithm is a break-point segment walk: // // 1. Partition twoslash nodes into `wrappers` (hover/error/highlight — // these decorate a range of text), `completions` (`^|` — zero-length, // so they anchor to a single offset) and `lineAnnotations` // (query/tag/error — these render as sibling spans after a line ends). // 2. Build a sorted set of break points from every token boundary, every // wrapper boundary, every completion offset, every newline, and // 0 / input.length. Every adjacent pair (segStart, segEnd) is a // segment that lies entirely inside at most one token and at most one // copy of each wrapper type, so tag nesting is unambiguous. // 3. Walk segments left-to-right, diffing the active wrapper+token // "stack" against the previously-open stack and emitting open/close // tags for the difference. // 4. After any segment whose last char is `\n`, temporarily unwind the // stack, emit queued line annotations for the line we just ended, // then restore the stack for the next segment. // 5. Completion lists emit at their offset, between the wrapper stack and // the token span. // // Output is wrapped in
…
. // `twinkleplop` is the class the themes bind token colours to (matching // core's `to_html` default); `twoslash` carries the popover/query styling. // (class_name configurable). `@twinkleplop/twoslash/style.css` carries the // layout and visibility rules that turn the popover spans into tooltips and // the query/error spans into block-level callouts; it is colour-free, so a // theme supplies the token colours. import { language } from "./language.js"; import type { DocTag, Wrapper, CompletionPoint, LineAnnotation, HighlightOptions, } from "./types.js"; import { TwoslashOptions, type TwoslashReturn, type NodeError } from "twoslash"; import { escape_html } from "@twinkleplop/core"; import type { TokenizeResult } from "@twinkleplop/core"; /** * Tokenize a short TypeScript fragment (a hover's type string, a query's * type readout, …) and return raw `…` HTML with * no `
` wrapper. Used to syntax-highlight tooltip / query
 * payloads the same way the main snippet is highlighted.
 *
 * Twoslash's type strings are valid TypeScript fragments (e.g.
 * `const x: { a: number; b: string }`), so running them through the same
 * `language` function gives consistent colors across code and tooltips.
 */
function highlight_fragment(text: string, ctx: RenderContext): string {
  if (!text) return "";
  const kind = QUICKINFO_KIND.exec(text);
  if (!kind) return highlight_tokens(text, "", ctx);
  // quickinfo after the kind label is not typescript alone, a throwaway
  // declaration keyword gives its annotation a type position
  const rest = text.substring(kind[0].length);
  const context = DECLARATION_START.test(rest)
    ? ""
    : CALLABLE_KINDS.has(kind[1])
      ? "function "
      : "let ";
  return highlight_tokens(kind[0], "", ctx) + highlight_tokens(rest, context, ctx);
}

const QUICKINFO_KIND = /^\(([a-z][a-z ]*)\) /;
const CALLABLE_KINDS = new Set(["method", "function", "local function", "constructor"]);
const DECLARATION_START =
  /^(?:type|interface|class|enum|namespace|module|const|let|var|function|import|export|declare)\b/;

/**
 * Highlight `text` as if `context` preceded it, emitting spans for `text`
 * only.
 */
function highlight_tokens(text: string, context: string, ctx: RenderContext): string {
  const { tokens, token_types } = language(context + text);
  const offset = context.length;
  let out = "";
  let last_end = 0;
  for (let i = 0; i < tokens.length; i += 3) {
    const type_id = tokens[i];
    const start = tokens[i + 1] - offset;
    const end = tokens[i + 2] - offset;
    if (end <= 0) continue;
    if (start > last_end) {
      out += ctx.escape(text.substring(last_end, start));
    }
    out += `${ctx.escape(
      text.substring(Math.max(start, 0), end),
    )}`;
    last_end = end;
  }
  if (last_end < text.length) {
    out += ctx.escape(text.substring(last_end));
  }
  return out;
}

/**
 * the caller hooks, resolved once per call so the walk below can reach for
 * them unconditionally; an absent one resolves to what was hard coded before.
 */
interface RenderContext {
  escape: (text: string) => string;
  render_docs: (markdown: string) => string;
  process_type: (type: string) => string;
  split_tags: boolean;
}

function resolve_render_context(options: HighlightOptions): RenderContext {
  const map = options.escape;
  const escape = (text: string) => escape_html(text, map);
  return {
    escape,
    render_docs: options.render_docs ?? escape,
    process_type: options.process_type ?? ((type) => type),
    split_tags: (options.docs_tags ?? "split") === "split",
  };
}

// jsdoc tags whose value opens with the name of what they document; that
// name is lifted out of the prose into its own element.
const NAMED_TAGS = new Set([
  "param",
  "arg",
  "argument",
  "prop",
  "property",
  "template",
  "typeparam",
]);

function is_tag_space(code: number): boolean {
  return code === 32 || code === 9;
}

// `prefix` is the enclosing block's class stem (`twoslash-popover`,
// `twoslash-query`) so tags sit in the same family as its type and docs.
function render_doc_tags(tags: DocTag[] | undefined, prefix: string, ctx: RenderContext): string {
  if (!ctx.split_tags || !tags || tags.length === 0) return "";
  let out = ``;
  for (const [name, text] of tags) {
    out +=
      `` +
      render_doc_tag_value(name, text, prefix, ctx) +
      ``;
  }
  return out + ``;
}

function render_doc_tag_value(
  name: string,
  text: string | undefined,
  prefix: string,
  ctx: RenderContext,
): string {
  // `@deprecated` carries no value, and a tag's boolean form reports `true`.
  if (typeof text !== "string" || text.length === 0) return "";
  if (!NAMED_TAGS.has(name.toLowerCase())) return ctx.render_docs(text);

  let name_end = 0;
  while (name_end < text.length && !is_tag_space(text.charCodeAt(name_end))) name_end++;
  let rest_start = name_end;
  while (rest_start < text.length && is_tag_space(text.charCodeAt(rest_start))) rest_start++;

  const named = `${ctx.escape(
    text.substring(0, name_end),
  )}`;
  if (rest_start >= text.length) return named;
  return `${named} ${ctx.render_docs(text.substring(rest_start))}`;
}

function add_line_annotation(
  map: Map,
  line: number,
  ann: LineAnnotation,
) {
  let arr = map.get(line);
  if (!arr) {
    arr = [];
    map.set(line, arr);
  }
  arr.push(ann);
}

/**
 * Line-starts array: lineStarts[n] = offset of the first char of line n.
 * Used to map a char offset back to a 0-indexed line number.
 */
function build_line_starts(input: string): number[] {
  const starts = [0];
  for (let i = 0; i < input.length; i++) {
    if (input.charCodeAt(i) === 10) starts.push(i + 1);
  }
  return starts;
}

function line_for_offset(line_starts: number[], offset: number): number {
  // find the largest index n where line_starts[n] <= offset.
  let lo = 0;
  let hi = line_starts.length - 1;
  while (lo < hi) {
    const mid = (lo + hi + 1) >> 1;
    if (line_starts[mid] <= offset) lo = mid;
    else hi = mid - 1;
  }
  return lo;
}

/**
 * Translate a wrapper descriptor into its `open` / `close` HTML strings.
 * For hovers the close string also carries the popover payload so the
 * whole tooltip structure is baked into a single close event.
 */
function wrapper_tags(w: Wrapper, ctx: RenderContext): { open: string; close: string } {
  switch (w.kind) {
    case "hover": {
      const open = ``;
      let popover = `${highlight_fragment(
        ctx.process_type(w.text ?? ""),
        ctx,
      )}`;
      if (w.docs) {
        popover += `${ctx.render_docs(w.docs)}`;
      }
      popover += render_doc_tags(w.tags, "twoslash-popover", ctx);
      const close = ``;
      return { open, close };
    }
    case "error": {
      let attrs = "";
      if (w.code != null) attrs += ` data-error-code="${ctx.escape(String(w.code))}"`;
      if (w.level) attrs += ` data-error-level="${ctx.escape(w.level)}"`;
      return {
        open: ``,
        close: ``,
      };
    }
    case "highlight": {
      const attr = w.text ? ` data-highlight-text="${ctx.escape(w.text)}"` : "";
      return {
        open: ``,
        close: ``,
      };
    }
  }
  return { open: "", close: "" };
}

/**
 * Render a completion list as a single self-contained element: an empty
 * host, which `.twoslash-completion { position: relative }` and the
 * absolutely positioned `.twoslash-completions` turn into a dropdown at the
 * caret. A completion the compiler returned nothing for has nothing to
 * anchor, so it emits nothing at all.
 */
function render_completion(point: CompletionPoint, ctx: RenderContext): string {
  const entries = point.completions;
  if (!Array.isArray(entries) || entries.length === 0) return "";
  const prefix_attr = point.prefix ? ` data-prefix="${ctx.escape(point.prefix)}"` : "";
  let inner = "";
  for (const c of entries) {
    inner += `${ctx.escape(c.name)}${
      c.docs ? `${ctx.render_docs(c.docs)}` : ""
    }`;
  }
  return (
    `` +
    `${inner}` +
    ``
  );
}

function render_line_annotation(ann: LineAnnotation, ctx: RenderContext) {
  switch (ann.kind) {
    case "query": {
      let out = `${highlight_fragment(
        ctx.process_type(ann.text ?? ""),
        ctx,
      )}`;
      if (ann.docs) {
        out += `${ctx.render_docs(ann.docs)}`;
      }
      out += render_doc_tags(ann.tags, "twoslash-query", ctx);
      out += ``;
      return out;
    }
    case "error-line": {
      let attrs = "";
      if (ann.code != null) attrs += ` data-error-code="${ctx.escape(String(ann.code))}"`;
      if (ann.level) attrs += ` data-error-level="${ctx.escape(ann.level)}"`;
      return `${ctx.escape(ann.text ?? "")}`;
    }
    case "tag": {
      return `${ctx.escape(ann.text ?? "")}`;
    }
  }
  return "";
}

export function render(
  input: string,
  token_result: TokenizeResult,
  twoslash_result: TwoslashReturn,
  options: HighlightOptions = {},
) {
  const { class_name = "twinkleplop twoslash" } = options;
  const { tokens, token_types } = token_result;
  const nodes = twoslash_result.nodes ?? [];
  const ctx = resolve_render_context(options);

  // -- Partition nodes --------------------------------------------------
  const wrappers: Wrapper[] = [];
  const completions: Map = new Map();
  const line_annotations: Map = new Map();

  for (const node of nodes) {
    const start = node.start;
    const end = node.start + node.length;
    switch (node.type) {
      case "hover":
        wrappers.push({
          start,
          end,
          kind: "hover",
          text: node.text,
          docs: node.docs,
          tags: node.tags,
        });
        break;
      case "error":
        wrappers.push({
          start,
          end,
          kind: "error",
          code: node.code,
          level: node.level,
        });
        add_line_annotation(line_annotations, node.line, {
          kind: "error-line",
          text: node.text,
          code: node.code,
          level: node.level,
        });
        break;
      case "highlight":
        wrappers.push({
          start,
          end,
          kind: "highlight",
          text: node.text,
        });
        break;
      case "completion": {
        // `start` is the caret the `^|` marker points at; `length` is always
        // 0, so this is a position, not a range.
        const point: CompletionPoint = {
          prefix: node.completionsPrefix,
          completions: node.completions,
        };
        const at = completions.get(start);
        if (at) at.push(point);
        else completions.set(start, [point]);
        break;
      }
      case "query":
        add_line_annotation(line_annotations, node.line, {
          kind: "query",
          text: node.text,
          docs: node.docs,
          tags: node.tags,
        });
        break;
      case "tag":
        add_line_annotation(line_annotations, node.line, {
          kind: "tag",
          name: node.name,
          // `// @log` with no value reports `true` rather than a string.
          text: typeof node.text === "string" ? node.text : undefined,
        });
        break;
    }
  }

  // Sort wrappers: start asc, then length desc (so wider containers become
  // outer when nesting). This order is the stable outer-to-inner order
  // used throughout the walk.
  wrappers.sort((a, b) => a.start - b.start || b.end - a.end);

  // -- Break points -----------------------------------------------------
  const break_set = new Set();
  break_set.add(0);
  break_set.add(input.length);
  for (let i = 0; i < tokens.length; i += 3) {
    break_set.add(tokens[i + 1]);
    break_set.add(tokens[i + 2]);
  }
  for (const w of wrappers) {
    break_set.add(w.start);
    break_set.add(w.end);
  }
  for (const offset of completions.keys()) break_set.add(offset);
  for (let i = 0; i < input.length; i++) {
    if (input.charCodeAt(i) === 10) break_set.add(i + 1);
  }
  const breaks = [...break_set].sort((a, b) => a - b);

  const line_starts = build_line_starts(input);

  // -- Walk segments ----------------------------------------------------
  const out: string[] = [];
  out.push(`
`);

  let stack: { ref: Wrapper; close: string }[] = [];
  let current_token_type: string | null = null;
  let token_cursor = 0;

  for (let bi = 0; bi < breaks.length - 1; bi++) {
    const seg_start = breaks[bi];
    const seg_end = breaks[bi + 1];
    if (seg_start === seg_end) continue;

    // Advance token cursor past any tokens that end at/before segStart.
    while (token_cursor < tokens.length && tokens[token_cursor + 2] <= seg_start) {
      token_cursor += 3;
    }
    let seg_token_type: string | null = null;
    if (
      token_cursor < tokens.length &&
      tokens[token_cursor + 1] <= seg_start &&
      tokens[token_cursor + 2] >= seg_end
    ) {
      seg_token_type = token_types[tokens[token_cursor]];
    }

    // Active wrappers: those whose range fully contains [segStart, segEnd).
    const active_wrappers: Wrapper[] = [];
    for (const w of wrappers) {
      if (w.start > seg_start) break;
      if (w.start <= seg_start && w.end >= seg_end) active_wrappers.push(w);
    }

    // Longest common prefix between current stack and target stack.
    let common = 0;
    while (
      common < stack.length &&
      common < active_wrappers.length &&
      stack[common].ref === active_wrappers[common]
    ) {
      common++;
    }

    // Close innermost token span first (it nests inside all wrappers).
    if (
      current_token_type !== null &&
      (common < stack.length || seg_token_type !== current_token_type)
    ) {
      out.push("");
      current_token_type = null;
    }

    // Close wrappers above the common prefix.
    while (stack.length > common) out.push(stack.pop()!.close);

    // Open new wrappers after the common prefix.
    while (stack.length < active_wrappers.length) {
      const w = active_wrappers[stack.length];
      const { open, close } = wrapper_tags(w, ctx);
      out.push(open);
      stack.push({ ref: w, close });
    }

    // Emit any completion list anchored here. It goes inside the wrapper
    // stack — the caret can sit mid-hover, and splitting that hover in two
    // would give the line two popovers — but outside the token span, so the
    // list does not inherit the token's colour (the same reason a hover
    // popover is a sibling of `.twoslash-target` rather than a child).
    const points = completions.get(seg_start);
    if (points !== undefined) {
      if (current_token_type !== null) {
        out.push("");
        current_token_type = null;
      }
      for (const point of points) out.push(render_completion(point, ctx));
    }

    // Open the token span (if we don't already have one of the same type).
    if (seg_token_type !== null && current_token_type !== seg_token_type) {
      out.push(``);
      current_token_type = seg_token_type;
    }

    // Emit the escaped segment text.
    out.push(ctx.escape(input.substring(seg_start, seg_end)));

    // After a newline, flush line annotations for the line we just ended.
    if (input.charCodeAt(seg_end - 1) === 10) {
      const ended_line = line_for_offset(line_starts, seg_end - 1);
      const anns = line_annotations.get(ended_line);
      if (anns && anns.length > 0) {
        // Unwind the stack temporarily so annotations sit at root level.
        if (current_token_type !== null) {
          out.push("");
          current_token_type = null;
        }
        const saved = stack.slice();
        while (stack.length) out.push(stack.pop()!.close);

        for (const ann of anns) out.push(render_line_annotation(ann, ctx));
        // Restore the stack for subsequent segments.
        for (const entry of saved) {
          const { open, close } = wrapper_tags(entry.ref, ctx);
          out.push(open);
          stack.push({ ref: entry.ref, close });
        }
      }
    }
  }

  // -- Drain ------------------------------------------------------------
  // `input.length` is only ever a segment *end*, so a completion anchored
  // there never reaches the walk above and has to be emitted here.
  const trailing = completions.get(input.length);
  if (trailing !== undefined) {
    if (current_token_type !== null) {
      out.push("");
      current_token_type = null;
    }
    for (const point of trailing) out.push(render_completion(point, ctx));
  }

  if (current_token_type !== null) out.push("");
  while (stack.length) out.push(stack.pop()!.close);

  // the walk flushes a line's annotations at its newline, so the last line
  // only gets them here, when the input does not end in one.
  if (input.charCodeAt(input.length - 1) !== 10) {
    for (const ann of line_annotations.get(line_starts.length - 1) ?? []) {
      out.push(render_line_annotation(ann, ctx));
    }
  }

  out.push(`
`); return out.join(""); }