-
-
Notifications
You must be signed in to change notification settings - Fork 310
/
graphql-hint.ts
85 lines (79 loc) · 2.51 KB
/
graphql-hint.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
import * as CodeMirror from 'codemirror';
import { getAutocompleteSuggestions } from 'graphql-language-service-interface';
/**
* Registers a "hint" helper for CodeMirror.
*
* Using CodeMirror's "hint" addon: https://codemirror.net/demo/complete.html
* Given an editor, this helper will take the token at the cursor and return a
* list of suggested tokens.
*
* Options:
*
* - schema: GraphQLSchema provides the hinter with positionally relevant info
*
* Additional Events:
*
* - hasCompletion (codemirror, data, token) - signaled when the hinter has a
* new list of completion suggestions.
*
*/
CodeMirror.registerHelper('hint', 'graphql', (editor, options) => {
const schema = options.schema;
if (!schema) {
return;
}
const cur = editor.getCursor();
const token = editor.getTokenAt(cur);
const rawResults = getAutocompleteSuggestions(
schema,
editor.getValue(),
cur,
token,
);
/**
* GraphQL language service responds to the autocompletion request with
* a different format:
* type CompletionItem = {
* label: string,
* kind?: number,
* detail?: string,
* documentation?: string,
* // GraphQL Deprecation information
* isDeprecated?: ?string,
* deprecationReason?: ?string,
* };
*
* Switch to codemirror-compliant format before returning results.
*/
const tokenStart =
token.type !== null && /"|\w/.test(token.string[0])
? token.start
: token.end;
const results = {
list: rawResults.map(item => ({
text: item.label,
type: item.detail && schema.getType(item.detail.replace(/[\[\]!]/g, '')),
description: item.documentation,
isDeprecated: item.isDeprecated,
deprecationReason: item.deprecationReason,
render : options.render || undefined,
typeDetail: item.detail,
})),
from: {line: cur.line, column: tokenStart},
to: {line: cur.line, column: token.end},
};
if (results && results.list && results.list.length > 0) {
results.from = <any>CodeMirror.Pos(results.from.line, results.from.column);
results.to = <any>CodeMirror.Pos(results.to.line, results.to.column);
CodeMirror.signal(editor, 'hasCompletion', editor, results, token);
}
return results;
});