From 6fe8b543505080dcbd47f555f26f45e371cb6302 Mon Sep 17 00:00:00 2001 From: Ashi Krishnan Date: Fri, 20 Jul 2018 18:16:22 -0400 Subject: [PATCH] Add tests and fix up the ascent query to find the smallest matching node. --- src/tree-sitter-language-mode.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/tree-sitter-language-mode.js b/src/tree-sitter-language-mode.js index a9818b1ad..2d948c2ee 100644 --- a/src/tree-sitter-language-mode.js +++ b/src/tree-sitter-language-mode.js @@ -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)