-
Notifications
You must be signed in to change notification settings - Fork 26
/
get-quick-info-at-position.ts
32 lines (31 loc) · 1.31 KB
/
get-quick-info-at-position.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import ts from 'typescript';
import { getHoverInformation } from 'graphql-language-service';
import { AnalysisContext, GetQuickInfoAtPosition } from './types';
import { SimplePosition } from './simple-position';
export function getQuickInfoAtPosition(
ctx: AnalysisContext,
delegate: GetQuickInfoAtPosition,
fileName: string,
position: number,
) {
if (ctx.getScriptSourceHelper().isExcluded(fileName)) return delegate(fileName, position);
const schema = ctx.getSchema();
if (!schema) return delegate(fileName, position);
const node = ctx.findAscendantTemplateNode(fileName, position);
if (!node) return delegate(fileName, position);
const { resolvedInfo } = ctx.resolveTemplateInfo(fileName, node);
if (!resolvedInfo) return delegate(fileName, position);
const { combinedText, getInnerPosition, convertInnerPosition2InnerLocation } = resolvedInfo;
const cursor = new SimplePosition(convertInnerPosition2InnerLocation(getInnerPosition(position).pos));
const result = getHoverInformation(schema, combinedText, cursor);
if (typeof result !== 'string' || !result.length) return delegate(fileName, position);
return {
kind: ts.ScriptElementKind.string,
textSpan: {
start: position,
length: 1,
},
kindModifiers: '',
displayParts: [{ text: result, kind: '' }],
} as ts.QuickInfo;
}