Commits

Anonymous committed dee5ec0

sourceutils: moved ChangeCollector to codeanalyze

Comments (0)

Files changed (13)

rope/base/codeanalyze.py

 import tokenize
 
 
+class ChangeCollector(object):
+
+    def __init__(self, text):
+        self.text = text
+        self.changes = []
+
+    def add_change(self, start, end, new_text=None):
+        if new_text is None:
+            new_text = self.text[start:end]
+        self.changes.append((start, end, new_text))
+
+    def get_changed(self):
+        if not self.changes:
+            return None
+        def compare_changes(change1, change2):
+            return cmp(change1[:2], change2[:2])
+        self.changes.sort(compare_changes)
+        pieces = []
+        last_changed = 0
+        for change in self.changes:
+            start, end, text = change
+            pieces.append(self.text[last_changed:start] + text)
+            last_changed = end
+        if last_changed < len(self.text):
+            pieces.append(self.text[last_changed:])
+        result = ''.join(pieces)
+        if result != self.text:
+            return result
+
+
 class Lines(object):
 
     def get_line(self, line_number):

rope/base/simplify.py

 
 
 def real_code(source):
-    import rope.refactor.sourceutils
-    collector = rope.refactor.sourceutils.ChangeCollector(source)
+    collector = codeanalyze.ChangeCollector(source)
     for start, end in ignored_regions(source):
         if source[start] == '#':
             replacement = ' ' * (end - start)
             replacement = '"%s"' % (' ' * (end - start - 2))
         collector.add_change(start, end, replacement)
     source = collector.get_changed() or source
-    collector = rope.refactor.sourceutils.ChangeCollector(source)
+    collector = codeanalyze.ChangeCollector(source)
     parens = 0
     for i, c in enumerate(source):
         if c in '({[':

rope/contrib/generate.py

 import rope.base.evaluate
-from rope.base import change, pyobjects, exceptions, pynames, worder
+from rope.base import change, pyobjects, exceptions, pynames, worder, codeanalyze
 from rope.refactor import sourceutils, importutils, functionutils, suites
 
 
         resource = self.info.get_insertion_resource()
         start, end = self.info.get_insertion_offsets()
 
-        collector = sourceutils.ChangeCollector(resource.read())
+        collector = codeanalyze.ChangeCollector(resource.read())
         collector.add_change(start, end, definition)
         changes.add_change(change.ChangeContents(
                            resource, collector.get_changed()))

rope/refactor/change_signature.py

 import copy
 
 import rope.base.exceptions
-from rope.base import pyobjects, taskhandle, evaluate, worder
+from rope.base import pyobjects, taskhandle, evaluate, worder, codeanalyze
 from rope.base.change import ChangeContents, ChangeSet
 from rope.refactor import occurrences, sourceutils, functionutils
 
 
     def get_changed_module(self):
         word_finder = worder.Worder(self.source)
-        change_collector = sourceutils.ChangeCollector(self.source)
+        change_collector = codeanalyze.ChangeCollector(self.source)
         for occurrence in self.occurrence_finder.find_occurrences(self.resource):
             if not occurrence.is_called() and not occurrence.is_defined():
                 continue

rope/refactor/extract.py

 import re
 
-from rope.base import ast
+from rope.base import ast, codeanalyze
 from rope.base.change import ChangeSet, ChangeContents
 from rope.base.exceptions import RefactoringError
 from rope.refactor import (sourceutils, similarfinder,
 
     def extract(self):
         extract_info = self._collect_info()
-        content = sourceutils.ChangeCollector(self.info.source)
+        content = codeanalyze.ChangeCollector(self.info.source)
         definition = extract_info.definition
         lineno, indents = extract_info.definition_location
         offset = self.info.lines.get_line_start(lineno)

rope/refactor/importutils/__init__.py

 from rope.refactor.importutils import module_imports
 from rope.refactor.importutils.importinfo import \
      (NormalImport, FromImport, get_module_name)
+import rope.base.codeanalyze
 
 
 class ImportOrganizer(object):
             pymodule.get_scope(), name)
         occurrence_finder = occurrences.create_finder(
             self.pycore, old_name, old_pyname, imports=False)
-        changes = rope.refactor.sourceutils.ChangeCollector(pymodule.source_code)
+        changes = rope.base.codeanalyze.ChangeCollector(pymodule.source_code)
         for occurrence in occurrence_finder.find_occurrences(pymodule=pymodule):
             start, end = occurrence.get_primary_range()
             if till_dot:

rope/refactor/introduce_parameter.py

 import rope.base.change
-from rope.base import exceptions, evaluate, worder
+from rope.base import exceptions, evaluate, worder, codeanalyze
 from rope.refactor import functionutils, sourceutils, occurrences
 
 
         definition_info = functionutils.DefinitionInfo.read(self.pyfunction)
         definition_info.args_with_defaults.append((new_parameter,
                                                    self._get_primary()))
