From 7a196589f83ef165d617e275deda5658d2de75fd Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 13 Jul 2018 16:07:14 -0700 Subject: [PATCH] Return containing tags from seek in the correct order --- package.json | 2 +- src/tree-sitter-language-mode.js | 30 +++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 5b0bcf0a9..be645ff2d 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,7 @@ "sinon": "1.17.4", "temp": "^0.8.3", "text-buffer": "13.14.5", - "tree-sitter": "0.12.20-1", + "tree-sitter": "0.12.20", "typescript-simple": "1.0.0", "underscore-plus": "^1.6.8", "winreg": "^1.2.1", diff --git a/src/tree-sitter-language-mode.js b/src/tree-sitter-language-mode.js index 6f2c77785..f3e64923f 100644 --- a/src/tree-sitter-language-mode.js +++ b/src/tree-sitter-language-mode.js @@ -512,7 +512,10 @@ class LanguageLayer { let includedRanges = null if (nodeRangeSet) { includedRanges = nodeRangeSet.getRanges() - if (includedRanges.length === 0) return + if (includedRanges.length === 0) { + this.tree = null + return + } } let affectedRange = this.editedRange @@ -650,13 +653,13 @@ class HighlightIterator { } seek (targetPosition) { - const openScopes = [] + const containingTags = [], containingTagStartIndices = [] const targetIndex = this.languageMode.buffer.characterIndexForPosition(targetPosition) for (let i = this.iterators.length - 1; i >= 0; i--) { - openScopes.push(...this.iterators[i].seek(targetIndex)) + this.iterators[i].seek(targetIndex, containingTags, containingTagStartIndices) } this.iterators.sort((a, b) => b.getIndex() - a.getIndex()) - return openScopes + return containingTags } moveToSuccessor () { @@ -702,11 +705,9 @@ class LayerHighlightIterator { this.openTags = [] } - seek (targetIndex) { + seek (targetIndex, containingTags, containingTagStartIndices) { while (this.treeCursor.gotoParent()) {} - const containingTags = [] - this.done = false this.atEnd = true this.closeTags.length = 0 @@ -717,7 +718,7 @@ class LayerHighlightIterator { if (targetIndex >= this.treeCursor.endIndex) { this.done = true - return containingTags + return } let childIndex = -1 @@ -730,7 +731,7 @@ class LayerHighlightIterator { if (scopeName) { const id = this.idForScope(scopeName) if (this.treeCursor.startIndex < targetIndex) { - containingTags.push(id) + insertContainingTag(id, this.treeCursor.startIndex, containingTags, containingTagStartIndices) } else { this.atEnd = false this.openTags.push(id) @@ -940,6 +941,17 @@ class NodeRangeSet { } } +function insertContainingTag (tag, index, tags, indices) { + const i = indices.findIndex(existingIndex => existingIndex > index) + if (i === -1) { + tags.push(tag) + indices.push(index) + } else { + tags.splice(i, 0, tag) + indices.splice(i, 0, index) + } +} + // Return true iff `mouse` is smaller than `house`. Only correct if // mouse and house overlap. //