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

feat: use internal suggestions (#132)

Changed files
+50 -23
.changeset
packages
+5
.changeset/hip-poems-remain.md
···
+
---
+
'@0no-co/graphqlsp': minor
+
---
+
+
Use our internal suggestions algo for better arugments and spread-suggestions
+43 -21
packages/graphqlsp/src/autoComplete.ts
···
CharacterStream,
ContextToken,
} from 'graphql-language-service';
-
import { FragmentDefinitionNode, GraphQLSchema, Kind, parse } from 'graphql';
+
import {
+
FragmentDefinitionNode,
+
GraphQLSchema,
+
Kind,
+
parse,
+
print,
+
} from 'graphql';
import {
bubbleUpCallExpression,
···
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;
···
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;
···
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,
+2 -2
packages/graphqlsp/src/quickInfo.ts
···
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 {
···
const hoverInfo = getHoverInformation(
schema.current,
text,
-
new Cursor(foundToken.line, foundToken.start)
+
new Cursor(foundToken.line, foundToken.start - 1)
);
return {