-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
utils.ts
167 lines (152 loc) · 4.39 KB
/
utils.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* Copyright (c) 2021 GraphQL Contributors.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { SchemaConfig } from './typings';
import type {
IRange as GraphQLRange,
IPosition as GraphQLPosition,
Diagnostic,
CompletionItem as GraphQLCompletionItem,
} from 'graphql-language-service';
import type { editor } from 'monaco-editor';
import { buildASTSchema, printSchema } from 'graphql';
import { Position } from 'graphql-language-service';
// for backwards compatibility
export const getModelLanguageId = (model: editor.ITextModel) => {
if ('getModeId' in model) {
// for <0.30.0 support
// @ts-expect-error
return model.getModeId();
}
return model.getLanguageId();
};
export type MonacoCompletionItem = monaco.languages.CompletionItem & {
isDeprecated?: boolean;
deprecationReason?: string | null;
};
export function toMonacoRange(range: GraphQLRange): monaco.IRange {
return {
startLineNumber: range.start.line + 1,
startColumn: range.start.character + 1,
endLineNumber: range.end.line + 1,
endColumn: range.end.character + 1,
};
}
export function toGraphQLPosition(position: monaco.Position): GraphQLPosition {
return new Position(position.lineNumber - 1, position.column - 1);
}
export type GraphQLWorkerCompletionItem = GraphQLCompletionItem & {
range?: monaco.IRange;
command?: monaco.languages.CompletionItem['command'];
};
export function toCompletion(
entry: GraphQLCompletionItem,
range?: GraphQLRange,
): GraphQLWorkerCompletionItem {
const results: GraphQLWorkerCompletionItem = {
label: entry.label,
insertText: entry.insertText,
insertTextFormat: entry.insertTextFormat,
sortText: entry.sortText,
filterText: entry.filterText,
documentation: entry.documentation,
detail: entry.detail,
range: range ? toMonacoRange(range) : undefined,
kind: entry.kind,
};
if (entry.insertTextFormat) {
results.insertTextFormat = entry.insertTextFormat;
}
if (entry.command) {
results.command = { ...entry.command, id: entry.command.command };
}
return results;
}
/**
* Monaco and Vscode have slightly different ideas of marker severity.
* for example, vscode has Error = 1, whereas monaco has Error = 8. this takes care of that
* @param severity {DiagnosticSeverity} optional vscode diagnostic severity to convert to monaco MarkerSeverity
* @returns {monaco.MarkerSeverity} the matching marker severity level on monaco's terms
*/
// export function toMonacoSeverity(severity?: Diagnostic['severity']): monaco.MarkerSeverity {
// switch (severity) {
// case 1: {
// return monaco.MarkerSeverity.Error
// }
// case 4: {
// return monaco.MarkerSeverity.Hint
// }
// case 3: {
// return monaco.MarkerSeverity.Info
// }
// case 2: {
// return monaco.MarkerSeverity.Warning
// }
// default: {
// return monaco.MarkerSeverity.Warning
// }
// }
// }
export function toMarkerData(
diagnostic: Diagnostic,
): monaco.editor.IMarkerData {
return {
startLineNumber: diagnostic.range.start.line + 1,
endLineNumber: diagnostic.range.end.line + 1,
startColumn: diagnostic.range.start.character + 1,
endColumn: diagnostic.range.end.character,
message: diagnostic.message,
severity: 5,
// severity: toMonacoSeverity(diagnostic.severity),
code: (diagnostic.code as string) || undefined,
};
}
/**
* Send the most minimal string representation
* to the worker for language service instantiation
*/
export const getStringSchema = (schemaConfig: SchemaConfig) => {
const {
schema: graphQLSchema,
documentAST,
introspectionJSON,
introspectionJSONString,
documentString,
...rest
} = schemaConfig;
if (graphQLSchema) {
return {
...rest,
documentString: printSchema(graphQLSchema),
};
}
if (introspectionJSONString) {
return {
...rest,
introspectionJSONString,
};
}
if (documentString) {
return {
...rest,
documentString,
};
}
if (introspectionJSON) {
return {
...rest,
introspectionJSONString: JSON.stringify(introspectionJSON),
};
}
if (documentAST) {
const schema = buildASTSchema(documentAST, rest.buildSchemaOptions);
return {
...rest,
documentString: printSchema(schema),
};
}
throw new Error('no schema supplied');
};