From 0bab7cb3750e20a4437ce258293b9c3b2428564e Mon Sep 17 00:00:00 2001 From: Rikki Schulte Date: Sun, 19 May 2024 10:49:17 +0200 Subject: [PATCH] fix cm5 hint bug --- .../src/__tests__/hint-test.ts | 8 +-- .../getAutocompleteSuggestions-test.ts | 9 +++- .../interface/getAutocompleteSuggestions.ts | 53 +++++++++---------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/packages/codemirror-graphql/src/__tests__/hint-test.ts b/packages/codemirror-graphql/src/__tests__/hint-test.ts index d498dc65b0e..4324ce5eef7 100644 --- a/packages/codemirror-graphql/src/__tests__/hint-test.ts +++ b/packages/codemirror-graphql/src/__tests__/hint-test.ts @@ -975,8 +975,8 @@ describe('graphql-hint', () => { description: 'The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.', }, - { text: 'TestEnum' }, - { text: 'TestInput' }, + { text: 'TestEnum', description: '' }, + { text: 'TestInput', description: '' }, { text: '__TypeKind', description: @@ -1022,8 +1022,8 @@ describe('graphql-hint', () => { description: 'The `ID` scalar type represents a unique identifier, often used to refetch an object or as key for a cache. The ID type appears in a JSON response as a String; however, it is not intended to be human-readable. When expected as an input type, any string (such as `"4"`) or integer (such as `4`) input value will be accepted as an ID.', }, - { text: 'TestEnum' }, - { text: 'TestInput' }, + { text: 'TestEnum', description: '' }, + { text: 'TestInput', description: '' }, { text: '__TypeKind', description: diff --git a/packages/graphql-language-service/src/interface/__tests__/getAutocompleteSuggestions-test.ts b/packages/graphql-language-service/src/interface/__tests__/getAutocompleteSuggestions-test.ts index dfbf88b49d6..9b8c51bb1bd 100644 --- a/packages/graphql-language-service/src/interface/__tests__/getAutocompleteSuggestions-test.ts +++ b/packages/graphql-language-service/src/interface/__tests__/getAutocompleteSuggestions-test.ts @@ -118,7 +118,7 @@ describe('getAutocompleteSuggestions', () => { if (suggestion.command && !options?.ignoreInsert) { response.command = suggestion.command; } - if (suggestion.documentation) { + if (suggestion.documentation?.length) { response.documentation = suggestion.documentation; } if (suggestion.labelDetails && !options?.ignoreInsert) { @@ -531,6 +531,7 @@ describe('getAutocompleteSuggestions', () => { { label: 'Foo', detail: 'Human', + documentation: 'fragment Foo on Human', labelDetails: { detail: 'fragment Foo on Human' }, }, ]); @@ -543,6 +544,8 @@ describe('getAutocompleteSuggestions', () => { { label: 'Foo', detail: 'Human', + documentation: 'fragment Foo on Human', + labelDetails: { detail: 'fragment Foo on Human' }, }, ]); @@ -557,6 +560,8 @@ describe('getAutocompleteSuggestions', () => { { label: 'Foo', detail: 'Human', + documentation: 'fragment Foo on Human', + labelDetails: { detail: 'fragment Foo on Human' }, }, ]); @@ -582,11 +587,13 @@ describe('getAutocompleteSuggestions', () => { { label: 'CharacterDetails', detail: 'Human', + documentation: 'fragment CharacterDetails on Human', labelDetails: { detail: 'fragment CharacterDetails on Human' }, }, { label: 'CharacterDetails2', detail: 'Human', + documentation: 'fragment CharacterDetails2 on Human', labelDetails: { detail: 'fragment CharacterDetails2 on Human' }, }, ]); diff --git a/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts b/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts index 052405bf8d5..d7815602e68 100644 --- a/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts +++ b/packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts @@ -447,7 +447,7 @@ export function getAutocompleteSuggestions( kind: CompletionItemKind.Function, insertText: options?.fillLeafsOnComplete ? type.name + '\n' - : undefined, + : type.name, insertTextMode: InsertTextMode.adjustIndentation, })), ); @@ -462,7 +462,7 @@ export function getAutocompleteSuggestions( kind: CompletionItemKind.Function, insertText: options?.fillLeafsOnComplete ? type.name + '\n$1' - : undefined, + : type.name, insertTextMode: InsertTextMode.adjustIndentation, insertTextFormat: InsertTextFormat.Snippet, })), @@ -486,9 +486,9 @@ export function getAutocompleteSuggestions( if (kind === RuleKinds.DIRECTIVE) { return getSuggestionsForDirective(token, state, schema, kind); } - // if (kind === RuleKinds.DIRECTIVE_DEF) { - // return getSuggestionsForDirectiveArguments(token, state, schema, kind); - // } + if (kind === RuleKinds.DIRECTIVE_DEF) { + return getSuggestionsForDirectiveArguments(token, state, schema, kind); + } return []; } @@ -844,7 +844,7 @@ function getSuggestionsForImplements( kind: CompletionItemKind.Interface, type, } as CompletionItem; - if (type?.description && type.description.length) { + if (type?.description) { result.documentation = type.description; } // TODO: should we report what an interface implements in CompletionItem.detail? @@ -898,7 +898,7 @@ function getSuggestionsForFragmentTypeConditions( const namedType = getNamedType(type); return { label: String(type), - documentation: namedType?.description as string | undefined, + documentation: (namedType?.description as string | undefined) || '', kind: CompletionItemKind.Field, }; }), @@ -949,6 +949,7 @@ function getSuggestionsForFragmentSpread( relevantFrags.map(frag => ({ label: frag.name.value, detail: String(typeMap[frag.typeCondition.name.value]), + documentation: `fragment ${frag.name.value} on ${frag.typeCondition.name.value}`, labelDetails: { detail: `fragment ${frag.name.value} on ${frag.typeCondition.name.value}`, }, @@ -1069,7 +1070,7 @@ function getSuggestionsForVariableDefinition( // TODO: couldn't get Exclude<> working here inputTypes.map((type: GraphQLNamedType) => ({ label: type.name, - documentation: type?.description?.length ? type.description : undefined, + documentation: type?.description || '', kind: CompletionItemKind.Variable, })), ); @@ -1089,9 +1090,7 @@ function getSuggestionsForDirective( token, directives.map(directive => ({ label: directive.name, - documentation: directive?.description?.length - ? directive.description - : undefined, + documentation: directive?.description || '', kind: CompletionItemKind.Function, })), ); @@ -1101,22 +1100,22 @@ function getSuggestionsForDirective( // I thought this added functionality somewhere, but I couldn't write any tests // to execute it. I think it's handled as Arguments -// function getSuggestionsForDirectiveArguments( -// token: ContextToken, -// state: State, -// schema: GraphQLSchema, -// _kind: string, -// ): Array { -// const directive = schema.getDirectives().find(d => d.name === state.name); -// return hintList( -// token, -// directive?.args.map(arg => ({ -// label: arg.name, -// documentation: arg.description || '', -// kind: CompletionItemKind.Field, -// })) || [], -// ); -// } +function getSuggestionsForDirectiveArguments( + token: ContextToken, + state: State, + schema: GraphQLSchema, + _kind: string, +): Array { + const directive = schema.getDirectives().find(d => d.name === state.name); + return hintList( + token, + directive?.args.map(arg => ({ + label: arg.name, + documentation: arg.description || '', + kind: CompletionItemKind.Field, + })) || [], + ); +} export function getTokenAtPosition( queryText: string,