From bd01fdd95fddb74b416213cdea17c1e1872ca513 Mon Sep 17 00:00:00 2001 From: Yoshi Sukeda Date: Sun, 5 Apr 2020 02:00:30 +0900 Subject: [PATCH] feat: export more functions (#1264) * add more type aliases * export more functions Co-authored-by: Rikki Schulte --- .../src/autocompleteUtils.ts | 20 +++++++------- .../src/getAutocompleteSuggestions.ts | 6 ++--- .../src/index.js.flow | 27 +++++++++++++++++++ .../src/index.ts | 7 ++++- .../src/index.js.flow | 23 ++++++++++++++++ .../src/index.ts | 23 ++++++++++++++++ 6 files changed, 92 insertions(+), 14 deletions(-) diff --git a/packages/graphql-language-service-interface/src/autocompleteUtils.ts b/packages/graphql-language-service-interface/src/autocompleteUtils.ts index fe670dccb3d..b9171673d28 100644 --- a/packages/graphql-language-service-interface/src/autocompleteUtils.ts +++ b/packages/graphql-language-service-interface/src/autocompleteUtils.ts @@ -15,8 +15,8 @@ import { TypeNameMetaFieldDef, } from 'graphql/type/introspection'; import { - CompletionItem, - ContextToken, + CompletionItemBase, + ContextTokenUnion, State, AllTypeInfo, } from 'graphql-language-service-types'; @@ -93,21 +93,21 @@ export function objectValues(object: Record): Array { } // Create the expected hint response given a possible list and a token -export function hintList( - token: ContextToken, - list: Array, -): Array { +export function hintList( + token: ContextTokenUnion, + list: Array, +): Array { return filterAndSortList(list, normalizeText(token.string)); } // Given a list of hint entries and currently typed text, sort and filter to // provide a concise list. -function filterAndSortList( - list: Array, +function filterAndSortList( + list: Array, text: string, -): Array { +): Array { if (!text) { - return filterNonEmpty(list, entry => !entry.isDeprecated); + return filterNonEmpty(list, entry => !entry.isDeprecated); } const byProximity = list.map(entry => ({ diff --git a/packages/graphql-language-service-interface/src/getAutocompleteSuggestions.ts b/packages/graphql-language-service-interface/src/getAutocompleteSuggestions.ts index daf2823ae6d..8393abaa603 100644 --- a/packages/graphql-language-service-interface/src/getAutocompleteSuggestions.ts +++ b/packages/graphql-language-service-interface/src/getAutocompleteSuggestions.ts @@ -320,7 +320,7 @@ function getSuggestionsForFragmentSpread( ); } -function getFragmentDefinitions( +export function getFragmentDefinitions( queryText: string, ): Array { const fragmentDefs: FragmentDefinitionNode[] = []; @@ -431,7 +431,7 @@ type callbackFnType = ( index: number, ) => void | 'BREAK'; -function runOnlineParser( +export function runOnlineParser( queryText: string, callback: callbackFnType, ): ContextToken { @@ -470,7 +470,7 @@ function runOnlineParser( }; } -function canUseDirective( +export function canUseDirective( state: State['prevState'], directive: GraphQLDirective, ): boolean { diff --git a/packages/graphql-language-service-interface/src/index.js.flow b/packages/graphql-language-service-interface/src/index.js.flow index 6f715b3b96a..6e18761bc4e 100644 --- a/packages/graphql-language-service-interface/src/index.js.flow +++ b/packages/graphql-language-service-interface/src/index.js.flow @@ -12,14 +12,20 @@ import type { Outline, Diagnostic, ContextToken, + ContextTokenUnion, Position, DefinitionQueryResult, Uri, GraphQLCache, CompletionItem, + CompletionItemBase, + TypeInfo, + State, } from 'graphql-language-service-types'; import type { ValidationRule, GraphQLSchema } from 'graphql'; import type { Hover } from 'vscode-languageserver-types'; +import { GraphQLDirective } from "graphql"; +import type { FragmentDefinitionNode } from "graphql"; declare export function getOutline(queryText: string): ?Outline; declare export function getDiagnostics( @@ -61,3 +67,24 @@ declare export class GraphQLLanguageService { filePath: Uri, ): Promise>; } +declare export function canUseDirective( + state: $PropertyType, + directive: GraphQLDirective +): boolean; +declare export function getDefinitionState( + tokenState: State +): State | null | void; +declare export function getFragmentDefinitions( + queryText: string +): Array; +declare export function getTypeInfo( + schema: GraphQLSchema, + tokenState: State +): TypeInfo; +declare export function hintList( + token: ContextTokenUnion, + list: Array +): Array; +declare export function objectValues(object: { + [key: string]: any, +}): Array; diff --git a/packages/graphql-language-service-interface/src/index.ts b/packages/graphql-language-service-interface/src/index.ts index 9899e59401a..389c71da9ca 100644 --- a/packages/graphql-language-service-interface/src/index.ts +++ b/packages/graphql-language-service-interface/src/index.ts @@ -15,7 +15,12 @@ export { hintList, } from './autocompleteUtils'; -export { getAutocompleteSuggestions } from './getAutocompleteSuggestions'; +export { + canUseDirective, + getAutocompleteSuggestions, + getFragmentDefinitions, + getTypeInfo, +} from './getAutocompleteSuggestions'; export { LANGUAGE, diff --git a/packages/graphql-language-service-types/src/index.js.flow b/packages/graphql-language-service-types/src/index.js.flow index be096a71c2e..c24067f485e 100644 --- a/packages/graphql-language-service-types/src/index.js.flow +++ b/packages/graphql-language-service-types/src/index.js.flow @@ -222,6 +222,16 @@ export type ContextToken = { style: string, }; +export type ContextTokenForCodeMirror = { + start: number, + end: number, + string: string, + type: string | null, + state: State, +}; + +export type ContextTokenUnion = ContextToken | ContextTokenForCodeMirror; + export type TypeInfo = { type: ?GraphQLType, parentType: ?GraphQLType, @@ -270,6 +280,19 @@ export type CompletionItem = { deprecationReason?: ?string, }; +export type CompletionItemBase = { + label: string, + isDeprecated?: boolean, +}; + +export type CompletionItemForCodeMirror = { + label: string, + type?: GraphQLType, + documentation?: ?string, + isDeprecated?: boolean, + deprecationReason?: ?string, +}; + // Below are basically a copy-paste from Nuclide rpc types for definitions. // Definitions/hyperlink diff --git a/packages/graphql-language-service-types/src/index.ts b/packages/graphql-language-service-types/src/index.ts index 305fc7f240b..96acb495a31 100644 --- a/packages/graphql-language-service-types/src/index.ts +++ b/packages/graphql-language-service-types/src/index.ts @@ -250,6 +250,16 @@ export type ContextToken = { style: string; }; +export type ContextTokenForCodeMirror = { + start: number; + end: number; + string: string; + type: string | null; + state: State; +}; + +export type ContextTokenUnion = ContextToken | ContextTokenForCodeMirror; + export type AllTypeInfo = { type: Maybe; parentType: Maybe; @@ -282,11 +292,24 @@ export type ObjectTypeInfo = { export type Diagnostic = DiagnosticType; +export type CompletionItemBase = { + label: string; + isDeprecated?: boolean; +}; + export type CompletionItem = CompletionItemType & { isDeprecated?: boolean; deprecationReason?: Maybe; }; +export type CompletionItemForCodeMirror = { + label: string; + type?: GraphQLType; + documentation: string | null | undefined; + isDeprecated?: boolean; + deprecationReason: string | null | undefined; +}; + // Below are basically a copy-paste from Nuclide rpc types for definitions. // Definitions/hyperlink