Add walk.findNodeAround

This commit is contained in:
Marijn Haverbeke 2013-01-28 17:17:51 +01:00
parent 1de79a277c
commit 792fa96e93

View File

@ -66,6 +66,26 @@
} }
}; };
// Find the innermost node of a given type that contains the given
// position. Interface similar to findNodeAt.
exports.findNodeAround = function(node, pos, targetType, base, state) {
try {
if (!base) base = exports;
var c = function(node, st, override) {
var type = override || node.type;
var inside = node.start <= pos && node.end >= pos;
if (inside)
base[type](node, st, c);
if (inside && (targetType == null || type == targetType))
throw new Found(node, st);
};
c(node, state);
} catch (e) {
if (e instanceof Found) return e;
throw e;
}
};
// Used to create a custom walker. Will fill in all missing node // Used to create a custom walker. Will fill in all missing node
// type properties with the defaults. // type properties with the defaults.
exports.make = function(funcs, base) { exports.make = function(funcs, base) {