def _goto(self, add_import_name=False): """ Used for goto and related_names. """ goto_path = self.module.get_path_under_cursor() context = self.module.get_context() if next(context) in ('class', 'def'): user_scope = self.parser.user_scope definitions = set([user_scope.name]) search_name = str(user_scope.name) elif isinstance(self.parser.user_stmt, parsing.Import): s, name_part = self._get_on_import_stmt() try: definitions = [s.follow(is_goto=True)[0]] except IndexError: definitions = [] search_name = str(name_part) if add_import_name: import_name = self.parser.user_stmt.get_defined_names() # imports have only one name if name_part == import_name[0].names[-1]: definitions.append(import_name[0]) else: stmt = self._get_under_cursor_stmt(goto_path) definitions, search_name = evaluate.goto(stmt) return definitions, search_name
def _goto(self, add_import_name=False): """ Used for goto_assignments and usages. :param add_import_name: Add the the name (if import) to the result. """ def follow_inexistent_imports(defs): """ Imports can be generated, e.g. following `multiprocessing.dummy` generates an import dummy in the multiprocessing module. The Import doesn't exist -> follow. """ definitions = set(defs) for d in defs: if isinstance(d.parent, pr.Import) \ and d.start_pos == (0, 0): i = imports.ImportPath(d.parent).follow(is_goto=True) definitions.remove(d) definitions |= follow_inexistent_imports(i) return definitions goto_path = self._module.get_path_under_cursor() context = self._module.get_context() user_stmt = self._user_stmt() if next(context) in ('class', 'def'): user_scope = self._parser.user_scope definitions = set([user_scope.name]) search_name = unicode(user_scope.name) elif isinstance(user_stmt, pr.Import): s, name_part = self._get_on_import_stmt(user_stmt) try: definitions = [s.follow(is_goto=True)[0]] except IndexError: definitions = [] search_name = unicode(name_part) if add_import_name: import_name = user_stmt.get_defined_names() # imports have only one name if not user_stmt.star \ and name_part == import_name[0].names[-1]: definitions.append(import_name[0]) else: stmt = self._get_under_cursor_stmt(goto_path) defs, search_name = evaluate.goto(stmt) definitions = follow_inexistent_imports(defs) if isinstance(user_stmt, pr.Statement): c = user_stmt.get_commands() if c and not isinstance(c[0], (str, unicode)) \ and c[0].start_pos > self._pos \ and not re.search(r'\.\w+$', goto_path): # The cursor must be after the start, otherwise the # statement is just an assignee. definitions = [user_stmt] return definitions, search_name
def _goto(self, add_import_name=False): """ Used for goto and related_names. :param add_import_name: TODO add description """ def follow_inexistent_imports(defs): """ Imports can be generated, e.g. following `multiprocessing.dummy` generates an import dummy in the multiprocessing module. The Import doesn't exist -> follow. """ definitions = set(defs) for d in defs: if isinstance(d.parent, parsing.Import) \ and d.start_pos == (0, 0): i = imports.ImportPath(d.parent).follow(is_goto=True) definitions.remove(d) definitions |= follow_inexistent_imports(i) return definitions goto_path = self.module.get_path_under_cursor() context = self.module.get_context() if next(context) in ('class', 'def'): user_scope = self.parser.user_scope definitions = set([user_scope.name]) search_name = unicode(user_scope.name) elif isinstance(self.parser.user_stmt, parsing.Import): s, name_part = self._get_on_import_stmt() try: definitions = [s.follow(is_goto=True)[0]] except IndexError: definitions = [] search_name = unicode(name_part) if add_import_name: import_name = self.parser.user_stmt.get_defined_names() # imports have only one name if name_part == import_name[0].names[-1]: definitions.append(import_name[0]) else: stmt = self._get_under_cursor_stmt(goto_path) defs, search_name = evaluate.goto(stmt) definitions = follow_inexistent_imports(defs) return definitions, search_name
def _goto(self, add_import_name=False): """ Used for goto and related_names. :param add_import_name: TODO add description """ def follow_inexistent_imports(defs): """ Imports can be generated, e.g. following `multiprocessing.dummy` generates an import dummy in the multiprocessing module. The Import doesn't exist -> follow. """ definitions = set(defs) for d in defs: if isinstance(d.parent, parsing.Import) \ and d.start_pos == (0, 0): i = imports.ImportPath(d.parent).follow(is_goto=True) definitions.remove(d) definitions |= follow_inexistent_imports(i) return definitions goto_path = self.module.get_path_under_cursor() context = self.module.get_context() if next(context) in ('class', 'def'): user_scope = self.parser.user_scope definitions = set([user_scope.name]) search_name = str(user_scope.name) elif isinstance(self.parser.user_stmt, parsing.Import): s, name_part = self._get_on_import_stmt() try: definitions = [s.follow(is_goto=True)[0]] except IndexError: definitions = [] search_name = str(name_part) if add_import_name: import_name = self.parser.user_stmt.get_defined_names() # imports have only one name if name_part == import_name[0].names[-1]: definitions.append(import_name[0]) else: stmt = self._get_under_cursor_stmt(goto_path) defs, search_name = evaluate.goto(stmt) definitions = follow_inexistent_imports(defs) return definitions, search_name
def check_call(call): result = [] follow = [] # There might be multiple search_name's in one call_path call_path = list(call.generate_call_path()) for i, name in enumerate(call_path): # name is `parsing.NamePart`. if name == search_name: follow.append(call_path[:i + 1]) for f in follow: follow_res, search = evaluate.goto(call.parent_stmt, f) follow_res = related_name_add_import_modules(follow_res, search) compare_follow_res = compare_array(follow_res) # compare to see if they match if any(r in compare_definitions for r in compare_follow_res): scope = call.parent_stmt result.append(api_classes.RelatedName(search, scope)) return result
def check_call(call): result = [] follow = [] # There might be multiple search_name's in one call_path call_path = list(call.generate_call_path()) for i, name in enumerate(call_path): # name is `parsing.NamePart`. if name == search_name: follow.append(call_path[:i + 1]) for f in follow: follow_res, search = evaluate.goto(call.parent_stmt, f) follow_res = related_name_add_import_modules(follow_res, search) #print follow_res, [d.parent for d in follow_res] # compare to see if they match if any(r in definitions for r in follow_res): scope = call.parent_stmt result.append(api_classes.RelatedName(search, scope)) return result
def check_call(call): result = [] follow = [] # There might be multiple search_name's in one call_path call_path = list(call.generate_call_path()) for i, name in enumerate(call_path): # name is `pr.NamePart`. if name == search_name: follow.append(call_path[:i + 1]) for f in follow: follow_res, search = evaluate.goto(call.parent, f) follow_res = usages_add_import_modules(follow_res, search) compare_follow_res = compare_array(follow_res) # compare to see if they match if any(r in compare_definitions for r in compare_follow_res): scope = call.parent result.append(api_classes.Usage(search, scope)) return result