Anonymous avatar Anonymous committed 0a9b5fe

Adding rope.base.libutils.report_change()

Comments (0)

Files changed (5)

docs/dev/library.txt

 performing it on changed scopes.
 
 But since rope is not notified about the changes the IDE performs, you
-should tell rope about the change.  You can do that by using
-`rope.base.libtutils.report_change()`.
+should tell rope about the change.  You can do so by using
+`rope.base.libutils.report_change()`.  That is whenever you want to
+change a module you can do something like::
+
+  old_contents = file(path).read()
+  write(path, new_content)
+  libutils.report_change(myproject, path, old_contents)
 
 
 Closing The Project
 modules run much slower.
 
 
-
 `rope.base.taskhandle.TaskHandle`
 =================================
 
 For monitoring the progress and stopping time consuming tasks like
-some of the refactorings.
-
+some of the refactorings.  `Project.do()` and
+`Refactoring.get_changes()` of some refactorings take a keyword
+argument called ``task_handle``.  You can pass a
+`rope.base.taskhandle.TaskHandle` object as this argument.  See its
+methods for more information.
 
 
 Refactorings

docs/dev/workingon.txt

 Being A Better Library
 ======================
 
-- Documenting `rope.base.change`
+- Refactoring classes list in library
+- Adding `rope.base.libutils.report_change()`
 
-* Adding `rope.base.libutils.file_changed()`
-* Refactoring classes list in library?
-* I'm not happy with `History.get_prev_contents()` in `PyCore`
+* I'm not happy with the use of `History.get_prev_contents()` in `PyCore`
 
 * Handling strings in following lines in `patchedast`
 * Using `svn` instead of `pysvn`

rope/base/libutils.py

 import os.path
 
 import rope.base.project
+import rope.base.pycore
 
 
 def path_to_resource(project, path, type=None):
     if type == 'folder':
         return project.get_folder(project_path)
     return None
+
+
+def report_change(project, path, old_content):
+    """Report that the contents of file at `path` was changed
+
+    The new contents of file is retrieved by reading the file.
+
+    """
+    resource = path_to_resource(project, path)
+    if resource is None:
+        return
+    for observer in list(project.observers):
+        observer.resource_changed(resource)
+    rope.base.pycore.perform_soi_on_changed_scopes(project, resource,
+                                                   old_content)

rope/base/pycore.py

         self.project.add_observer(observer)
 
     def _file_changed_for_soi(self, resource, new_resource=None):
-        if resource.exists() and self.is_python_file(resource):
-            try:
-                old_contents = self.project.history.get_prev_contents(resource)
-                new_contents = resource.read()
-                # detecting changes in new_contents relative to old_contents
-                detector = _TextChangeDetector(new_contents, old_contents)
-                def should_analyze(pydefined):
-                    scope = pydefined.get_scope()
-                    return detector.is_changed(scope.get_start(),
-                                               scope.get_end())
-                self.analyze_module(resource, should_analyze)
-            except SyntaxError:
-                pass
+        old_contents = self.project.history.get_prev_contents(resource)
+        if old_contents is not None:
+            perform_soi_on_changed_scopes(self.project, resource, old_contents)
 
     def is_python_file(self, resource):
         return not resource.is_folder() and resource.name.endswith('.py')
             return self.class_lines
 
 
+def perform_soi_on_changed_scopes(project, resource, old_contents):
+    pycore = project.pycore
+    if resource.exists() and pycore.is_python_file(resource):
+        try:
+            new_contents = resource.read()
+            # detecting changes in new_contents relative to old_contents
+            detector = _TextChangeDetector(new_contents, old_contents)
+            def should_analyze(pydefined):
+                scope = pydefined.get_scope()
+                return detector.is_changed(scope.get_start(),
+                                           scope.get_end())
+            pycore.analyze_module(resource, should_analyze)
+        except SyntaxError:
+            pass
+
+
 class _TextChangeDetector(object):
 
     def __init__(self, old, new):

ropetest/advanced_oi_test.py

 import unittest
 
 import rope.base.oi
+import rope.base.libutils
 from ropetest import testutils
 
 
         self.assertTrue(isinstance(p_object.get_type(),
                                    rope.base.builtins.Str))
 
+    def test_report_change_in_libutils(self):
+        code = 'class C(object):\n    pass\ndef f(p):\n    pass\nf(C())\n'
+        mod_file = open(self.mod.real_path, 'w')
+        mod_file.write(code)
+        mod_file.close()
+        rope.base.libutils.report_change(self.project, self.mod.real_path, '')
+        pymod = self.pycore.resource_to_pyobject(self.mod)
+        c_class = pymod.get_attribute('C').get_object()
+        f_scope = pymod.get_attribute('f').get_object().get_scope()
+        p_type = f_scope.get_name('p').get_object().get_type()
+        self.assertEquals(c_class, p_type)
+
 
 def suite():
     result = unittest.TestSuite()
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.