Mirror: TypeScript LSP plugin that finds GraphQL documents in your code and provides diagnostics, auto-complete and hover-information.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: use internal suggestions (#132)

authored by

Jovi De Croock and committed by
GitHub
8614a802 eb74276b

+50 -23
+5
.changeset/hip-poems-remain.md
··· 1 + --- 2 + '@0no-co/graphqlsp': minor 3 + --- 4 + 5 + Use our internal suggestions algo for better arugments and spread-suggestions
+43 -21
packages/graphqlsp/src/autoComplete.ts
··· 11 11 CharacterStream, 12 12 ContextToken, 13 13 } from 'graphql-language-service'; 14 - import { FragmentDefinitionNode, GraphQLSchema, Kind, parse } from 'graphql'; 14 + import { 15 + FragmentDefinitionNode, 16 + GraphQLSchema, 17 + Kind, 18 + parse, 19 + print, 20 + } from 'graphql'; 15 21 16 22 import { 17 23 bubbleUpCallExpression, ··· 31 37 schema: { current: GraphQLSchema | null }, 32 38 info: ts.server.PluginCreateInfo 33 39 ): ts.WithMetadata<ts.CompletionInfo> | undefined { 40 + const logger: any = (msg: string) => 41 + info.project.projectService.logger.info(`[GraphQLSP] ${msg}`); 42 + 34 43 const tagTemplate = info.config.template || 'gql'; 35 44 const isCallExpression = info.config.templateIsCallExpression ?? false; 36 45 ··· 56 65 57 66 const queryText = node.arguments[0].getText(); 58 67 const fragments = getAllFragments(filename, node, info); 59 - const cursor = new Cursor(foundToken.line, foundToken.start); 60 - const items = getAutocompleteSuggestions( 68 + const cursor = new Cursor(foundToken.line, foundToken.start - 1); 69 + const text = `${queryText}\m${fragments.map(x => print(x)).join('\n')}`; 70 + 71 + const [suggestions, spreadSuggestions] = getSuggestionsInternal( 61 72 schema.current, 62 - queryText, 63 - cursor, 64 - undefined, 65 - fragments 73 + text, 74 + cursor 66 75 ); 67 76 68 77 return { 69 78 isGlobalCompletion: false, 70 79 isMemberCompletion: false, 71 80 isNewIdentifierLocation: false, 72 - entries: items.map(suggestion => ({ 73 - ...suggestion, 74 - kind: ts.ScriptElementKind.variableElement, 75 - name: suggestion.label, 76 - kindModifiers: 'declare', 77 - sortText: suggestion.sortText || '0', 78 - labelDetails: { 79 - detail: suggestion.type 80 - ? ' ' + suggestion.type?.toString() 81 - : undefined, 82 - description: suggestion.documentation, 83 - }, 84 - })), 81 + entries: [ 82 + ...suggestions.map(suggestion => ({ 83 + ...suggestion, 84 + kind: ts.ScriptElementKind.variableElement, 85 + name: suggestion.label, 86 + kindModifiers: 'declare', 87 + sortText: suggestion.sortText || '0', 88 + labelDetails: { 89 + detail: suggestion.type 90 + ? ' ' + suggestion.type?.toString() 91 + : undefined, 92 + description: suggestion.documentation, 93 + }, 94 + })), 95 + ...spreadSuggestions.map(suggestion => ({ 96 + ...suggestion, 97 + kind: ts.ScriptElementKind.variableElement, 98 + name: suggestion.label, 99 + insertText: '...' + suggestion.label, 100 + kindModifiers: 'declare', 101 + sortText: '0', 102 + labelDetails: { 103 + description: suggestion.documentation, 104 + }, 105 + })), 106 + ], 85 107 }; 86 108 } else if (ts.isTaggedTemplateExpression(node)) { 87 109 const { template, tag } = node; ··· 107 129 108 130 foundToken.line = foundToken.line + amountOfLines; 109 131 110 - const cursor = new Cursor(foundToken.line, foundToken.start); 132 + const cursor = new Cursor(foundToken.line, foundToken.start - 1); 111 133 112 134 const [suggestions, spreadSuggestions] = getSuggestionsInternal( 113 135 schema.current,
+2 -2
packages/graphqlsp/src/quickInfo.ts
··· 42 42 if (!schema.current || !foundToken) return undefined; 43 43 44 44 const queryText = node.arguments[0].getText(); 45 - const cursor = new Cursor(foundToken.line, foundToken.start); 45 + const cursor = new Cursor(foundToken.line, foundToken.start - 1); 46 46 const hoverInfo = getHoverInformation(schema.current, queryText, cursor); 47 47 48 48 return { ··· 83 83 const hoverInfo = getHoverInformation( 84 84 schema.current, 85 85 text, 86 - new Cursor(foundToken.line, foundToken.start) 86 + new Cursor(foundToken.line, foundToken.start - 1) 87 87 ); 88 88 89 89 return {