1 | import CodeMirror from 'codemirror';
|
2 | import 'codemirror/addon/hint/show-hint';
|
3 | import { getAutocompleteSuggestions, Position } from 'graphql-language-service';
|
4 | CodeMirror.registerHelper('hint', 'graphql', (editor, options) => {
|
5 | const schema = options.schema;
|
6 | if (!schema) {
|
7 | return;
|
8 | }
|
9 | const cur = editor.getCursor();
|
10 | const token = editor.getTokenAt(cur);
|
11 | const tokenStart = token.type !== null && /"|\w/.test(token.string[0])
|
12 | ? token.start
|
13 | : token.end;
|
14 | const position = new Position(cur.line, tokenStart);
|
15 | const rawResults = getAutocompleteSuggestions(schema, editor.getValue(), position, token, options.externalFragments);
|
16 | const results = {
|
17 | list: rawResults.map(item => ({
|
18 | text: item.label,
|
19 | type: item.type,
|
20 | description: item.documentation,
|
21 | isDeprecated: item.isDeprecated,
|
22 | deprecationReason: item.deprecationReason,
|
23 | })),
|
24 | from: { line: cur.line, ch: tokenStart },
|
25 | to: { line: cur.line, ch: token.end },
|
26 | };
|
27 | if ((results === null || results === void 0 ? void 0 : results.list) && results.list.length > 0) {
|
28 | results.from = CodeMirror.Pos(results.from.line, results.from.ch);
|
29 | results.to = CodeMirror.Pos(results.to.line, results.to.ch);
|
30 | CodeMirror.signal(editor, 'hasCompletion', editor, results, token);
|
31 | }
|
32 | return results;
|
33 | });
|
34 |
|
\ | No newline at end of file |