src/vs/editor/common/model/bracketPairsTextModelPart/bracketPairsTree/nodeReader.ts/NodeReader/readLongestNodeAt
class NodeReader:	readLongestNodeAt(offset: Length, predicate: (node: AstNode) => boolean): AstNode | undefined {
		if (lengthLessThan(offset, this.lastOffset)) {
			throw new Error('Invalid offset');
		}
		this.lastOffset = offset;

		// Find the longest node of all those that are closest to the current offset.
		while (true) {
			const curNode = lastOrUndefined(this.nextNodes);

			if (!curNode) {
				return undefined;
			}
			const curNodeOffset = lastOrUndefined(this.offsets)!;

			if (lengthLessThan(offset, curNodeOffset)) {
				// The next best node is not here yet.
				// The reader must advance before a cached node is hit.
				return undefined;
			}

			if (lengthLessThan(curNodeOffset, offset)) {
				// The reader is ahead of the current node.
				if (lengthAdd(curNodeOffset, curNode.length) <= offset) {
					// The reader is after the end of the current node.
					this.nextNodeAfterCurrent();
				} else {
					// The reader is somewhere in the current node.
					const nextChildIdx = getNextChildIdx(curNode);
					if (nextChildIdx !== -1) {
						// Go to the first child and repeat.
						this.nextNodes.push(curNode.getChild(nextChildIdx)!);
						this.offsets.push(curNodeOffset);
						this.idxs.push(nextChildIdx);
					} else {
						// We don't have children
						this.nextNodeAfterCurrent();
					}
				}
			} else {
				// readerOffsetBeforeChange === curNodeOffset
				if (predicate(curNode)) {
					this.nextNodeAfterCurrent();
					return curNode;
				} else {
					const nextChildIdx = getNextChildIdx(curNode);
					// look for shorter node
					if (nextChildIdx === -1) {
						// There is no shorter node.
						this.nextNodeAfterCurrent();
						return undefined;
					} else {
						// Descend into first child & repeat.
						this.nextNodes.push(curNode.getChild(nextChildIdx)!);
						this.offsets.push(curNodeOffset);
						this.idxs.push(nextChildIdx);
					}
				}
			}
		}
	}