Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: export more functions #1264

Merged
merged 3 commits into from
Apr 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {
TypeNameMetaFieldDef,
} from 'graphql/type/introspection';
import {
CompletionItem,
ContextToken,
CompletionItemBase,
ContextTokenUnion,
State,
AllTypeInfo,
} from 'graphql-language-service-types';
Expand Down Expand Up @@ -93,21 +93,21 @@ export function objectValues(object: Record<string, any>): Array<any> {
}

// Create the expected hint response given a possible list and a token
export function hintList(
token: ContextToken,
list: Array<CompletionItem>,
): Array<CompletionItem> {
export function hintList<T extends CompletionItemBase>(
token: ContextTokenUnion,
list: Array<T>,
): Array<T> {
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<CompletionItem>,
function filterAndSortList<T extends CompletionItemBase>(
list: Array<T>,
text: string,
): Array<CompletionItem> {
): Array<T> {
if (!text) {
return filterNonEmpty<CompletionItem>(list, entry => !entry.isDeprecated);
return filterNonEmpty<T>(list, entry => !entry.isDeprecated);
}

const byProximity = list.map(entry => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function getSuggestionsForFragmentSpread(
);
}

function getFragmentDefinitions(
export function getFragmentDefinitions(
queryText: string,
): Array<FragmentDefinitionNode> {
const fragmentDefs: FragmentDefinitionNode[] = [];
Expand Down Expand Up @@ -431,7 +431,7 @@ type callbackFnType = (
index: number,
) => void | 'BREAK';

function runOnlineParser(
export function runOnlineParser(
queryText: string,
callback: callbackFnType,
): ContextToken {
Expand Down Expand Up @@ -470,7 +470,7 @@ function runOnlineParser(
};
}

function canUseDirective(
export function canUseDirective(
state: State['prevState'],
directive: GraphQLDirective,
): boolean {
Expand Down
27 changes: 27 additions & 0 deletions packages/graphql-language-service-interface/src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -61,3 +67,24 @@ declare export class GraphQLLanguageService {
filePath: Uri,
): Promise<Array<CompletionItem>>;
}
declare export function canUseDirective(
state: $PropertyType<State, "prevState">,
directive: GraphQLDirective
): boolean;
declare export function getDefinitionState(
tokenState: State
): State | null | void;
declare export function getFragmentDefinitions(
queryText: string
): Array<FragmentDefinitionNode>;
declare export function getTypeInfo(
schema: GraphQLSchema,
tokenState: State
): TypeInfo;
declare export function hintList<T: CompletionItemBase>(
token: ContextTokenUnion,
list: Array<T>
): Array<T>;
declare export function objectValues(object: {
[key: string]: any,
}): Array<any>;
7 changes: 6 additions & 1 deletion packages/graphql-language-service-interface/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export {
hintList,
} from './autocompleteUtils';

export { getAutocompleteSuggestions } from './getAutocompleteSuggestions';
export {
canUseDirective,
getAutocompleteSuggestions,
getFragmentDefinitions,
getTypeInfo,
} from './getAutocompleteSuggestions';

export {
LANGUAGE,
Expand Down
23 changes: 23 additions & 0 deletions packages/graphql-language-service-types/src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions packages/graphql-language-service-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,16 @@ export type ContextToken = {
style: string;
};

export type ContextTokenForCodeMirror = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ContextToken is only used by Codemirror, so lets combine this into one type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't ContextToken used inside graphql-language-service-interface even if you do with vscode?

start: number;
end: number;
string: string;
type: string | null;
state: State;
};

export type ContextTokenUnion = ContextToken | ContextTokenForCodeMirror;

export type AllTypeInfo = {
type: Maybe<GraphQLType>;
parentType: Maybe<GraphQLType>;
Expand Down Expand Up @@ -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<string>;
};

export type CompletionItemForCodeMirror = {
Copy link
Member

@acao acao Jan 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is diffferent about this type? do you just need to make it deprecationReason?: string | null?

Copy link
Contributor Author

@yoshiakis yoshiakis Jan 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a successive PR, getAutocompleteSuggestions for CodeMirror returns an object that only contains properties label, type, documentation, isDeprecated, deprecationReason. It doesn't contain many of properties of CompletionItemType and I don't have a plan to add those properties, so I thought using a type designated for it, which is CompletionItemForCodeMirror, it is easier to understand what getAutocompleteSuggestions for CodeMirror actually returns.

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
Expand Down