Skip to content

Commit

Permalink
fix: fill leafs on complete config gating, fine tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
acao committed May 12, 2024
1 parent 1a91fa7 commit 672a83d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 56 deletions.
10 changes: 6 additions & 4 deletions packages/graphql-language-service-server/src/MessageProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import {
import { NoopLogger, Logger } from './Logger';
import glob from 'fast-glob';
import { isProjectSDLOnly, unwrapProjectSchema } from './common';
import { DefinitionQueryResponse } from 'graphql-language-service/src/interface';

const configDocLink =
'https://www.npmjs.com/package/graphql-language-service-server#user-content-graphql-configuration-file';
Expand All @@ -101,7 +102,7 @@ type RelayLSPLocateCommand = (
projectName: string,
typeName: string,
info: AdditionalLocateInfo,
) => string;
) => `${string}:${string}:${string}` | `${string}:${string}` | string;

type GraphQLLocateCommand = (
projectName: string,
Expand Down Expand Up @@ -842,7 +843,7 @@ export class MessageProcessor {
position.line -= parentRange.start.line;
}

let result = null;
let result: DefinitionQueryResponse | null = null;

try {
result = await this._languageService.getDefinition(
Expand Down Expand Up @@ -893,7 +894,7 @@ export class MessageProcessor {
);
}
}
if (locateCommand && result.printedName) {
if (locateCommand && result && result?.printedName) {
try {
const locateResult = locateCommand(

Check warning on line 899 in packages/graphql-language-service-server/src/MessageProcessor.ts

View check run for this annotation

Codecov / codecov/patch

packages/graphql-language-service-server/src/MessageProcessor.ts#L898-L899

Added lines #L898 - L899 were not covered by tests
project.name,
Expand All @@ -905,7 +906,8 @@ export class MessageProcessor {
},
);
if (typeof locateResult === 'string') {
const [uri, startLine, endLine] = locateResult.split(':');
const [uri, startLine = '1', endLine = '1'] =
locateResult.split(':');
return {

Check warning on line 911 in packages/graphql-language-service-server/src/MessageProcessor.ts

View check run for this annotation

Codecov / codecov/patch

packages/graphql-language-service-server/src/MessageProcessor.ts#L910-L911

Added lines #L910 - L911 were not covered by tests
uri,
range: new Range(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,6 @@ describe('getAutocompleteSuggestions', () => {
expect(result).toEqual([
{
label: 'id',
detail: 'String!',
insertText: 'id: ',
command: suggestionCommand,
insertTextFormat: 2,
Expand All @@ -309,7 +308,6 @@ describe('getAutocompleteSuggestions', () => {
expect(result).toEqual([
{
label: 'id',
detail: 'String!',
command: suggestionCommand,
insertText: 'id: ',
insertTextFormat: 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,9 @@ export function getAutocompleteSuggestions(
insertTextMode: InsertTextMode.adjustIndentation,
insertTextFormat: InsertTextFormat.Snippet,
command: SuggestionCommand,
detail: String(argDef.type),
labelDetails: {
detail: ' ' + String(argDef.type),
},
documentation: argDef.description ?? undefined,
kind: CompletionItemKind.Variable,
type: argDef.type,
Expand Down Expand Up @@ -526,6 +528,27 @@ const getInputInsertText = (
return getInsertText(prefix, type, fallback);
};

/**
* generates a TextSnippet for a field with possible required arguments
* that dynamically adjusts to the number of required arguments
* @param field
* @returns
*/
const getFieldInsertText = (field: GraphQLField<null, null>) => {
const requiredArgs = field.args.filter(arg =>
arg.type.toString().endsWith('!'),

Check warning on line 539 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#L538-L539

Added lines #L538 - L539 were not covered by tests
);
if (!requiredArgs.length) {
return;

Check warning on line 542 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#L542

Added line #L542 was not covered by tests
}
return (

Check warning on line 544 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#L544

Added line #L544 was not covered by tests
field.name +
`(${requiredArgs.map(
(arg, i) => `${arg.name}: $${i + 1}`,

Check warning on line 547 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#L547

Added line #L547 was not covered by tests
)}) ${getInsertText('', field.type, '\n')}`
);
};

// /**
// * Choose carefully when to insert the `insertText`!
// * @param field
Expand Down Expand Up @@ -591,10 +614,6 @@ function getSuggestionsForExtensionDefinitions(token: ContextToken) {
return hintList(token, typeSystemCompletionItems);
}

// const getFieldInsertText = (field: GraphQLField<null, null>) => {
// return field.name + '($1) {\n\n \n}';
// };

function getSuggestionsForFieldNames(
token: ContextToken,
typeInfo: AllTypeInfo,
Expand All @@ -617,6 +636,7 @@ function getSuggestionsForFieldNames(
if (parentType === options?.schema?.getQueryType()) {
fields.push(SchemaMetaFieldDef, TypeMetaFieldDef);
}

return hintList(
token,
fields.map<CompletionItem>((field, index) => {
Expand All @@ -632,51 +652,34 @@ function getSuggestionsForFieldNames(
deprecationReason: field.deprecationReason,
kind: CompletionItemKind.Field,
labelDetails: {
detail: field.type.toString().endsWith('!') ? 'NonNull' : undefined,
description: field.description ?? undefined,
detail: ' ' + field.type.toString(),
},
type: field.type,
};
if (options?.fillLeafsOnComplete) {
// const hasArgs =
// // token.state.needsAdvance &&
// // @ts-expect-error
// parentType?._fields[field?.name];

suggestion.insertText = getFieldInsertText(field);

Check warning on line 665 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#L665

Added line #L665 was not covered by tests

// eslint-disable-next-line logical-assignment-operators
if (!suggestion.insertText) {
suggestion.insertText = getInsertText(

Check warning on line 669 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#L669

Added line #L669 was not covered by tests
field.name,
field.type,
// if we are replacing a field with arguments, we don't want the extra line
field.name + (token.state.needsAdvance ? '' : '\n'),
);
}

// const hasArgs =
// token.state.needsAdvance &&
// // @ts-expect-error
// parentType?._fields[field?.name];

// if (!hasArgs) {
// suggestion.insertText = getInsertText(
// field.name,
// field.type,
// field.name + '\n',
// );
// }

// const requiredArgs = field.args.filter(arg =>
// arg.type.toString().endsWith('!'),
// );
// if (
// hasArgs &&
// requiredArgs.length &&
// !argDefs?.find(d => requiredArgs.find(a => d.name === a.name))
// ) {
// suggestion.insertText = getFieldInsertText(field);
// }

// if (suggestion.insertText) {
// suggestion.insertTextFormat = InsertTextFormat.Snippet;
// suggestion.insertTextMode = InsertTextMode.adjustIndentation;
// suggestion.command = SuggestionCommand;
// }

// if (options?.fillLeafsOnComplete) {
// // TODO: fillLeafs capability
// const insertText = getInsertText(field);
// if (insertText) {
// suggestion.insertText = field.name + insertText;
// suggestion.insertTextFormat = InsertTextFormat.Snippet;
// suggestion.command = SuggestionCommand;
// }
// }
if (suggestion.insertText) {
suggestion.insertTextFormat = InsertTextFormat.Snippet;
suggestion.insertTextMode = InsertTextMode.adjustIndentation;

Check warning on line 679 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#L679

Added line #L679 was not covered by tests
suggestion.command = SuggestionCommand;
}
}

return suggestion;
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import {
UniqueVariableNamesRule,
FragmentsOnCompositeTypesRule,
ProvidedRequiredArgumentsRule,

// ProvidedRequiredArgumentsOnDirectivesRule,
} from 'graphql';

const specifiedSDLRules = [
Expand All @@ -56,7 +54,6 @@ const specifiedSDLRules = [
UniqueVariableNamesRule,
FragmentsOnCompositeTypesRule,
ProvidedRequiredArgumentsRule,
// ProvidedRequiredArgumentsOnDirectivesRule,
];

/**
Expand Down
1 change: 0 additions & 1 deletion packages/vscode-graphql-syntax/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ matching.
- PHP (example: [test.php](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.php))
- Markdown (examples: [test.md](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.md) & [test-py.md](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test-py.md))
- Scala (example: [test.scala](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.scala))
- ruby (example: [test.rb](https://github.com/graphql/graphiql/blob/main/packages/vscode-graphql-syntax/tests/__fixtures__/test.rb))

You'll want to install this if you do not use `graphql-config`, or want to use
the highlighting with other extensions than `vscode-graphql`
Expand Down

0 comments on commit 672a83d

Please sign in to comment.