def _find_definition(self, scope, name): # if the name is the name of a function definition if isinstance(scope, tree.Function): if scope.children[1] == name: return scope.children[1] # 0th child is keyword "def", 1st is name else: definition = self._get_def_from_function_params(scope, name) if definition: return definition for c in scope.children: if ( isinstance(c, tree.BaseNode) and c.type == "simple_stmt" and isinstance(c.children[0], tree.ImportName) ): for n in c.children[0].get_defined_names(): if n.value == name.value: return n # print(c.path_for_name(name.value)) if ( isinstance(c, tree.Function) and c.children[1].value == name.value and not isinstance(jedi_utils.get_parent_scope(c), tree.Class) ): return c.children[1] if isinstance(c, tree.BaseNode) and c.type == "suite": for x in c.children: if self._is_global_stmt_with_name(x, name.value): return self._find_definition( jedi_utils.get_parent_scope(scope), name ) if ( isinstance(x, tree.Name) and x.is_definition() and x.value == name.value ): return x def_candidate = self._find_def_in_simple_node(x, name) if def_candidate: return def_candidate if not isinstance(scope, tree.Module): return self._find_definition(jedi_utils.get_parent_scope(scope), name) # if name itself is the left side of an assignment statement, then the name is the definition if name.is_definition(): return name return None
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 _find_usages(self, name, stmt, module): # check if stmt is dot qualified, disregard these dot_names = self._get_dot_names(stmt) if len(dot_names) > 1 and dot_names[1].value == name.value: return set() # search for definition definition = self._find_definition(jedi_utils.get_parent_scope(name), name) searched_scopes = set() is_function_definition = self._is_name_function_definition( definition) if definition else False 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 if definition: if self._is_name_function_definition(definition): scope = jedi_utils.get_parent_scope( jedi_utils.get_parent_scope(definition)) else: scope = jedi_utils.get_parent_scope(definition) else: scope = jedi_utils.get_parent_scope(name) usages = find_usages_in_node(scope) return usages