source: node_modules/@swagger-api/apidom-core/es/traversal/findAtOffset.mjs

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.7 KB
Line 
1import { last, pathOr } from 'ramda';
2import { isNumber } from 'ramda-adjunct';
3import { hasElementSourceMap } from "../predicates/index.mjs";
4import { visit } from "./visitor.mjs";
5import toValue from "../transformers/serializers/value/index.mjs";
6class Visitor {
7 result;
8 offset;
9 includeRightBound;
10 constructor({
11 offset = 0,
12 includeRightBound = false
13 } = {}) {
14 this.result = [];
15 this.offset = offset;
16 this.includeRightBound = includeRightBound;
17 }
18 enter(element) {
19 if (!hasElementSourceMap(element)) {
20 return undefined; // dive in
21 }
22 const sourceMapElement = element.getMetaProperty('sourceMap');
23 const charStart = toValue(sourceMapElement.positionStart.get(2));
24 const charEnd = toValue(sourceMapElement.positionEnd.get(2));
25 const isWithinOffsetRange = this.offset >= charStart && (this.offset < charEnd || this.includeRightBound && this.offset <= charEnd);
26 if (isWithinOffsetRange) {
27 this.result.push(element);
28 return undefined; // push to stack and dive in
29 }
30 return false; // skip entire sub-tree
31 }
32}
33// Finds the most inner node at the given offset.
34// If includeRightBound is set, also finds nodes that end at the given offset.
35// findAtOffset :: Number -> Element -> Element | Undefined
36const findAtOffset = (options, element) => {
37 let offset;
38 let includeRightBound;
39 if (isNumber(options)) {
40 offset = options;
41 includeRightBound = false;
42 } else {
43 offset = pathOr(0, ['offset'], options);
44 includeRightBound = pathOr(false, ['includeRightBound'], options);
45 }
46 const visitor = new Visitor({
47 offset,
48 includeRightBound
49 });
50 visit(element, visitor);
51 return last(visitor.result);
52};
53export default findAtOffset;
Note: See TracBrowser for help on using the repository browser.