Add tests and fix up the ascent query to find the smallest matching node.

This commit is contained in:
Ashi Krishnan
2018-07-20 18:16:22 -04:00
parent 176ee2a3b3
commit 6fe8b54350

View File

@@ -355,15 +355,17 @@ class TreeSitterLanguageMode {
const searchEndIndex = Math.max(0, endIndex - 1)
const matches = matcherForSelector(selector)
let smallestNode
this._forEachTreeWithRange(range, tree => {
let node = tree.rootNode.descendantForIndex(startIndex, searchEndIndex)
while (node && !nodeContainsIndices(node, startIndex, endIndex)) {
node = node.parent
console.log(node)
while (node) {
if (nodeContainsIndices(node, startIndex, endIndex) && matches(node.type)) {
if (nodeIsSmaller(node, smallestNode)) smallestNode = node
break
}
node = node.parent
}
if (matches(node.type) && nodeIsSmaller(node, smallestNode)) smallestNode = node
})
if (smallestNode) return rangeForNode(smallestNode)