CodeMirror 6 Highlight Tree Unstyled Tokens Not Emitted

There seems to be a discrepancy between the description of highlightTree and its behavior: empty strings are not emitted from highlightTreeRange and highlightTree as a result.

/// Given a string of code and a language, parse the code in that
/// language and run the tree highlighter over the resulting syntax
/// tree. For each differently-styled range, call `emit` with the
/// extend of the range and the CSS classes (as a space-separated
/// string) that apply to it. `emit` will be called with an empty
/// string for unstyled ranges.
export function highlightTree(
  tree: Tree,
  /// Get the CSS classes used to style a given [tag](#highlight.Tag),
  /// or `null` if it isn't styled.
  getStyle: (tag: Tag) => string | null,
  /// Assign styling to a region of the text. Will only be in order of
  /// position for any ranges where more than zero classes apply.
  /// `classes` is a space separated string of CSS classes.
  putStyle: (from: number, to: number, classes: string) => void
) {
  highlightTreeRange(tree, 0, tree.length, getStyle, putStyle)
}

I’m assuming emit here refers to putStyle since the callback is similar-ish (empty string implies zero classes apply though).

Empty strings are not being emitted because of the following in enter and leave of highlightTreeRange: when spanClass="", span=putStyle is never called.

if (start > spanStart && spanClass) span(spanStart, start, spanClass)

I’ve tried fixing that here: https://github.com/codemirror/highlight/pull/1. I’m only approaching this from the perspective of implementing “runmode” from Codemirror 5, so I’m unsure of how this will affect things outside of that.

I changed the code and only partially updated the docs. The intention was to just call it for styled ranges. This patch fixes the docs to match the code.

1 Like