def get_completions(gql, idx, schema):
"""Creates AST from `gql` query string, finds out exactly where cursor is in
string, and uses `schema` to get appropriate completions. Doesn't protect
against exceptions. They should be handled by calling code.
"""
try: # at module import time this package is not available
from graphql.parser import GraphQLParser
from graphql.lexer import GraphQLLexer
except ImportError:
raise Exception('Install graphql-py with pip for GraphQL autocomplete')
try: # monkey-patch this class, the `t_NULL` method breaks parsing
delattr(GraphQLLexer, 't_NULL')
except AttributeError:
pass
start, end = slurp_word(gql, idx)
gql_parser = GraphQLParser()
ast = gql_parser.parse(gql[:start] + placeholder + gql[end:], lexer=GraphQLLexer())
for query in ast.definitions: # get path if it exists
path = placeholder_path(query, placeholder)
if path is not None:
break
query_type, types = schema
t = resolve_type(path, types, query_type)
fields = types[t]['fields']
completions = []
for f in fields.values():
name = f['name']
args = [a['name'] + ':' for a in f['args']]
args_string = '({})'.format(', '.join(args)) if args else ''
type_name = resolve_field_type(f)
completions.append([
'{}{}\t{}'.format(name, args_string, type_name),
'{}{}'.format(name, args_string),
])
return (
completions,
sublime.INHIBIT_WORD_COMPLETIONS | sublime.INHIBIT_EXPLICIT_COMPLETIONS
)
评论列表
文章目录