diff --git a/CHANGELOG.md b/CHANGELOG.md index 88e4748e2..85aabb3e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # **Upcoming release** +- #925 Allow `report_change(..., perform_soa=False)` to refresh cached modules without running static object analysis on each editor buffer update. (@wozhenbang2004) - #859 Fix `find_definition`/Rename not resolving `async def` methods called via `self` (@gadievron) - #895 patchedast Starred and keyword now consumes their syntactically expected and ** (@lieryan) - #896 patchedast cleanup and refactoring (@lieryan) diff --git a/docs/library.rst b/docs/library.rst index 6a62c3e00..1b83df5ba 100644 --- a/docs/library.rst +++ b/docs/library.rst @@ -233,6 +233,22 @@ changed scopes of modules, then you should report the changes by calling Note, however, that the use of ``automatic_soa`` is discouraged, because it may slow down saving considerably. +Editors that serve unsaved buffers through a custom ``fscommands.read()`` can +also use ``report_change()`` to refresh Rope's cached modules after a buffer +edit. Pass ``perform_soa=False`` to notify resource observers without running +static object analysis on each edit: + +.. code-block:: python + + # Update the buffer returned by fscommands.read(), then notify Rope. + libutils.report_change(myproject, path, old_contents, perform_soa=False) + +This option applies only to the current call; it does not change the project's +``automatic_soa`` preference. If analysis is deferred across multiple edits, +call ``libutils.analyze_module(myproject, resource)`` when the editor is ready +to analyze the complete module, or retain the last analyzed contents for a +later ``report_change()`` call with analysis enabled. + Closing The Project ------------------- diff --git a/rope/base/libutils.py b/rope/base/libutils.py index 0974b11e7..58d4ed97e 100644 --- a/rope/base/libutils.py +++ b/rope/base/libutils.py @@ -45,18 +45,24 @@ def relative(root, path): return path[len(root) + 1 :] -def report_change(project, path, old_content): +def report_change(project, path, old_content, *, perform_soa=True): """Report that the contents of file at `path` was changed The new contents of file is retrieved by reading the file. + Set `perform_soa` to `False` to skip static object analysis for this + call, while still notifying resource observers and invalidating cached + module data. This does not change the project's `automatic_soa` + preference. The default performs analysis only if that preference + is enabled. + """ resource = path_to_resource(project, path) if resource is None: return for observer in list(project.observers): observer.resource_changed(resource) - if project.pycore.automatic_soa: + if perform_soa and project.pycore.automatic_soa: rope.base.pycore.perform_soa_on_changed_scopes(project, resource, old_content) diff --git a/ropetest/advanced_oi_test.py b/ropetest/advanced_oi_test.py index 79f514ad0..f1a9736b1 100644 --- a/ropetest/advanced_oi_test.py +++ b/ropetest/advanced_oi_test.py @@ -3,6 +3,7 @@ import rope.base.libutils import rope.base.oi +from rope.base import pyobjects from rope.base.builtins import Str from ropetest import testutils @@ -991,6 +992,52 @@ def f(p): p_type = f_scope["p"].get_object().get_type() self.assertEqual(c_class, p_type) + def test_report_change_can_skip_soa(self): + self.project.prefs["automatic_soa"] = True + code = dedent("""\ + class C: + pass + def f(p): + pass + f(C()) + """) + with open(self.mod.real_path, "w") as mod_file: + mod_file.write(code) + + rope.base.libutils.report_change( + self.project, self.mod.real_path, "", perform_soa=False + ) + pymod = self.project.get_pymodule(self.mod) + parameter = pymod["f"].get_object().get_scope()["p"] + self.assertIs(parameter.get_object(), pyobjects.get_unknown()) + self.assertTrue(self.project.pycore.automatic_soa) + + rope.base.libutils.report_change(self.project, self.mod.real_path, "") + pymod = self.project.get_pymodule(self.mod) + parameter = pymod["f"].get_object().get_scope()["p"] + self.assertEqual(parameter.get_object().get_type(), pymod["C"].get_object()) + + def test_report_change_does_not_enable_soa(self): + self.project.prefs["automatic_soa"] = False + code = dedent("""\ + class C: + pass + def f(p): + pass + f(C()) + """) + with open(self.mod.real_path, "w") as mod_file: + mod_file.write(code) + + rope.base.libutils.report_change( + self.project, self.mod.real_path, "", perform_soa=True + ) + + pymod = self.project.get_pymodule(self.mod) + parameter = pymod["f"].get_object().get_scope()["p"] + self.assertIs(parameter.get_object(), pyobjects.get_unknown()) + self.assertFalse(self.project.pycore.automatic_soa) + def test_report_libutils_and_analyze_all_modules(self): code = dedent("""\ class C(object): diff --git a/ropetest/projecttest.py b/ropetest/projecttest.py index 1f1b37d83..af40f3bf5 100644 --- a/ropetest/projecttest.py +++ b/ropetest/projecttest.py @@ -7,7 +7,7 @@ from rope.base.exceptions import ResourceNotFoundError, RopeError from rope.base.fscommands import FileSystemCommands -from rope.base.libutils import path_to_resource +from rope.base.libutils import path_to_resource, report_change from rope.base.project import NoProject, Project, _realpath from rope.base.resourceobserver import FilteredResourceObserver from rope.base.resources import File, Folder @@ -1090,6 +1090,46 @@ def test_fscommands_and_ignored_resources(self): myfile.create() self.assertEqual("", fscommands.log) + def test_report_change_refreshes_unsaved_buffer_without_soa(self): + class Overlay(FileSystemCommands): + def __init__(self): + self.buffers = {} + + def read(self, path): + if path in self.buffers: + return self.buffers[path].encode() + return super().read(path) + + fscommands = Overlay() + self.project = testutils.sample_project( + fscommands=fscommands, automatic_soa=True, ropefolder=None + ) + module = testutils.create_module(self.project, "mod") + old_contents = dedent("""\ + def before(): + return 1 + """) + module.write(old_contents) + cached = self.project.get_pymodule(module) + self.assertIn("before", cached) + new_contents = dedent("""\ + def after(): + return 2 + """) + fscommands.buffers[module.real_path] = new_contents + + report_change( + self.project, module.real_path, old_contents, perform_soa=False + ) + + refreshed = self.project.get_pymodule(module) + self.assertIsNot(cached, refreshed) + self.assertNotIn("before", refreshed) + self.assertIn("after", refreshed) + self.assertEqual(refreshed.source_code, new_contents) + self.assertEqual(pathlib.Path(module.real_path).read_text(), old_contents) + self.assertTrue(self.project.pycore.automatic_soa) + def test_deprecated_fscommands(self): fscommands = _DeprecatedFSCommands() self.project = testutils.sample_project(fscommands=fscommands)