Skip to content

Commit

Permalink
Merge pull request #330 from rubendibattista/relative-imports
Browse files Browse the repository at this point in the history
Fix relative import offset calculation
  • Loading branch information
mcepl committed Feb 9, 2021
2 parents d24f6bc + 21f39d1 commit 1876b3e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions rope/base/worder.py
Expand Up @@ -225,6 +225,14 @@ def _find_primary_start(self, offset):
prev = self._find_last_non_space_char(offset - 1)
if offset <= 0 or self.code[prev] != '.':
break

# Check if relative import
# XXX: Looks like a hack...
prev_word_end = self._find_last_non_space_char(prev - 1)
if self.code[prev_word_end-3:prev_word_end+1] == "from":
offset = prev
break

offset = self._find_primary_without_dot_start(prev - 1)
if not self._is_id_char(offset):
break
Expand Down
5 changes: 5 additions & 0 deletions ropetest/codeanalyzetest.py
Expand Up @@ -157,6 +157,11 @@ def test_extra_spaces(self):
self.assertEqual('a_func ( "(" ) . an_attr',
self._find_primary(code, 26))

def test_relative_import(self):
code = "from .module import smt"
self.assertEqual('.module',
self._find_primary(code, 5))

def test_functions_on_ending_parens(self):
code = 'A()'
self.assertEqual('A()', self._find_primary(code, 2))
Expand Down

0 comments on commit 1876b3e

Please sign in to comment.