Skip to content

Commit

Permalink
TMP
Browse files Browse the repository at this point in the history
  • Loading branch information
lieryan committed May 7, 2024
1 parent c4469af commit fcecc3d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,6 @@ requires = [
'setuptools',
]
build-backend = 'setuptools.build_meta'

[tool.rope]
imports.preferred_import_style = "normal-import"
1 change: 1 addition & 0 deletions rope/base/prefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def get_config(root: Folder, ropefolder: Folder) -> PyToolConfig:
bases=[".ropefolder"],
recursive=False,
global_config=True,
# fall_through=True,
)
return config

Expand Down
4 changes: 4 additions & 0 deletions rope/base/pyscopes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
8 changes: 7 additions & 1 deletion rope/contrib/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
42 changes: 41 additions & 1 deletion ropetest/refactor/movetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit fcecc3d

Please sign in to comment.