Anonymous avatar Anonymous committed 8756c1d

worder: cleaning up

Comments (0)

Files changed (3)

rope/base/worder.py

     def is_from_aliased(self, offset):
         return self.code_finder.is_from_aliased(offset)
 
-    def find_parens_start_from_inside(self, offset, stop=0):
-        return self.code_finder.find_parens_start_from_inside(offset, stop)
+    def find_parens_start_from_inside(self, offset):
+        return self.code_finder.find_parens_start_from_inside(offset)
 
     def is_a_name_after_from_import(self, offset):
         return self.code_finder.is_a_name_after_from_import(offset)
         return self.code_finder.get_word_parens_range(offset)
 
     def is_name_assigned_in_class_body(self, offset):
-        return self.code_finder._is_name_assigned_in_class_body(offset)
+        return self.code_finder.is_name_assigned_in_class_body(offset)
 
-    def is_on_function_call_keyword(self, offset, stop=0):
-        return self.code_finder.is_on_function_call_keyword(offset, stop)
+    def is_on_function_call_keyword(self, offset):
+        return self.code_finder.is_on_function_call_keyword(offset)
 
     def _find_parens_start(self, offset):
         return self.code_finder._find_parens_start(offset)
         except ValueError:
             return len(self.code)
 
-    def _is_followed_by_equals(self, offset):
-        offset = self._find_first_non_space_char(offset)
-        if offset + 1 < len(self.code) and \
-           self.code[offset] == '=' and self.code[offset + 1] != '=':
-            return True
-        return False
-
-    def _is_name_assigned_in_class_body(self, offset):
+    def is_name_assigned_in_class_body(self, offset):
         word_start = self._find_word_start(offset - 1)
         word_end = self._find_word_end(offset) + 1
         if '.' in self.code[word_start:word_end]:
             return False
         line_start = self._get_line_start(word_start)
         line = self.code[line_start:word_start].strip()
-        if not line and self._is_followed_by_equals(word_end):
-            return True
-        return False
+        return not line and self.get_assignment_type(offset) == '='
 
     def is_a_class_or_function_name_in_header(self, offset):
         word_start = self._find_word_start(offset - 1)
 
     def is_a_name_after_from_import(self, offset):
         try:
-            # XXX: what if the char after from or around import is not
-            # space?
-            last_from = self.code.rindex('from ', 0, offset)
+            line_start = self._get_line_start(offset)
+            last_from = self.code.rindex('from ', line_start, offset)
             from_import = self.code.index(' import ', last_from)
             from_names = from_import + 8
         except ValueError:
         if word_end + 1 == len(self.code):
             return False
         next_char = self._find_first_non_space_char(word_end + 1)
-        if next_char + 2 >= len(self.code) or \
-           self.code[next_char] != '=' or \
-           self.code[next_char + 1] == '=':
+        equals = self.code[next_char:next_char + 2]
+        if equals == '==' or not equals.startswith('='):
             return False
         word_start = self._find_word_start(offset)
         prev_char = self._find_last_non_space_char(word_start - 1)
-        if prev_char - 1 < 0 or self.code[prev_char] not in ',(':
-            return False
-        return True
+        return prev_char - 1 >= 0 and self.code[prev_char] in ',('
 
-    def is_on_function_call_keyword(self, offset, stop_searching=0):
+    def is_on_function_call_keyword(self, offset):
+        stop = self._get_line_start(offset)
         if self._is_id_char(offset):
             offset = self._find_word_start(offset) - 1
         offset = self._find_last_non_space_char(offset)
-        if offset <= stop_searching or \
-           self.code[offset] not in '(,':
+        if offset <= stop or self.code[offset] not in '(,':
             return False
-        parens_start = self.find_parens_start_from_inside(offset, stop_searching)
-        if stop_searching < parens_start:
-            return True
-        return False
+        parens_start = self.find_parens_start_from_inside(offset)
+        return stop < parens_start
 
-    def find_parens_start_from_inside(self, offset, stop_searching=0):
+    def find_parens_start_from_inside(self, offset):
+        stop = self._get_line_start(offset)
         opens = 1
-        while offset > stop_searching:
+        while offset > stop:
             if self.code[offset] == '(':
                 break
             if self.code[offset] != ',':
                 offset = self._find_primary_start(offset)
             offset -= 1
-        return max(stop_searching, offset)
+        return max(stop, offset)
 
     def is_assigned_here(self, offset):
-        operation = self.get_assignment_type(offset)
-        operations = ('=', '-=', '+=', '*=', '/=', '%=', '**=',
-                      '>>=', '<<=', '&=', '^=', '|=')
-        return operation in operations
+        return self.get_assignment_type(offset) is not None
 
     def get_assignment_type(self, offset):
+        # XXX: does not handle tuple assignments
         word_end = self._find_word_end(offset)
         next_char = self._find_first_non_space_char(word_end + 1)
-        current_char = next_char
-        while current_char + 1 < len(self.code) and \
-              (self.code[current_char] != '=' or \
-               self.code[current_char + 1] == '=') and \
-              current_char < next_char + 3:
-            current_char += 1
-        operation = self.code[next_char:current_char + 1]
-        return operation
+        single = self.code[next_char:next_char + 1]
+        double = self.code[next_char:next_char + 2]
+        triple = self.code[next_char:next_char + 3]
+        if double != '==':
+            for op in [single, double, triple]:
+                if op.endswith('='):
+                    return op
 
     def get_primary_range(self, offset):
         start = self._find_primary_start(offset)
             equals_offset = self.code.index('=', start, end)
         except ValueError:
             return False
-        if prev_char != ',' and next_char not in ',)':
+        if prev_char not in '(,' and next_char not in ',)':
             return False
-        parens_start = self.find_parens_start_from_inside(offset, start)
+        parens_start = self.find_parens_start_from_inside(offset)
+        # XXX: only handling (x, y) = value
         return offset < equals_offset and \
-               (parens_start <= 0 or self.code[start:parens_start].strip() == '')
+               self.code[start:parens_start].strip() == ''

rope/contrib/codeassist.py

         word_finder = worder.Worder(self.code, True)
         lines = SourceLinesAdapter(self.code)
         lineno = lines.get_line_number(offset)
-        stop_line = LogicalLineFinder(lines).logical_line_in(lineno)[0]
-        stop = lines.get_line_start(stop_line)
-        if word_finder.is_on_function_call_keyword(offset - 1, stop):
+        if word_finder.is_on_function_call_keyword(offset - 1):
             name_finder = rope.base.evaluate.ScopeNameFinder(pymodule)
             function_parens = word_finder.\
-                find_parens_start_from_inside(offset - 1, stop)
+                find_parens_start_from_inside(offset - 1)
             primary = word_finder.get_primary_at(function_parens - 1)
             try:
                 function_pyname = rope.base.evaluate.\

ropetest/codeanalyzetest.py

         self.assertEquals(code.index('('),
                           finder.find_parens_start_from_inside(len(code) - 1))
 
+    def test_is_on_function_keyword(self):
+        code = 'myfunc(va'
+        finder = worder.Worder(code)
+        self.assertTrue(finder.is_on_function_call_keyword(len(code) - 1))
+
 
 class ScopeNameFinderTest(unittest.TestCase):
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.