Skip to content

Commit 8766102

Browse files
Clip injection ranges to the bounds of the buffer when needed
1 parent 33f193c commit 8766102

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/wasm-tree-sitter-language-mode.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3238,7 +3238,7 @@ class LanguageLayer {
32383238
// Do nothing; we don't need to set this boundary.
32393239
} else {
32403240
// The range must end somewhere within our range.
3241-
//
3241+
//
32423242
// Close the boundaries in the opposite order of how we opened them.
32433243
for (let i = languageScopeIds.length - 1; i >= 0; i--) {
32443244
this.scopeResolver.setBoundary(
@@ -3767,7 +3767,7 @@ class LanguageLayer {
37673767
}
37683768

37693769
getOrParseTree({ force = true, anonymous = false } = {}) {
3770-
if (!this.treeIsDirty || !force) { return this.tree; }
3770+
if (this.tree && (!this.treeIsDirty || !force)) { return this.tree; }
37713771

37723772
// Eventually we'll take this out, but for now it serves as an indicator of
37733773
// how often we have to manually re-parse in between transactions —
@@ -4146,8 +4146,15 @@ class NodeRangeSet {
41464146
const previousRanges = this.previous?.getRanges(buffer);
41474147
let result = [];
41484148

4149-
for (const node of this.nodeSpecs) {
4149+
for (let node of this.nodeSpecs) {
4150+
// An injection point isn't given the point at which the buffer ends, so
4151+
// it's free to return an `endIndex` of `Infinity` here and rely on us to
4152+
// clip it to the boundary of the buffer.
4153+
if (node.endIndex === Infinity) {
4154+
node = this._clipRange(node, buffer);
4155+
}
41504156
let position = node.startPosition, index = node.startIndex;
4157+
41514158
if (node.children && !this.includeChildren) {
41524159
// If `includeChildren` is `false`, we're effectively collecting all
41534160
// the disjoint text nodes that are direct descendants of this node.
@@ -4203,6 +4210,13 @@ class NodeRangeSet {
42034210
return this._consolidateRanges(result);
42044211
}
42054212

4213+
_clipRange(range, buffer) {
4214+
// Convert this range spec to an actual `Range`, clip it, then convert it
4215+
// back to a range spec with accurate `startIndex` and `endIndex` values.
4216+
let clippedRange = buffer.clipRange(rangeForNode(range));
4217+
return rangeToTreeSitterRangeSpec(clippedRange, buffer);
4218+
}
4219+
42064220
// Combine adjacent ranges to minimize the number of boundaries.
42074221
_consolidateRanges(ranges) {
42084222
if (ranges.length === 1) { return ranges; }

0 commit comments

Comments
 (0)