-        collector = sourceutils.ChangeCollector(self.resource.read())
+        collector = codeanalyze.ChangeCollector(self.resource.read())
         header_start, header_end = self._get_header_offsets()
         body_start, body_end = sourceutils.get_body_region(self.pyfunction)
         collector.add_change(header_start, header_end,

rope/refactor/method_object.py

 import warnings
 
-from rope.base import pyobjects, exceptions, change, evaluate
+from rope.base import pyobjects, exceptions, change, evaluate, codeanalyze
 from rope.refactor import sourceutils, occurrences, rename
 
 
                 'new_class_name parameter is deprecated; use classname',
                 DeprecationWarning, stacklevel=2)
             classname = new_class_name
-        collector = sourceutils.ChangeCollector(self.pymodule.source_code)
+        collector = codeanalyze.ChangeCollector(self.pymodule.source_code)
         start, end = sourceutils.get_body_region(self.pyfunction)
         indents = sourceutils.get_indents(
             self.pymodule.lines, self.pyfunction.get_scope().get_start()) + \

rope/refactor/move.py

             new_name = self.get_method_name()
         resource1, start1, end1, new_content1 = \
             self._get_changes_made_by_old_class(dest_attr, new_name)
-        collector1 = sourceutils.ChangeCollector(resource1.read())
+        collector1 = codeanalyze.ChangeCollector(resource1.read())
         collector1.add_change(start1, end1, new_content1)
 
         resource2, start2, end2, new_content2 = \
         if resource1 == resource2:
             collector1.add_change(start2, end2, new_content2)
         else:
-            collector2 = sourceutils.ChangeCollector(resource2.read())
+            collector2 = codeanalyze.ChangeCollector(resource2.read())
             collector2.add_change(start2, end2, new_content2)
             result = collector2.get_changed()
             import_tools = importutils.ImportTools(self.pycore)
 
     def get_changed_module(self):
         source = self.resource.read()
-        change_collector = sourceutils.ChangeCollector(source)
+        change_collector = codeanalyze.ChangeCollector(source)
         if self.replacement is not None:
             change_collector.add_change(self.skip_start, self.skip_end,
                                         self.replacement)

rope/refactor/rename.py

 import warnings
 
-from rope.base import exceptions, pyobjects, pynames, taskhandle, evaluate, worder
+from rope.base import exceptions, pyobjects, pynames, taskhandle, evaluate, worder, codeanalyze
 from rope.base.change import ChangeSet, ChangeContents, MoveResource
 from rope.refactor import occurrences, sourceutils
 
         source_code = resource.read()
     else:
         source_code = pymodule.source_code
-    change_collector = sourceutils.ChangeCollector(source_code)
+    change_collector = codeanalyze.ChangeCollector(source_code)
     for occurrence in occurrences_finder.find_occurrences(resource, pymodule):
         if replace_primary and occurrence.is_a_fixed_primary():
             continue

rope/refactor/restructure.py

                 return None
             return result
         else:
-            collector = sourceutils.ChangeCollector(self.source)
+            collector = codeanalyze.ChangeCollector(self.source)
             last_end = -1
             for match in self.matches:
                 start, end = match.get_region()
             return self._get_matched_text(self.matched_asts[node])
         start, end = patchedast.node_region(node)
         main_text = self.source[start:end]
-        collector = sourceutils.ChangeCollector(main_text)
+        collector = codeanalyze.ChangeCollector(main_text)
         for node in self._get_nearest_roots(node):
             sub_start, sub_end = patchedast.node_region(node)
             collector.add_change(sub_start - start, sub_end - start,

rope/refactor/similarfinder.py

         return self.names.keys()
 
     def substitute(self, mapping):
-        collector = sourceutils.ChangeCollector(self.template)
+        collector = codeanalyze.ChangeCollector(self.template)
         for name, occurrences in self.names.items():
             for region in occurrences:
                 collector.add_change(region[0], region[1], mapping[name])
 
 def make_pattern(code, variables):
     variables = set(variables)
-    collector = sourceutils.ChangeCollector(code)
+    collector = codeanalyze.ChangeCollector(code)
     def does_match(node, name):
         return isinstance(node, ast.Name) and node.id == name
     finder = RawSimilarFinder(code, does_match=does_match)

rope/refactor/sourceutils.py

 from rope.base import ast, codeanalyze
 
 
-class ChangeCollector(object):
-
-    def __init__(self, text):
-        self.text = text
-        self.changes = []
-
-    def add_change(self, start, end, new_text=None):
-        if new_text is None:
-            new_text = self.text[start:end]
-        self.changes.append((start, end, new_text))
-
-    def get_changed(self):
-        if not self.changes:
-            return None
-        def compare_changes(change1, change2):
-            return cmp(change1[:2], change2[:2])
-        self.changes.sort(compare_changes)
-        pieces = []
-        last_changed = 0
-        for change in self.changes:
-            start, end, text = change
-            pieces.append(self.text[last_changed:start] + text)
-            last_changed = end
-        if last_changed < len(self.text):
-            pieces.append(self.text[last_changed:])
-        result = ''.join(pieces)
-        if result != self.text:
-            return result
-
-
 def get_indents(lines, lineno):
     return codeanalyze.count_line_indents(lines.get_line(lineno))