Anonymous avatar Anonymous committed a01bd9c

Making move refactorings not to change out of project files

Comments (0)

Files changed (1)

rope/refactor/move.py

     """
 
     def __init__(self, project, resource, offset):
+        self.project = project
         self.pycore = project.pycore
         pyname = codeanalyze.get_pyname_at(project.pycore, resource, offset)
         self.method_name = codeanalyze.get_name_at(resource, offset)
                                                               resource2)
                 result = _add_imports_to_module(
                     import_tools, goal_pymodule, new_imports)
-            changes.add_change(ChangeContents(resource2, result))
+            if resource2.project == self.project:
+                changes.add_change(ChangeContents(resource2, result))
 
-        changes.add_change(ChangeContents(resource1,
-                                          collector1.get_changed()))
+        if resource1.project == self.project:
+            changes.add_change(ChangeContents(resource1,
+                                              collector1.get_changed()))
         return changes
 
     def get_method_name(self):
         if self.source == dest:
             raise exceptions.RefactoringError(
                 'Moving global elements to the same module.')
+        return self._calculate_changes(dest, task_handle)
+
+    def _calculate_changes(self, dest, task_handle):
         changes = ChangeSet('Moving global <%s>' % self.old_name)
         job_set = task_handle.create_jobset(
             'Collecting Changes', len(self.pycore.get_python_files()))
-        job_set.started_job('Working on destination module')
-        self._change_destination_module(changes, dest)
-        job_set.finished_job()
-        job_set.started_job('Working on source module')
-        self._change_source_module(changes, dest)
-        job_set.finished_job()
-        self._change_other_modules(changes, dest, job_set)
+        for file_ in self.pycore.get_python_files():
+            job_set.started_job('Working on <%s>' % file_.path)
+            if file_ == self.source:
+                changes.add_change(self._source_module_changes(dest))
+            elif file_ == dest:
+                changes.add_change(self._dest_module_changes(dest))
+            elif self.tools.occurs_in_module(resource=file_):
+                pymodule = self.pycore.resource_to_pyobject(file_)
+                # Changing occurrences
+                source = self.tools.rename_in_module(self._new_name(dest),
+                                                     resource=file_)
+                should_import = source is not None
+                # Removing out of date imports
+                pymodule = self.tools.new_pymodule(pymodule, source)
+                source = self.tools.remove_old_imports(pymodule)
+                # Adding new import
+                if should_import:
+                    pymodule = self.tools.new_pymodule(pymodule, source)
+                    source = self.tools.add_imports(pymodule,
+                                                    [self._new_import(dest)])
+                source = self.tools.new_source(pymodule, source)
+                if source != file_.read():
+                    changes.add_change(ChangeContents(file_, source))
+            job_set.finished_job()
         return changes
 
     def _new_name(self, dest):
     def _new_import(self, dest):
         return self.import_tools.get_import(dest)
 
-    def _change_source_module(self, changes, dest):
+    def _source_module_changes(self, dest):
         handle = _ChangeMoveOccurrencesHandle(self._new_name(dest))
         occurrence_finder = occurrences.FilteredFinder(
             self.pycore, self.old_name, [self.old_pyname])
             # Adding new import
             source = self.tools.add_imports(
                 pymodule, [self._new_import(dest)])
-        changes.add_change(ChangeContents(self.source, source))
+        return ChangeContents(self.source, source)
 
-    def _change_destination_module(self, changes, dest):
+    def _dest_module_changes(self, dest):
         # Changing occurrences
         pymodule = self.pycore.resource_to_pyobject(dest)
         source = self.tools.rename_in_module(self.old_name, pymodule)
         pymodule = self.pycore.get_string_module(source, dest)
         source = self.import_tools.organize_imports(pymodule,
                                                     sort=False, unused=False)
-        changes.add_change(ChangeContents(dest, source))
+        return ChangeContents(dest, source)
 
     def _get_moving_element_with_imports(self):
         return _get_moving_element_with_imports(
         return importutils.get_imports(self.pycore,
                                        self.old_pyname.get_object())
 
-    def _change_other_modules(self, changes, dest, job_set):
-        for file_ in self.pycore.get_python_files():
-            if file_ in (self.source, dest):
-                continue
-            job_set.started_job('Working on <%s>' % file_.path)
-            if not self.tools.occurs_in_module(resource=file_):
-                job_set.finished_job()
-                continue
-            pymodule = self.pycore.resource_to_pyobject(file_)
-            # Changing occurrences
-            source = self.tools.rename_in_module(self._new_name(dest),
-                                                 resource=file_)
-            should_import = source is not None
-            # Removing out of date imports
-            pymodule = self.tools.new_pymodule(pymodule, source)
-            source = self.tools.remove_old_imports(pymodule)
-            # Adding new import
-            if should_import:
-                pymodule = self.tools.new_pymodule(pymodule, source)
-                source = self.tools.add_imports(
-                    pymodule, [self._new_import(dest)])
-            source = self.tools.new_source(pymodule, source)
-            if source != file_.read():
-                changes.add_change(ChangeContents(file_, source))
-            job_set.finished_job()
-
     def _add_imports2(self, pymodule, new_imports):
         source = self.tools.add_imports(pymodule, new_imports)
         if source is None:
     """For moving modules and packages"""
 
     def __init__(self, project, resource):
+        self.project = project
         self.pycore = project.pycore
         if not resource.is_folder() and resource.name == '__init__.py':
             resource = resource.parent
         if dest is None or not dest.is_folder():
             raise exceptions.RefactoringError(
                 'Move destination for modules should be packages.')
+        return self._calculate_changes(dest, task_handle)
+
+    def _calculate_changes(self, dest, task_handle):
         changes = ChangeSet('Moving module <%s>' % self.old_name)
         job_set = task_handle.create_jobset(
-            'Collecting Changes', len(self.pycore.get_python_files()) - 1)
-        self._change_other_modules(changes, dest, job_set)
-        job_set.started_job('Moving main module')
-        self._change_moving_module(changes, dest)
-        job_set.finished_job()
+            'Collecting changes', len(self.pycore.get_python_files()))
+        for module in self.pycore.get_python_files():
+            job_set.started_job('Working on <%s>' % module.path)
+            if module == self.source:
+                self._change_moving_module(changes, dest)
+            else:
+                source = self._change_occurrences_in_module(
+                    self._new_import(dest), self._new_name(dest),
+                    resource=module)
+                if source is not None:
+                    changes.add_change(ChangeContents(module, source))
+            job_set.finished_job()
+        if self.project == self.source.project:
+            changes.add_change(MoveResource(self.source, dest.path))
         return changes
 
     def _new_import(self, dest):
             source = self.tools.new_source(pymodule, source)
             if source != self.source.read():
                 changes.add_change(ChangeContents(self.source, source))
-        changes.add_change(MoveResource(self.source, dest.path))
-
-    def _change_other_modules(self, changes, dest, job_set):
-        for module in self.pycore.get_python_files():
-            if module in (self.source, dest):
-                continue
-            job_set.started_job('Working On <%s>' % module.path)
-            source = self._change_occurrences_in_module(
-                self._new_import(dest), self._new_name(dest), resource=module)
-            if source is not None:
-                changes.add_change(ChangeContents(module, source))
-            job_set.finished_job()
 
     def _change_occurrences_in_module(self, new_import, new_name,
                                       pymodule=None, resource=None):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.