diff --git a/pyproject.toml b/pyproject.toml index f10067297..c05aa688e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -110,3 +110,6 @@ requires = [ 'setuptools', ] build-backend = 'setuptools.build_meta' + +[tool.rope] +imports.preferred_import_style = "normal-import" diff --git a/rope/base/prefs.py b/rope/base/prefs.py index b28610ce4..9bb10900c 100644 --- a/rope/base/prefs.py +++ b/rope/base/prefs.py @@ -339,6 +339,7 @@ def get_config(root: Folder, ropefolder: Folder) -> PyToolConfig: bases=[".ropefolder"], recursive=False, global_config=True, + # fall_through=True, ) return config diff --git a/rope/base/pyscopes.py b/rope/base/pyscopes.py index 565541903..689ed35ea 100644 --- a/rope/base/pyscopes.py +++ b/rope/base/pyscopes.py @@ -96,6 +96,7 @@ def get_logical_end(self): end = property(get_end) logical_end = property(get_logical_end) + # this should be abstract method def get_kind(self): pass @@ -173,6 +174,9 @@ def __init__(self, pycore, pyobject, visitor): self.defineds = None self.visitor = visitor + def get_kind(self): + return "Comprehension" + def _get_names(self): if self.names is None: self._visit_comprehension() diff --git a/rope/contrib/generate.py b/rope/contrib/generate.py index 06a8335f7..c86b42c0d 100644 --- a/rope/contrib/generate.py +++ b/rope/contrib/generate.py @@ -253,7 +253,10 @@ def _get_goal_scope(self): if self.goal_resource: return self.pycore.project.get_pymodule(self.goal_resource).get_scope() else: - return self._get_source_scope() + scope = self._get_source_scope() + # if scope.get_kind() == "Comprehension": + # open("/tmp/xxx", "a").write('--->' + str(scope)) + return scope pyobject = self.primary.get_object() if isinstance(pyobject, pyobjects.PyDefinedObject): return pyobject.get_scope() @@ -275,6 +278,7 @@ def _get_source_scope(self): def get_insertion_lineno(self): lines = self.goal_pymodule.lines if self.goal_scope == self.source_scope: + # open("/tmp/xxx", "a").write(str(self.goal_scope) + ' -- ' + str(self.source_scope)) line_finder = self.goal_pymodule.logical_lines lineno = lines.get_line_number(self.offset) lineno = line_finder.logical_line_in(lineno)[0] @@ -320,6 +324,8 @@ def get_blank_lines(self): base_blanks = 1 if self.goal_scope.get_kind() == "Function": base_blanks = 0 + if self.goal_scope.get_kind() == "Comprehension": + base_blanks = 0 if self.goal_scope == self.source_scope: return (0, base_blanks) return (base_blanks, 0) diff --git a/ropetest/refactor/movetest.py b/ropetest/refactor/movetest.py index 1bf07655d..1683afc0d 100644 --- a/ropetest/refactor/movetest.py +++ b/ropetest/refactor/movetest.py @@ -210,6 +210,25 @@ class AClass(object): self.mod3.read(), ) + def test_changing_other_modules_replacing_from_imports(self) -> None: + """When moving a class from origin_module to destination_module, references to the class in mod3 is updated to point to destination_module""" + self.origin_module.write(dedent("""\ + class AClass(object): + pass + """)) + self.mod3.write(dedent("""\ + from origin_module import AClass + a_var = AClass() + """)) + self._move(self.origin_module, self.origin_module.read().index("AClass") + 1, self.destination_module) + self.assertEqual( + dedent("""\ + from destination_module import AClass + a_var = AClass() + """), + self.mod3.read(), + ) + def test_changing_other_modules_adding_normal_imports(self) -> None: self.origin_module.write(dedent("""\ class AClass(object): @@ -231,7 +250,28 @@ def a_function(): self.mod3.read(), ) - def test_adding_imports_prefer_from_module(self) -> None: + def test_changing_other_modules_adding_from_imports(self) -> None: + self.origin_module.write(dedent("""\ + class AClass(object): + pass + def a_function(): + pass + """)) + self.mod3.write(dedent("""\ + from origin_module import AClass, a_function + a_var = AClass() + a_function()""")) + self._move(self.origin_module, self.origin_module.read().index("AClass") + 1, self.destination_module) + self.assertEqual( + dedent("""\ + from origin_module import a_function + from destination_module import AClass + a_var = AClass() + a_function()"""), + self.mod3.read(), + ) + + def test_adding_imports_prefer_from_module_imports(self) -> None: self.project.prefs["prefer_module_from_imports"] = True self.origin_module.write(dedent("""\ class AClass(object):