Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!this._schema) return delegate(fileName, position, options);
const node = this._helper.getNode(fileName, position);
if (!node || node.kind !== ts.SyntaxKind.NoSubstitutionTemplateLiteral) {
return delegate(fileName, position, options);
}
if (this._tagCondition && !isTagged(node, this._tagCondition)) {
return delegate(fileName, position, options);
}
const cursor = position - node.getStart();
const baseLC = this._helper.getLineAndChar(fileName, node.getStart());
const cursorLC = this._helper.getLineAndChar(fileName, position);
const relativeLC = { line: cursorLC.line - baseLC.line, character: cursorLC.character - baseLC.character + 1 };
const p = new SimplePosition(relativeLC);
const text = node.getText().slice(1, cursor + 1); // remove the backquote char
this._logger('Search text: "' + text + '" at ' + cursor + ' position');
const gqlCompletionItems = getAutocompleteSuggestions(this._schema, text, p);
this._logger(JSON.stringify(gqlCompletionItems));
return translateCompletionItems(gqlCompletionItems);
}
CodeMirror.registerHelper('hint', 'graphql', (editor, options) => {
const schema = options.schema;
if (!schema) {
return;
}
const cur = editor.getCursor();
const token = editor.getTokenAt(cur);
const rawResults = getAutocompleteSuggestions(
schema,
editor.getValue(),
cur,
token,
);
/**
* GraphQL language service responds to the autocompletion request with
* a different format:
* type CompletionItem = {
* label: string,
* kind?: number,
* detail?: string,
* documentation?: string,
* // GraphQL Deprecation information
* isDeprecated?: ?string,
* deprecationReason?: ?string,
export async function provideCompletionItems({
position,
model,
schema
}: ProviderItemInput): Promise<
languages.ProviderResult
> {
const graphQLPosition = new GraphQLPosition(
position.lineNumber - 1,
position.column - 1,
);
graphQLPosition.setCharacter(position.column - 1);
graphQLPosition.line = position.lineNumber - 1;
const suggestions = await getAutocompleteSuggestions(
schema,
model.getValue(),
graphQLPosition,
);
// @ts-ignore wants range
return {
// TODO: possibly return different kinds of completion items?
// TODO: (optionally?) show all completion items at first?
suggestions: suggestions.map(s => ({
label: s.label,
kind: s.kind,
detail: s.detail,
documentation: s.documentation,
insertText: s.label,
})),
};
export default async function({schema, query, fileContent, bufferPosition}) {
if (!isCursorInQuery(query, fileContent, bufferPosition)) return []
const cursor = {
line: getRow(fileContent, query, bufferPosition),
character: getColumn(fileContent, query, bufferPosition)
}
const cleanedQuery = cleanQuery(query)
const hints = getAutocompleteSuggestions(schema, cleanedQuery, cursor)
const custom = getCustomSnippets({hints, schema, query: cleanedQuery, cursor})
hints.push(...custom)
if (!hints) return []
return hints.map(hint => {
return {
snippet: hint.label,
displayText: hint.label,
leftLabel: hint.detail,
description: hint.documentation || 'Self descriptive.'
}
})
}
function _getAutocompleteSuggestions(
queryText: string,
point: Position,
schemaPath: string,
): EXIT_CODE {
invariant(
schemaPath,
'A schema path is required to provide GraphQL autocompletion',
);
try {
const schema = schemaPath ? generateSchema(schemaPath) : null;
const resultArray = schema
? getAutocompleteSuggestions(schema, queryText, point)
: [];
const resultObject = resultArray.reduce((prev, cur, index) => {
prev[index] = cur;
return prev;
}, {});
process.stdout.write(JSON.stringify(resultObject, null, 2));
return GRAPHQL_SUCCESS_CODE;
} catch (error) {
process.stderr.write(error);
return GRAPHQL_FAILURE_CODE;
}
}
export default function ({ schema, query, cursor }) {
return getAutocompleteSuggestions(schema, query, cursor)
}
}
}
if (key === 'TAB' && !menuOn) {
qs = ib.getInput();
let bStack = [];
for (let c of qs) {
if (c === '{' || c === '(' || c === '[') {
bStack.push(c);
}
if (c === '}' || c === ')' || c === ']') {
bStack.pop();
}
}
p.setCharacter(qs.length);
let acs = getAutocompleteSuggestions(schema, qs, p);
acs = acs.map(o => o.label);
let bStackACS = bStack.map(c => {
switch (c) {
case '{':
return '}';
case '(':
return ')';
case '[':
return ']';
}
return undefined;
});
if (bStackACS.length > 0) {
acs.unshift(bStackACS[bStackACS.length - 1]);