Repository: atom/atom
File: src/tree-sitter-language-mode.js (JavaScript)

Code before:
    let i = this.iterators.length - 1;
    let iterator = this.iterators[i];
    if (!iterator) return;
    const offset = iterator.getOffset();
    if (iterator.languageLayer.isOpaque) {
      this.opaqueLayerDepth = iterator.languageLayer.depth;
    }

    while (i > 0) {
      i--;
      iterator = this.iterators[i];
      if (iterator.startOffset > offset) break;
      if (iterator.languageLayer.isOpaque) {
        const { depth } = iterator.languageLayer;
        if (depth > this.opaqueLayerDepth) {
          this.opaqueLayerDepth = depth;
        }
      }
    }
  }

  getPosition() {
    const iterator = last(this.iterators);
    if (iterator) {
      return iterator.getPosition();
    } else {
      return Point.INFINITY;
    }
  }

  getCloseScopeIds() {
    const iterator = last(this.iterators);
    if (iterator && iterator.languageLayer.depth >= this.opaqueLayerDepth) {
      return iterator.getCloseScopeIds();
    } else {
      return [];
    }
  }

  getOpenScopeIds() {
    const iterator = last(this.iterators);
    if (iterator && iterator.languageLayer.depth >= this.opaqueLayerDepth) {
      return iterator.getOpenScopeIds();
    } else {
      return [];
    }
  }

  logState() {
    const iterator = last(this.iterators);
    if (iterator && iterator.treeCursor) {

Review comment (refactor):
Whenever the ordering of the iterators changes, we call this method which records the depth of any *opaque* layers that span the current position. By consulting this depth value, we prevent highlights from being emitted from parent layers while we're inside of child layers.

In particular, when we've injected a Rust layer into a token tree node in the parent syntax tree, it prevents us from rendering highlights for the token tree node. That would be bad, because we'd get duplicated scopes, and conflicting scopes for some tokens. For example, identifier in the code would get styles as *both* `type_identifier` (via the injected layer) and regular `identifier` (via the original token tree).

Code after:
    } else {
      // If the layer iterator was at the end of its syntax tree, then remove
      // it from the array.
      this.iterators.pop();
    }

    this.detectCoveredScope();
  }

  // Detect whether or not another more deeply-nested language layer has a
  // scope boundary at this same position. If so, the current language layer's
  // scope boundary should not be reported.
  detectCoveredScope() {
    const layerCount = this.iterators.length;
    if (layerCount > 1) {
      const first = this.iterators[layerCount - 1];
      const next = this.iterators[layerCount - 2];
      if (
        next.offset === first.offset &&
        next.atEnd === first.atEnd &&
        next.languageLayer.depth > first.languageLayer.depth
      ) {
        this.currentScopeIsCovered = true;
        return;
      }
    }
    this.currentScopeIsCovered = false;
  }

  getPosition() {
    const iterator = last(this.iterators);
    if (iterator) {
      return iterator.getPosition();
    } else {
      return Point.INFINITY;
    }
  }

  getCloseScopeIds() {
    const iterator = last(this.iterators);
    if (iterator && !this.currentScopeIsCovered) {
      return iterator.getCloseScopeIds();
    }
    return [];
  }

  getOpenScopeIds() {
    const iterator = last(this.iterators);
    if (iterator && !this.currentScopeIsCovered) {
      return iterator.getOpenScopeIds();
    }