def find_value_at_cursor(ast_tree, filename, line, col, root_env=gcl.default_env): """Find the value of the object under the cursor.""" q = gcl.SourceQuery(filename, line, col) rootpath = ast_tree.find_tokens(q) rootpath = path_until(rootpath, is_thunk) if len(rootpath) <= 1: # Just the file tuple itself, or some non-thunk element at the top level return None tup = inflate_context_tuple(rootpath, root_env) try: if isinstance(rootpath[-1], ast.Inherit): # Special case handling of 'Inherit' nodes, show the value that's being # inherited. return tup[rootpath[-1].name] return rootpath[-1].eval(tup.env(tup)) except gcl.EvaluationError as e: return e
def find_completions_at_cursor(ast_tree, filename, line, col, root_env=gcl.default_env): """Find completions at the cursor. Return a dict of { name => Completion } objects. """ q = gcl.SourceQuery(filename, line, col - 1) rootpath = ast_tree.find_tokens(q) if is_identifier_position(rootpath): return find_inherited_key_completions(rootpath, root_env) try: ret = find_deref_completions(rootpath, root_env) or enumerate_scope( rootpath, root_env=root_env) assert isinstance(ret, dict) return ret except gcl.EvaluationError: # Probably an unbound value or something--just return an empty list return {}
def testIncludeDefaultBuiltins(self): tree = gcl.reads('henk = 5', filename='input.gcl') rootpath = tree.find_tokens(gcl.SourceQuery('input.gcl', 1, 1)) return ast_util.enumerate_scope(rootpath, include_default_builtins=True) assert '+' in scope
def readAndQueryScope(source, **kwargs): source, line, col = find_cursor(source) tree = gcl.reads(source, filename='input.gcl', **kwargs) rootpath = tree.find_tokens(gcl.SourceQuery('input.gcl', line, col)) return ast_util.enumerate_scope(rootpath)