def moving_code_with_imports(project, resource, source): import_tools = importutils.ImportTools(project) pymodule = libutils.get_string_module(project, source, resource) # Strip comment prefix, if any. These need to stay before the moving # section, but imports would be added between them. lines = codeanalyze.SourceLinesAdapter(source) start = 1 while start < lines.length() and lines.get_line(start).startswith('#'): start += 1 moving_prefix = source[:lines.get_line_start(start)] pymodule = libutils.get_string_module(project, source[lines.get_line_start(start):], resource) origin = project.get_pymodule(resource) imports = [] for stmt in import_tools.module_imports(origin).imports: imports.append(stmt.import_info) back_names = [] for name in origin: if name not in pymodule: back_names.append(name) imports.append(import_tools.get_from_import(resource, back_names)) source = _add_imports_to_module(import_tools, pymodule, imports) pymodule = libutils.get_string_module(project, source, resource) source = import_tools.relatives_to_absolutes(pymodule) pymodule = libutils.get_string_module(project, source, resource) source = import_tools.organize_imports(pymodule, selfs=False) pymodule = libutils.get_string_module(project, source, resource) # extracting imports after changes module_imports = import_tools.module_imports(pymodule) imports = [ import_stmt.import_info for import_stmt in module_imports.imports ] start = 1 if module_imports.imports: start = module_imports.imports[-1].end_line lines = codeanalyze.SourceLinesAdapter(source) while start < lines.length() and not lines.get_line(start).strip(): start += 1 # Reinsert the prefix which was removed at the beginning moving = moving_prefix + source[lines.get_line_start(start):] return moving, imports
def moving_code_with_imports(project, resource, source): import_tools = importutils.ImportTools(project) pymodule = libutils.get_string_module(project, source, resource) origin = project.get_pymodule(resource) imports = [] for stmt in import_tools.module_imports(origin).imports: imports.append(stmt.import_info) back_names = [] for name in origin: if name not in pymodule: back_names.append(name) imports.append(import_tools.get_from_import(resource, back_names)) source = _add_imports_to_module(import_tools, pymodule, imports) pymodule = libutils.get_string_module(project, source, resource) source = import_tools.relatives_to_absolutes(pymodule) pymodule = libutils.get_string_module(project, source, resource) source = import_tools.organize_imports(pymodule, selfs=False) pymodule = libutils.get_string_module(project, source, resource) # extracting imports after changes module_imports = import_tools.module_imports(pymodule) imports = [import_stmt.import_info for import_stmt in module_imports.imports] start = 1 if module_imports.imports: start = module_imports.imports[-1].end_line lines = codeanalyze.SourceLinesAdapter(source) while start < lines.length() and not lines.get_line(start).strip(): start += 1 moving = source[lines.get_line_start(start):] return moving, imports
def _inline_variable(project, pymodule, pyname, name, remove=True, region=None, docs=False): definition = _getvardef(pymodule, pyname) start, end = _assigned_lineno(pymodule, pyname) occurrence_finder = occurrences.create_finder(project, name, pyname, docs=docs) changed_source = rename.rename_in_module(occurrence_finder, definition, pymodule=pymodule, replace_primary=True, writes=False, region=region) if changed_source is None: changed_source = pymodule.source_code if remove: lines = codeanalyze.SourceLinesAdapter(changed_source) source = changed_source[:lines.get_line_start(start)] + \ changed_source[lines.get_line_end(end) + 1:] else: source = changed_source return source
def _check_nothing_after_return(self, source, offset): lines = codeanalyze.SourceLinesAdapter(source) lineno = lines.get_line_number(offset) logical_lines = codeanalyze.LogicalLineFinder(lines) lineno = logical_lines.logical_line_in(lineno)[1] if source[lines.get_line_end(lineno):len(source)].strip() != '': raise rope.base.exceptions.RefactoringError( 'Cannot inline functions with statements ' + 'after return statement.')
def replace(code, pattern, goal): """used by other refactorings""" finder = similarfinder.RawSimilarFinder(code) matches = list(finder.get_matches(pattern)) ast = patchedast.get_patched_ast(code) lines = codeanalyze.SourceLinesAdapter(code) template = similarfinder.CodeTemplate(goal) computer = _ChangeComputer(code, ast, lines, template, matches) result = computer.get_changed() if result is None: return code return result
def __init__(self, source, children=False): self.source = _Source(source) self.children = children self.lines = codeanalyze.SourceLinesAdapter(source) self.children_stack = []