Skip to content

Commit 6ac0086

Browse files
committed
fix: fill leafs on complete config gating, fine tuning
1 parent 8934219 commit 6ac0086

File tree

5 files changed

+55
-56
lines changed

5 files changed

+55
-56
lines changed

packages/graphql-language-service-server/src/MessageProcessor.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ import {
8181
import { NoopLogger, Logger } from './Logger';
8282
import glob from 'fast-glob';
8383
import { isProjectSDLOnly, unwrapProjectSchema } from './common';
84+
import { DefinitionQueryResponse } from 'graphql-language-service/src/interface';
8485

8586
const configDocLink =
8687
'https://www.npmjs.com/package/graphql-language-service-server#user-content-graphql-configuration-file';
@@ -101,7 +102,7 @@ type RelayLSPLocateCommand = (
101102
projectName: string,
102103
typeName: string,
103104
info: AdditionalLocateInfo,
104-
) => string;
105+
) => `${string}:${string}:${string}` | `${string}:${string}` | string;
105106

106107
type GraphQLLocateCommand = (
107108
projectName: string,
@@ -842,7 +843,7 @@ export class MessageProcessor {
842843
position.line -= parentRange.start.line;
843844
}
844845

845-
let result = null;
846+
let result: DefinitionQueryResponse | null = null;
846847

847848
try {
848849
result = await this._languageService.getDefinition(
@@ -893,7 +894,7 @@ export class MessageProcessor {
893894
);
894895
}
895896
}
896-
if (locateCommand && result.printedName) {
897+
if (locateCommand && result && result?.printedName) {
897898
try {
898899
const locateResult = locateCommand(
899900
project.name,
@@ -905,7 +906,8 @@ export class MessageProcessor {
905906
},
906907
);
907908
if (typeof locateResult === 'string') {
908-
const [uri, startLine, endLine] = locateResult.split(':');
909+
const [uri, startLine = '1', endLine = '1'] =
910+
locateResult.split(':');
909911
return {
910912
uri,
911913
range: new Range(

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@ describe('getAutocompleteSuggestions', () => {
293293
expect(result).toEqual([
294294
{
295295
label: 'id',
296-
detail: 'String!',
297296
insertText: 'id: ',
298297
command: suggestionCommand,
299298
insertTextFormat: 2,
@@ -309,7 +308,6 @@ describe('getAutocompleteSuggestions', () => {
309308
expect(result).toEqual([
310309
{
311310
label: 'id',
312-
detail: 'String!',
313311
command: suggestionCommand,
314312
insertText: 'id: ',
315313
insertTextFormat: 2,

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

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,9 @@ export function getAutocompleteSuggestions(
345345
insertTextMode: InsertTextMode.adjustIndentation,
346346
insertTextFormat: InsertTextFormat.Snippet,
347347
command: SuggestionCommand,
348-
detail: String(argDef.type),
348+
labelDetails: {
349+
detail: ' ' + String(argDef.type),
350+
},
349351
documentation: argDef.description ?? undefined,
350352
kind: CompletionItemKind.Variable,
351353
type: argDef.type,
@@ -526,6 +528,27 @@ const getInputInsertText = (
526528
return getInsertText(prefix, type, fallback);
527529
};
528530

531+
/**
532+
* generates a TextSnippet for a field with possible required arguments
533+
* that dynamically adjusts to the number of required arguments
534+
* @param field
535+
* @returns
536+
*/
537+
const getFieldInsertText = (field: GraphQLField<null, null>) => {
538+
const requiredArgs = field.args.filter(arg =>
539+
arg.type.toString().endsWith('!'),
540+
);
541+
if (!requiredArgs.length) {
542+
return;
543+
}
544+
return (
545+
field.name +
546+
`(${requiredArgs.map(
547+
(arg, i) => `${arg.name}: $${i + 1}`,
548+
)}) ${getInsertText('', field.type, '\n')}`
549+
);
550+
};
551+
529552
// /**
530553
// * Choose carefully when to insert the `insertText`!
531554
// * @param field
@@ -591,10 +614,6 @@ function getSuggestionsForExtensionDefinitions(token: ContextToken) {
591614
return hintList(token, typeSystemCompletionItems);
592615
}
593616

594-
// const getFieldInsertText = (field: GraphQLField<null, null>) => {
595-
// return field.name + '($1) {\n\n \n}';
596-
// };
597-
598617
function getSuggestionsForFieldNames(
599618
token: ContextToken,
600619
typeInfo: AllTypeInfo,
@@ -617,6 +636,7 @@ function getSuggestionsForFieldNames(
617636
if (parentType === options?.schema?.getQueryType()) {
618637
fields.push(SchemaMetaFieldDef, TypeMetaFieldDef);
619638
}
639+
620640
return hintList(
621641
token,
622642
fields.map<CompletionItem>((field, index) => {
@@ -632,51 +652,34 @@ function getSuggestionsForFieldNames(
632652
deprecationReason: field.deprecationReason,
633653
kind: CompletionItemKind.Field,
634654
labelDetails: {
635-
detail: field.type.toString().endsWith('!') ? 'NonNull' : undefined,
636-
description: field.description ?? undefined,
655+
detail: ' ' + field.type.toString(),
637656
},
638657
type: field.type,
639658
};
659+
if (options?.fillLeafsOnComplete) {
660+
// const hasArgs =
661+
// // token.state.needsAdvance &&
662+
// // @ts-expect-error
663+
// parentType?._fields[field?.name];
664+
665+
suggestion.insertText = getFieldInsertText(field);
666+
667+
// eslint-disable-next-line logical-assignment-operators
668+
if (!suggestion.insertText) {
669+
suggestion.insertText = getInsertText(
670+
field.name,
671+
field.type,
672+
// if we are replacing a field with arguments, we don't want the extra line
673+
field.name + (token.state.needsAdvance ? '' : '\n'),
674+
);
675+
}
640676

641-
// const hasArgs =
642-
// token.state.needsAdvance &&
643-
// // @ts-expect-error
644-
// parentType?._fields[field?.name];
645-
646-
// if (!hasArgs) {
647-
// suggestion.insertText = getInsertText(
648-
// field.name,
649-
// field.type,
650-
// field.name + '\n',
651-
// );
652-
// }
653-
654-
// const requiredArgs = field.args.filter(arg =>
655-
// arg.type.toString().endsWith('!'),
656-
// );
657-
// if (
658-
// hasArgs &&
659-
// requiredArgs.length &&
660-
// !argDefs?.find(d => requiredArgs.find(a => d.name === a.name))
661-
// ) {
662-
// suggestion.insertText = getFieldInsertText(field);
663-
// }
664-
665-
// if (suggestion.insertText) {
666-
// suggestion.insertTextFormat = InsertTextFormat.Snippet;
667-
// suggestion.insertTextMode = InsertTextMode.adjustIndentation;
668-
// suggestion.command = SuggestionCommand;
669-
// }
670-
671-
// if (options?.fillLeafsOnComplete) {
672-
// // TODO: fillLeafs capability
673-
// const insertText = getInsertText(field);
674-
// if (insertText) {
675-
// suggestion.insertText = field.name + insertText;
676-
// suggestion.insertTextFormat = InsertTextFormat.Snippet;
677-
// suggestion.command = SuggestionCommand;
678-
// }
679-
// }
677+
if (suggestion.insertText) {
678+
suggestion.insertTextFormat = InsertTextFormat.Snippet;
679+
suggestion.insertTextMode = InsertTextMode.adjustIndentation;
680+
suggestion.command = SuggestionCommand;
681+
}
682+
}
680683

681684
return suggestion;
682685
}),

packages/graphql-language-service/src/utils/validateWithCustomRules.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ import {
3535
UniqueVariableNamesRule,
3636
FragmentsOnCompositeTypesRule,
3737
ProvidedRequiredArgumentsRule,
38-
39-
// ProvidedRequiredArgumentsOnDirectivesRule,
4038
} from 'graphql';
4139

4240
const specifiedSDLRules = [
@@ -56,7 +54,6 @@ const specifiedSDLRules = [
5654
UniqueVariableNamesRule,
5755
FragmentsOnCompositeTypesRule,
5856
ProvidedRequiredArgumentsRule,
59-
// ProvidedRequiredArgumentsOnDirectivesRule,
6057
];
6158

6259
/**

packages/vscode-graphql-syntax/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ matching.
1212
- PHP (example: [test.php](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.php))
1313
- Markdown (examples: [test.md](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.md) & [test-py.md](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test-py.md))
1414
- Scala (example: [test.scala](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.scala))
15-
- ruby (example: [test.rb](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.rb))
1615

1716
You'll want to install this if you do not use `graphql-config`, or want to use
1817
the highlighting with other extensions than `vscode-graphql`

0 commit comments

Comments
 (0)