Skip to content

Commit 21af822

Browse files
authored
improvement: completion for defined variables (#1627)
- completion support for defined variables - monaco adjustment for variable completion insertText
1 parent 4f62a8f commit 21af822

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

packages/graphql-language-service-interface/src/autocompleteUtils.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,17 @@ function filterAndSortList<T extends CompletionItemBase>(
115115
entry,
116116
}));
117117

118-
const conciseMatches = filterNonEmpty(
118+
return filterNonEmpty(
119119
filterNonEmpty(byProximity, pair => pair.proximity <= 2),
120120
pair => !pair.entry.isDeprecated,
121-
);
122-
123-
const sortedMatches = conciseMatches.sort(
124-
(a, b) =>
125-
(a.entry.isDeprecated ? 1 : 0) - (b.entry.isDeprecated ? 1 : 0) ||
126-
a.proximity - b.proximity ||
127-
a.entry.label.length - b.entry.label.length,
128-
);
129-
130-
return sortedMatches.map(pair => pair.entry);
121+
)
122+
.sort(
123+
(a, b) =>
124+
(a.entry.isDeprecated ? 1 : 0) - (b.entry.isDeprecated ? 1 : 0) ||
125+
a.proximity - b.proximity ||
126+
a.entry.label.length - b.entry.label.length,
127+
)
128+
.map(pair => pair.entry);
131129
}
132130

133131
// Filters the array by the predicate, unless it results in an empty array,

packages/graphql-language-service-interface/src/getAutocompleteSuggestions.ts

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import {
1717
GraphQLEnumValue,
1818
GraphQLField,
1919
GraphQLFieldMap,
20+
parse,
21+
visit,
22+
VariableDefinitionNode,
23+
GraphQLNamedType,
2024
} from 'graphql';
2125

2226
import {
@@ -57,7 +61,6 @@ import {
5761
hintList,
5862
objectValues,
5963
} from './autocompleteUtils';
60-
6164
/**
6265
* Given GraphQLSchema, queryText, and context of the current position within
6366
* the source text, provide a list of typeahead entries.
@@ -150,7 +153,38 @@ export function getAutocompleteSuggestions(
150153
(kind === RuleKinds.OBJECT_FIELD && step === 2) ||
151154
(kind === RuleKinds.ARGUMENT && step === 2)
152155
) {
153-
return getSuggestionsForInputValues(token, typeInfo, kind);
156+
return getSuggestionsForInputValues(token, typeInfo, kind as string);
157+
}
158+
159+
// complete for all variables available in the query
160+
if (kind === RuleKinds.VARIABLE && step === 1) {
161+
const queryVariables: VariableDefinitionNode[] = [];
162+
visit(
163+
parse(queryText, {
164+
allowLegacySDLEmptyFields: true,
165+
allowLegacySDLImplementsInterfaces: true,
166+
}),
167+
{
168+
VariableDefinition(node) {
169+
queryVariables.push(node);
170+
},
171+
},
172+
);
173+
174+
return hintList(
175+
token,
176+
queryVariables.map(
177+
variableDef =>
178+
({
179+
label: `$${variableDef.variable.name.value}`,
180+
kind: CompletionItemKind.Variable,
181+
detail:
182+
'name' in variableDef.type
183+
? variableDef.type.name.value
184+
: 'Variable',
185+
} as CompletionItem),
186+
),
187+
);
154188
}
155189

156190
// Fragment type conditions
@@ -417,9 +451,9 @@ function getSuggestionsForVariableDefinition(
417451
return hintList(
418452
token,
419453
// TODO: couldn't get Exclude<> working here
420-
inputTypes.map((type: any) => ({
454+
inputTypes.map((type: GraphQLNamedType) => ({
421455
label: type.name,
422-
documentation: type.description,
456+
documentation: type.description as string,
423457
kind: CompletionItemKind.Variable,
424458
})),
425459
);

packages/monaco-graphql/src/utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ export function toCompletion(
3838
): GraphQLCompletionItem & { range: monaco.IRange } {
3939
return {
4040
label: entry.label,
41-
insertText: entry.insertText || (entry.label as string),
41+
// TODO: when adding variables to getAutocompleteSuggestions, we appended the $.
42+
// this appears to cause an issue in monaco, but not vscode
43+
insertText:
44+
entry.insertText ||
45+
(!entry.label.startsWith('$') ? entry.label : entry.label.substring(1)),
4246
sortText: entry.sortText,
4347
filterText: entry.filterText,
4448
documentation: entry.documentation,

0 commit comments

Comments
 (0)