Skip to content

Commit

Permalink
feat: use internal suggestions (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock authored Dec 10, 2023
1 parent eb74276 commit 8614a80
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-poems-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphqlsp': minor
---

Use our internal suggestions algo for better arugments and spread-suggestions
64 changes: 43 additions & 21 deletions packages/graphqlsp/src/autoComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -31,6 +37,9 @@ export function getGraphQLCompletions(
schema: { current: GraphQLSchema | null },
info: ts.server.PluginCreateInfo
): ts.WithMetadata<ts.CompletionInfo> | 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;

Expand All @@ -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;
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/graphqlsp/src/quickInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8614a80

Please sign in to comment.