1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.pointToOffset = exports.getASTNodeAtPosition = void 0;
|
4 | const graphql_1 = require("graphql");
|
5 | function getASTNodeAtPosition(query, ast, point) {
|
6 | const offset = pointToOffset(query, point);
|
7 | let nodeContainingPosition;
|
8 | graphql_1.visit(ast, {
|
9 | enter(node) {
|
10 | if (node.kind !== 'Name' &&
|
11 | node.loc &&
|
12 | node.loc.start <= offset &&
|
13 | offset <= node.loc.end) {
|
14 | nodeContainingPosition = node;
|
15 | }
|
16 | else {
|
17 | return false;
|
18 | }
|
19 | },
|
20 | leave(node) {
|
21 | if (node.loc && node.loc.start <= offset && offset <= node.loc.end) {
|
22 | return false;
|
23 | }
|
24 | },
|
25 | });
|
26 | return nodeContainingPosition;
|
27 | }
|
28 | exports.getASTNodeAtPosition = getASTNodeAtPosition;
|
29 | function pointToOffset(text, point) {
|
30 | const linesUntilPosition = text.split('\n').slice(0, point.line);
|
31 | return (point.character +
|
32 | linesUntilPosition
|
33 | .map(line => line.length + 1)
|
34 | .reduce((a, b) => a + b, 0));
|
35 | }
|
36 | exports.pointToOffset = pointToOffset;
|
37 |
|
\ | No newline at end of file |