Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
16 changes: 16 additions & 0 deletions docs/library.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------------
Expand Down
10 changes: 8 additions & 2 deletions rope/base/libutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
47 changes: 47 additions & 0 deletions ropetest/advanced_oi_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
42 changes: 41 additions & 1 deletion ropetest/projecttest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading