diff --git a/src/util/sourceMappingDecoder.js b/src/util/sourceMappingDecoder.js index eb1154499d..a0dc4bdc4d 100644 --- a/src/util/sourceMappingDecoder.js +++ b/src/util/sourceMappingDecoder.js @@ -9,6 +9,15 @@ function SourceMappingDecoder () { // s:l:f:j } +/** + * get a list of nodes that are at the given @arg position + * + * @param {String} astNodeType - type of node to return + * @param {Int} position - cursor position + * @return {Object} ast object given by the compiler + */ +SourceMappingDecoder.prototype.nodesAtPosition = nodesAtPosition + /** * Decode the source mapping for the given @arg index * @@ -156,6 +165,29 @@ function findNodeAtSourceLocation (astNodeType, sourceLocation, ast) { return found } +function nodesAtPosition (astNodeType, position, ast) { + var astWalker = new AstWalker() + var callback = {} + var found = {} + callback['*'] = function (node) { + var nodeLocation = sourceLocationFromAstNode(node) + if (!nodeLocation) { + return + } + if (nodeLocation.start <= position && nodeLocation.start + nodeLocation.length >= position) { + if (!astNodeType || astNodeType === node.name) { + found[node.name] = node + if (astNodeType) return false + } + return true + } else { + return false + } + } + astWalker.walk(ast.AST, callback) + return found +} + function atIndex (index, mapping) { var ret = {} var map = mapping.split(';')