Skip to content

Commit

Permalink
fix cm5 hint bug
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed May 19, 2024
1 parent 798d782 commit 0bab7cb
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
8 changes: 4 additions & 4 deletions packages/codemirror-graphql/src/__tests__/hint-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -531,6 +531,7 @@ describe('getAutocompleteSuggestions', () => {
{
label: 'Foo',
detail: 'Human',
documentation: 'fragment Foo on Human',
labelDetails: { detail: 'fragment Foo on Human' },
},
]);
Expand All @@ -543,6 +544,8 @@ describe('getAutocompleteSuggestions', () => {
{
label: 'Foo',
detail: 'Human',
documentation: 'fragment Foo on Human',

labelDetails: { detail: 'fragment Foo on Human' },
},
]);
Expand All @@ -557,6 +560,8 @@ describe('getAutocompleteSuggestions', () => {
{
label: 'Foo',
detail: 'Human',
documentation: 'fragment Foo on Human',

labelDetails: { detail: 'fragment Foo on Human' },
},
]);
Expand All @@ -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' },
},
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ export function getAutocompleteSuggestions(
kind: CompletionItemKind.Function,
insertText: options?.fillLeafsOnComplete
? type.name + '\n'
: undefined,
: type.name,
insertTextMode: InsertTextMode.adjustIndentation,
})),
);
Expand All @@ -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,
})),
Expand All @@ -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);

Check warning on line 490 in packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts

View check run for this annotation

Codecov / codecov/patch

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

Added line #L490 was not covered by tests
}

return [];
}
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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,
};
}),
Expand Down Expand Up @@ -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}`,
},
Expand Down Expand Up @@ -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,
})),
);
Expand All @@ -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,
})),
);
Expand All @@ -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<CompletionItem> {
// 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<CompletionItem> {
const directive = schema.getDirectives().find(d => d.name === state.name);
return hintList(

Check warning on line 1110 in packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts

View check run for this annotation

Codecov / codecov/patch

packages/graphql-language-service/src/interface/getAutocompleteSuggestions.ts#L1109-L1110

Added lines #L1109 - L1110 were not covered by tests
token,
directive?.args.map(arg => ({
label: arg.name,
documentation: arg.description || '',
kind: CompletionItemKind.Field,
})) || [],
);
}

export function getTokenAtPosition(
queryText: string,
Expand Down

0 comments on commit 0bab7cb

Please sign in to comment.