def find_usages_in_node(node, global_encountered=False): names = [] if isinstance(node, tree.BaseNode): if jedi_utils.is_scope(node): global_encountered = False if node in searched_scopes: return names searched_scopes.add(node) if isinstance(node, tree.Function): d = self._get_def_from_function_params(node, name) if d and d != definition: return [] for c in node.children: dot_names = self._get_dot_names(c) if len(dot_names) > 1 and dot_names[1].value == name.value: continue sub_result = find_usages_in_node(c, global_encountered=global_encountered) if sub_result is None: if not jedi_utils.is_scope(node): return ( None if definition and node != jedi_utils.get_parent_scope(definition) else [definition] ) else: sub_result = [] names.extend(sub_result) if self._is_global_stmt_with_name(c, name.value): global_encountered = True elif isinstance(node, tree.Name) and node.value == name.value: if definition and definition != node: if self._is_name_function_definition(node): if isinstance( jedi_utils.get_parent_scope(jedi_utils.get_parent_scope(node)), tree.Class, ): return [] else: return None if ( node.is_definition() and not global_encountered and ( is_function_definition or jedi_utils.get_parent_scope(node) != jedi_utils.get_parent_scope(definition) ) ): return None if self._is_name_function_definition(definition) and isinstance( jedi_utils.get_parent_scope(jedi_utils.get_parent_scope(definition)), tree.Class, ): return None names.append(node) return names
def _get_positions_correct_but_using_private_parts(self): from jedi import Script tree = jedi_utils.import_tree() locs = [] def process_scope(scope): if isinstance(scope, tree.Function): # process all children after name node, # (otherwise name of global function will be marked as local def) local_names = set() global_names = set() for child in scope.children[2:]: process_node(child, local_names, global_names) else: for child in scope.subscopes: process_scope(child) def process_node(node, local_names, global_names): if isinstance(node, tree.GlobalStmt): global_names.update([n.value for n in node.get_global_names()]) elif isinstance(node, tree.Name): if node.value in global_names: return if node.is_definition(): # local def locs.append(node) local_names.add(node.value) elif node.value in local_names: # use of local locs.append(node) elif isinstance(node, tree.BaseNode): # ref: jedi/parser/grammar*.txt if node.type == "trailer" and node.children[0].value == ".": # this is attribute return if isinstance(node, tree.Function): global_names = set( ) # outer global statement doesn't have effect anymore for child in node.children: process_node(child, local_names, global_names) source = self.text.get('1.0', 'end') script = Script(source + ")") # https://github.com/davidhalter/jedi/issues/897 module = jedi_utils.get_module_node(script) for child in module.children: if isinstance(child, tree.BaseNode) and jedi_utils.is_scope(child): process_scope(child) loc_pos = set( ("%d.%d" % (usage.start_pos[0], usage.start_pos[1]), "%d.%d" % (usage.start_pos[0], usage.start_pos[1] + len(usage.value))) for usage in locs) return loc_pos
def get_positions(self): tree = jedi_utils.import_python_tree() locs = [] def process_scope(scope): if isinstance(scope, tree.Function): # process all children after name node, # (otherwise name of global function will be marked as local def) local_names = set() global_names = set() for child in scope.children[2:]: process_node(child, local_names, global_names) else: if hasattr(scope, "subscopes"): for child in scope.subscopes: process_scope(child) elif hasattr(scope, "children"): for child in scope.children: process_scope(child) def process_node(node, local_names, global_names): if isinstance(node, tree.GlobalStmt): global_names.update([n.value for n in node.get_global_names()]) elif isinstance(node, tree.Name): if node.value in global_names: return if node.is_definition(): # local def locs.append(node) local_names.add(node.value) elif node.value in local_names: # use of local locs.append(node) elif isinstance(node, tree.BaseNode): # ref: jedi/parser/grammar*.txt if node.type == "trailer" and node.children[0].value == ".": # this is attribute return if isinstance(node, tree.Function): global_names = set( ) # outer global statement doesn't have effect anymore for child in node.children: process_node(child, local_names, global_names) source = self.text.get("1.0", "end") module = jedi_utils.parse_source(source) for child in module.children: if isinstance(child, tree.BaseNode) and jedi_utils.is_scope(child): process_scope(child) loc_pos = set(( "%d.%d" % (usage.start_pos[0], usage.start_pos[1]), "%d.%d" % (usage.start_pos[0], usage.start_pos[1] + len(usage.value)), ) for usage in locs) return loc_pos