diff --git a/CHANGELOG.md b/CHANGELOG.md index 88e4748e2..5afa27fcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - #922 Classify autoimport project sources by path containment (@marlon-costa-dc) - #926, #929 Raise RefactoringError instead of IndexError/ModuleSyntaxError when extracting an empty or whitespace-only region (@aliassar) - #933 Add type annotations to project.py (@lieryan) +- #918 Add `match_filter` and `import_filter` hooks to `Restructure` (@marlon-costa-dc) - #119, #928 Fix IndexError in rename and find occurrences when a `from` import is the last thing in a module without a trailing newline (@mcepl) - #921 Patch the else suite of try statements that also have a finally (@marlon-costa-dc) - #937 Preserve non-ASCII argument expressions and default values when changing function signatures (@Jinyong-Li) diff --git a/rope/refactor/restructure.py b/rope/refactor/restructure.py index 55a4b2d03..9af935eca 100644 --- a/rope/refactor/restructure.py +++ b/rope/refactor/restructure.py @@ -1,9 +1,17 @@ +from __future__ import annotations + import warnings +from typing import Callable, List, Optional, Sequence from rope.base import ast, builtins, change, codeanalyze, libutils, taskhandle +from rope.base.pyobjectsdef import PyModule +from rope.base.resources import File from rope.refactor import patchedast, similarfinder, sourceutils from rope.refactor.importutils import module_imports +MatchFilter = Callable[[PyModule, similarfinder.Match], bool] +ImportFilter = Callable[[File, List[str]], Sequence[str]] + class Restructure: """A class to perform python restructurings @@ -71,11 +79,29 @@ class Restructure: """ - def __init__(self, project, pattern, goal, args=None, imports=None, wildcards=None): + def __init__( + self, + project, + pattern, + goal, + args=None, + imports=None, + wildcards=None, + match_filter: Optional[MatchFilter] = None, + import_filter: Optional[ImportFilter] = None, + ): """Construct a restructuring See class pydoc for more info about the arguments. + `match_filter`, when given, is called as ``match_filter(pymodule, + match)`` for every match found in a module; only the matches it + returns a true value for are restructured. + + `import_filter`, when given, is called as ``import_filter(resource, + imports)`` before `imports` are added to a changed module; only the + imports it returns are added. + """ self.project = project self.pattern = pattern @@ -87,6 +113,8 @@ def __init__(self, project, pattern, goal, args=None, imports=None, wildcards=No if self.imports is None: self.imports = [] self.wildcards = wildcards + self.match_filter = match_filter + self.import_filter = import_filter self.template = similarfinder.CodeTemplate(self.goal) def get_changes( @@ -150,6 +178,10 @@ def get_changes( pymodule = self.project.get_pymodule(resource) finder = similarfinder.SimilarFinder(pymodule, wildcards=self.wildcards) matches = list(finder.get_matches(self.pattern, self.args)) + if self.match_filter is not None: + matches = [ + match for match in matches if self.match_filter(pymodule, match) + ] computer = self._compute_changes(matches, pymodule) result = computer.get_changed() if result is not None: @@ -168,6 +200,8 @@ def _compute_changes(self, matches, pymodule): ) def _add_imports(self, resource, source, imports): + if self.import_filter is not None: + imports = list(self.import_filter(resource, list(imports))) if not imports: return source import_infos = self._get_import_infos(resource, imports) diff --git a/ropetest/refactor/restructuretest.py b/ropetest/refactor/restructuretest.py index 9db6b77f7..0c29cefa3 100644 --- a/ropetest/refactor/restructuretest.py +++ b/ropetest/refactor/restructuretest.py @@ -1,3 +1,4 @@ +import typing import unittest from textwrap import dedent @@ -182,6 +183,67 @@ def test_not_importing_names_when_there_are_no_changes(self): self.project.do(refactoring.get_changes()) self.assertEqual("a = True\n", self.mod.read()) + def test_match_filter_selects_restructured_matches(self): + self.mod.write("a = 1\nb = 1\n") + seen = [] + + def match_filter(pymodule, match): + seen.append(pymodule.get_resource()) + return match.get_region()[0] > 0 + + refactoring = restructure.Restructure( + self.project, "${x} = 1", "${x} = 2", match_filter=match_filter + ) + self.project.do(refactoring.get_changes()) + self.assertEqual("a = 1\nb = 2\n", self.mod.read()) + self.assertEqual([self.mod, self.mod], seen) + + def test_match_filter_rejecting_every_match_changes_nothing(self): + self.mod.write("a = 1\n") + refactoring = restructure.Restructure( + self.project, + "${x} = 1", + "${x} = 2", + imports=["import myconsts"], + match_filter=lambda pymodule, match: False, + ) + self.assertEqual([], refactoring.get_changes().changes) + self.assertEqual("a = 1\n", self.mod.read()) + + def test_hook_annotations_resolve_at_runtime(self): + hints = typing.get_type_hints(restructure.Restructure.__init__) + self.assertEqual( + typing.Optional[restructure.MatchFilter], hints["match_filter"] + ) + self.assertEqual( + typing.Optional[restructure.ImportFilter], hints["import_filter"] + ) + + def test_import_filter_selects_added_imports(self): + self.mod.write("a = 2\n") + received = [] + + def import_filter(resource, imports): + received.append((resource, imports)) + return [declaration for declaration in imports if "other" not in declaration] + + refactoring = restructure.Restructure( + self.project, + "${a} = 2", + "${a} = myconsts.two", + imports=["import myconsts", "import other"], + import_filter=import_filter, + ) + self.project.do(refactoring.get_changes()) + self.assertEqual( + dedent("""\ + import myconsts + a = myconsts.two + """), + self.mod.read(), + ) + self.assertEqual([(self.mod, ["import myconsts", "import other"])], received) + def test_handling_containing_matches(self): self.mod.write("a = 1 / 2 / 3\n") refactoring = restructure.Restructure(