diff --git a/rope/base/worder.py b/rope/base/worder.py index 94089b5a8..64b6313d9 100644 --- a/rope/base/worder.py +++ b/rope/base/worder.py @@ -136,6 +136,18 @@ def __init__(self, code, raw): self.code = code self.raw = raw + def _find_next_word_start(self, offset): + current_offset = offset + + try: + end = self.code.index("\n", offset + 1) + except ValueError: + end = len(self.code) + + while current_offset < end and not self._is_id_char(current_offset): + current_offset += 1 + return current_offset + def _find_word_start(self, offset): current_offset = offset while current_offset >= 0 and self._is_id_char(current_offset): @@ -341,7 +353,7 @@ def is_import_statement(self, offset): line_start = self._get_line_start(last_import) return ( self._find_import_end(last_import + 7) >= offset - and self._find_word_start(line_start) == last_import + and self._find_next_word_start(line_start) == last_import ) def is_from_statement(self, offset): diff --git a/ropetest/codeanalyzetest.py b/ropetest/codeanalyzetest.py index 88a4caf96..ae9497eb5 100644 --- a/ropetest/codeanalyzetest.py +++ b/ropetest/codeanalyzetest.py @@ -292,6 +292,10 @@ def test_is_import_statement(self): result = a.b.c.d.f() + def import_later(): + + import lazy + +++++ """)) word_finder = worder.Worder(code) self.assert_equal_annotation( diff --git a/ropetest/refactor/movetest.py b/ropetest/refactor/movetest.py index 508069aa0..207e1f05c 100644 --- a/ropetest/refactor/movetest.py +++ b/ropetest/refactor/movetest.py @@ -598,6 +598,47 @@ def test_moving_modules_normal_import(self): print(pkg2.pkg3.pkg4.mod4)""") self.assertEqual(expected, self.mod1.read()) + def test_moving_modules_lazy_import(self): + pkg2 = testutils.create_package(self.project, "pkg2") + pkg3 = testutils.create_package(self.project, "pkg3", pkg2) + pkg4 = testutils.create_package(self.project, "pkg4", pkg3) + code = dedent("""\ + def import_later(): + import pkg.mod4 + pkg.mod4""") + self.mod1.write(code) + self._move(self.mod4, None, pkg4) + self.assertTrue(self.project.find_module("pkg2.pkg3.pkg4.mod4") is not None) + expected = dedent("""\ + def import_later(): + import pkg2.pkg3.pkg4.mod4 + pkg2.pkg3.pkg4.mod4""") + self.assertEqual(expected, self.mod1.read()) + + def test_moving_modules_lazy_from_import(self): + pkg2 = testutils.create_package(self.project, "pkg2") + pkg3 = testutils.create_package(self.project, "pkg3", pkg2) + pkg4 = testutils.create_package(self.project, "pkg4", pkg3) + code = dedent("""\ + def import_later(): + from pkg import mod4 + from pkg.mod4 import thing + + mod4 + thing""") + self.mod1.write(code) + self._move(self.mod4, None, pkg4) + self.assertTrue(self.project.find_module("pkg2.pkg3.pkg4.mod4") is not None) + expected = dedent("""\ + def import_later(): + from pkg2.pkg3.pkg4 import mod4 + from pkg2.pkg3.pkg4.mod4 import thing + + mod4 + thing""") + self.assertEqual(expected, self.mod1.read()) + + def test_moving_package_with_from_and_normal_imports(self): pkg2 = testutils.create_package(self.project, "pkg2") code = dedent("""\