From 8614a80202d7eac474be77e8f86c9af0450acf3a Mon Sep 17 00:00:00 2001 From: Jovi De Croock Date: Sun, 10 Dec 2023 11:54:11 +0100 Subject: [PATCH] feat: use internal suggestions (#132) --- .changeset/hip-poems-remain.md | 5 ++ packages/graphqlsp/src/autoComplete.ts | 64 +++++++++++++++++--------- packages/graphqlsp/src/quickInfo.ts | 4 +- 3 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 .changeset/hip-poems-remain.md diff --git a/.changeset/hip-poems-remain.md b/.changeset/hip-poems-remain.md new file mode 100644 index 00000000..431b40cb --- /dev/null +++ b/.changeset/hip-poems-remain.md @@ -0,0 +1,5 @@ +--- +'@0no-co/graphqlsp': minor +--- + +Use our internal suggestions algo for better arugments and spread-suggestions diff --git a/packages/graphqlsp/src/autoComplete.ts b/packages/graphqlsp/src/autoComplete.ts index de1bbec0..7b4dc198 100644 --- a/packages/graphqlsp/src/autoComplete.ts +++ b/packages/graphqlsp/src/autoComplete.ts @@ -11,7 +11,13 @@ import { CharacterStream, ContextToken, } from 'graphql-language-service'; -import { FragmentDefinitionNode, GraphQLSchema, Kind, parse } from 'graphql'; +import { + FragmentDefinitionNode, + GraphQLSchema, + Kind, + parse, + print, +} from 'graphql'; import { bubbleUpCallExpression, @@ -31,6 +37,9 @@ export function getGraphQLCompletions( schema: { current: GraphQLSchema | null }, info: ts.server.PluginCreateInfo ): ts.WithMetadata | undefined { + const logger: any = (msg: string) => + info.project.projectService.logger.info(`[GraphQLSP] ${msg}`); + const tagTemplate = info.config.template || 'gql'; const isCallExpression = info.config.templateIsCallExpression ?? false; @@ -56,32 +65,45 @@ export function getGraphQLCompletions( const queryText = node.arguments[0].getText(); const fragments = getAllFragments(filename, node, info); - const cursor = new Cursor(foundToken.line, foundToken.start); - const items = getAutocompleteSuggestions( + const cursor = new Cursor(foundToken.line, foundToken.start - 1); + const text = `${queryText}\m${fragments.map(x => print(x)).join('\n')}`; + + const [suggestions, spreadSuggestions] = getSuggestionsInternal( schema.current, - queryText, - cursor, - undefined, - fragments + text, + cursor ); return { isGlobalCompletion: false, isMemberCompletion: false, isNewIdentifierLocation: false, - entries: items.map(suggestion => ({ - ...suggestion, - kind: ts.ScriptElementKind.variableElement, - name: suggestion.label, - kindModifiers: 'declare', - sortText: suggestion.sortText || '0', - labelDetails: { - detail: suggestion.type - ? ' ' + suggestion.type?.toString() - : undefined, - description: suggestion.documentation, - }, - })), + entries: [ + ...suggestions.map(suggestion => ({ + ...suggestion, + kind: ts.ScriptElementKind.variableElement, + name: suggestion.label, + kindModifiers: 'declare', + sortText: suggestion.sortText || '0', + labelDetails: { + detail: suggestion.type + ? ' ' + suggestion.type?.toString() + : undefined, + description: suggestion.documentation, + }, + })), + ...spreadSuggestions.map(suggestion => ({ + ...suggestion, + kind: ts.ScriptElementKind.variableElement, + name: suggestion.label, + insertText: '...' + suggestion.label, + kindModifiers: 'declare', + sortText: '0', + labelDetails: { + description: suggestion.documentation, + }, + })), + ], }; } else if (ts.isTaggedTemplateExpression(node)) { const { template, tag } = node; @@ -107,7 +129,7 @@ export function getGraphQLCompletions( foundToken.line = foundToken.line + amountOfLines; - const cursor = new Cursor(foundToken.line, foundToken.start); + const cursor = new Cursor(foundToken.line, foundToken.start - 1); const [suggestions, spreadSuggestions] = getSuggestionsInternal( schema.current, diff --git a/packages/graphqlsp/src/quickInfo.ts b/packages/graphqlsp/src/quickInfo.ts index da8e522b..7a0bd57a 100644 --- a/packages/graphqlsp/src/quickInfo.ts +++ b/packages/graphqlsp/src/quickInfo.ts @@ -42,7 +42,7 @@ export function getGraphQLQuickInfo( if (!schema.current || !foundToken) return undefined; const queryText = node.arguments[0].getText(); - const cursor = new Cursor(foundToken.line, foundToken.start); + const cursor = new Cursor(foundToken.line, foundToken.start - 1); const hoverInfo = getHoverInformation(schema.current, queryText, cursor); return { @@ -83,7 +83,7 @@ export function getGraphQLQuickInfo( const hoverInfo = getHoverInformation( schema.current, text, - new Cursor(foundToken.line, foundToken.start) + new Cursor(foundToken.line, foundToken.start - 1) ); return {