Anonymous avatar Anonymous committed b331f2e

Fixing offset passed to WordFinder in generate

Comments (0)

Files changed (5)

docs/dev/workingon.txt

 Small Stories
 =============
 
-- Inlining classmethods
-
-* Generate method and functions
 * Assignment to parameters in inline method
+* Assignment to parameter warning
+* Refactoring codeanalyze; lots of estimations
 
 * What to do with files that cannot be compiled when refactoring?
 * Extracting subexpressions; look at `extracttest` for more info

rope/base/codeanalyze.py

 class WordRangeFinder(object):
 
     def __init__(self, source_code):
-        self.source_code = source_code
+        self.source = source_code
 
     def _find_word_start(self, offset):
         current_offset = offset
 
     def _find_word_end(self, offset):
         current_offset = offset + 1
-        while current_offset < len(self.source_code) and \
+        while current_offset < len(self.source) and \
               self._is_id_char(current_offset):
             current_offset += 1;
         return current_offset - 1
         if offset <= 0:
             return 0
         current_offset = offset
-        while current_offset >= 0 and self.source_code[current_offset] in ' \t\n':
-            while current_offset >= 0 and self.source_code[current_offset] in ' \t':
+        while current_offset >= 0 and self.source[current_offset] in ' \t\n':
+            while current_offset >= 0 and self.source[current_offset] in ' \t':
                 current_offset -= 1
-            if current_offset >= 0 and self.source_code[current_offset] == '\n':
+            if current_offset >= 0 and self.source[current_offset] == '\n':
                 current_offset -= 1
-                if current_offset >= 0 and self.source_code[current_offset] == '\\':
+                if current_offset >= 0 and self.source[current_offset] == '\\':
                     current_offset -= 1
         return current_offset
 
     def get_word_before(self, offset):
-        return self.source_code[self._find_word_start(offset - 1):offset]
+        return self.source[self._find_word_start(offset - 1):offset]
 
 
     def get_word_at(self, offset):
         offset = self._get_fixed_offset(offset)
-        return self.source_code[self._find_word_start(offset):
+        return self.source[self._find_word_start(offset):
                                 self._find_word_end(offset) + 1]
 
     def _get_fixed_offset(self, offset):
-        if offset >= len(self.source_code):
+        if offset >= len(self.source):
             return offset - 1
         if not self._is_id_char(offset):
             if offset > 0 and self._is_id_char(offset - 1):
                 return offset - 1
-            if offset < len(self.source_code) - 1 and self._is_id_char(offset + 1):
+            if offset < len(self.source) - 1 and self._is_id_char(offset + 1):
                 return offset + 1
         return offset
 
     def _is_id_char(self, offset):
-        return self.source_code[offset].isalnum() or self.source_code[offset] == '_'
+        return self.source[offset].isalnum() or self.source[offset] == '_'
 
     def _find_string_start(self, offset):
-        kind = self.source_code[offset]
+        kind = self.source[offset]
         current_offset = offset - 1
-        while self.source_code[current_offset] != kind:
+        while self.source[current_offset] != kind:
             current_offset -= 1
         return current_offset
 
     def _find_parens_start(self, offset):
         current_offset = self._find_last_non_space_char(offset - 1)
-        while current_offset >= 0 and self.source_code[current_offset] not in '[({':
-            if self.source_code[current_offset] in ':,':
+        while current_offset >= 0 and self.source[current_offset] not in '[({':
+            if self.source[current_offset] in ':,':
                 pass
             else:
                 current_offset = self._find_primary_start(current_offset)
 
     def _find_atom_start(self, offset):
         old_offset = offset
-        if self.source_code[offset] in '\n\t ':
+        if self.source[offset] in '\n\t ':
             offset = self._find_last_non_space_char(offset)
-        if self.source_code[offset] in '\'"':
+        if self.source[offset] in '\'"':
             return self._find_string_start(offset)
-        if self.source_code[offset] in ')]}':
+        if self.source[offset] in ')]}':
             return self._find_parens_start(offset)
         if self._is_id_char(offset):
             return self._find_word_start(offset)
     def _find_primary_without_dot_start(self, offset):
         last_parens = offset
         current_offset = self._find_last_non_space_char(offset)
-        while current_offset > 0 and self.source_code[current_offset] in ')]}':
+        while current_offset > 0 and self.source[current_offset] in ')]}':
             last_parens = self._find_parens_start(current_offset)
             current_offset = self._find_last_non_space_char(last_parens - 1)
-        if self.source_code[last_parens] == '(' and self._is_id_char(current_offset):
+        if self.source[last_parens] == '(' and self._is_id_char(current_offset):
             return self._find_primary_without_dot_start(current_offset)
 
 
-        if current_offset > 0 and self.source_code[current_offset] in '\'"':
+        if current_offset > 0 and self.source[current_offset] in '\'"':
             return self._find_string_start(current_offset)
         elif current_offset > 0 and self._is_id_char(current_offset):
             return self._find_word_start(current_offset)
         return last_parens
 
     def _find_primary_start(self, offset):
-        if offset >= len(self.source_code):
-            offset = len(self.source_code) - 1
+        if offset >= len(self.source):
+            offset = len(self.source) - 1
         current_offset = offset + 1
-        if self.source_code[offset] != '.':
+        if self.source[offset] != '.':
             current_offset = self._find_primary_without_dot_start(offset)
         while current_offset > 0 and \
-              self.source_code[self._find_last_non_space_char(current_offset - 1)] == '.':
+              self.source[self._find_last_non_space_char(current_offset - 1)] == '.':
             dot_position = self._find_last_non_space_char(current_offset - 1)
             current_offset = self._find_primary_without_dot_start(dot_position - 1)
 
 
     def get_primary_at(self, offset):
         offset = self._get_fixed_offset(offset)
-        return self.source_code[self._find_primary_start(offset):
+        return self.source[self._find_primary_start(offset):
                                 self._find_word_end(offset) + 1].strip()
 
     def get_splitted_primary_before(self, offset):
         end = offset - 1
         word_start = self._find_atom_start(end)
         real_start = self._find_primary_start(end)
-        if self.source_code[word_start:offset].strip() == '':
+        if self.source[word_start:offset].strip() == '':
             word_start = end
-        if self.source_code[real_start:word_start].strip() == '':
+        if self.source[real_start:word_start].strip() == '':
             real_start = word_start
         if real_start == word_start == end and not self._is_id_char(end):
             return ('', '', offset)
         if real_start == word_start:
-            return ('', self.source_code[word_start:offset], word_start)
+            return ('', self.source[word_start:offset], word_start)
         else:
-            if self.source_code[end] == '.':
-                return (self.source_code[real_start:end], '', offset)
+            if self.source[end] == '.':
+                return (self.source[real_start:end], '', offset)
             last_dot_position = word_start
-            if self.source_code[word_start] != '.':
+            if self.source[word_start] != '.':
                 last_dot_position = self._find_last_non_space_char(word_start - 1)
             last_char_position = self._find_last_non_space_char(last_dot_position - 1)
-            return (self.source_code[real_start:last_char_position + 1],
-                    self.source_code[word_start:offset], word_start)
+            return (self.source[real_start:last_char_position + 1],
+                    self.source[word_start:offset], word_start)
 
     def _get_line_start(self, offset):
-        while offset > 0 and self.source_code[offset] != '\n':
+        while offset > 0 and self.source[offset] != '\n':
             offset -= 1
         return offset
 
     def _get_line_end(self, offset):
-        while offset < len(self.source_code) and self.source_code[offset] != '\n':
+        while offset < len(self.source) and self.source[offset] != '\n':
             offset += 1
         return offset
 
     def _is_followed_by_equals(self, offset):
-        while offset < len(self.source_code) and self.source_code[offset] in ' \\':
-            if self.source_code[offset] == '\\':
+        while offset < len(self.source) and self.source[offset] in ' \\':
+            if self.source[offset] == '\\':
                 offset = self._get_line_end(offset)
             offset += 1
-        if offset + 1 < len(self.source_code) and \
-           self.source_code[offset] == '=' and self.source_code[offset + 1] != '=':
+        if offset + 1 < len(self.source) and \
+           self.source[offset] == '=' and self.source[offset + 1] != '=':
             return True
         return False
 
     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) + 1
-        if '.' in self.source_code[word_start:word_end]:
+        if '.' in self.source[word_start:word_end]:
             return False
         line_start = self._get_line_start(word_start)
-        line = self.source_code[line_start:word_start].strip()
+        line = self.source[line_start:word_start].strip()
         if line == '' and self._is_followed_by_equals(word_end):
             return True
         return False
     def is_a_class_or_function_name_in_header(self, offset):
         word_start = self._find_word_start(offset - 1)
         line_start = self._get_line_start(word_start)
-        prev_word = self.source_code[line_start:word_start].strip()
+        prev_word = self.source[line_start:word_start].strip()
         return prev_word in ['def', 'class']
 
     def _find_first_non_space_char(self, offset):
-        if offset >= len(self.source_code):
-            return len(self.source_code)
+        if offset >= len(self.source):
+            return len(self.source)
         current_offset = offset
-        while current_offset < len(self.source_code) and\
-              self.source_code[current_offset] in ' \t\n':
-            while current_offset < len(self.source_code) and \
-                  self.source_code[current_offset] in ' \t\n':
+        while current_offset < len(self.source) and\
+              self.source[current_offset] in ' \t\n':
+            while current_offset < len(self.source) and \
+                  self.source[current_offset] in ' \t\n':
                 current_offset += 1
-            if current_offset + 1 < len(self.source_code) and \
-               self.source_code[current_offset] == '\\':
+            if current_offset + 1 < len(self.source) and \
+               self.source[current_offset] == '\\':
                 current_offset += 2
         return current_offset
 
     def is_a_function_being_called(self, offset):
         word_end = self._find_word_end(offset - 1) + 1
         next_char = self._find_first_non_space_char(word_end)
-        return next_char < len(self.source_code) and \
-               self.source_code[next_char] == '(' and \
+        return next_char < len(self.source) and \
+               self.source[next_char] == '(' and \
                not self.is_a_class_or_function_name_in_header(offset)
                
 
     def _find_import_pair_end(self, start):
         next_char = self._find_first_non_space_char(start)
-        if self.source_code[next_char] == '(':
+        if self.source[next_char] == '(':
             try:
-                return self.source_code.index(')', next_char) + 1
+                return self.source.index(')', next_char) + 1
             except ValueError:
                 return SyntaxError('Unmatched Parens')
         else:
             current_offset = next_char
-            while current_offset < len(self.source_code):
-                if self.source_code[current_offset] == '\n':
+            while current_offset < len(self.source):
+                if self.source[current_offset] == '\n':
                     break
-                if self.source_code[current_offset] == '\\':
+                if self.source[current_offset] == '\\':
                     current_offset += 1
                 current_offset += 1
             return current_offset
 
     def is_import_statement(self, offset):
         try:
-            last_import = self.source_code.rindex('import ', 0, offset)
+            last_import = self.source.rindex('import ', 0, offset)
         except ValueError:
             return False
         return self._find_import_pair_end(last_import + 7) >= offset
 
     def is_from_statement(self, offset):
         try:
-            last_from = self.source_code.rindex('from ', 0, offset)
-            from_import = self.source_code.index(' import ', last_from)
+            last_from = self.source.rindex('from ', 0, offset)
+            from_import = self.source.index(' import ', last_from)
             from_names = from_import + 8
         except ValueError:
             return False
         return self._find_import_pair_end(from_names) >= offset
 
     def is_from_statement_module(self, offset):
-        if offset >= len(self.source_code) - 1:
+        if offset >= len(self.source) - 1:
             return False
         stmt_start = self._find_primary_start(offset)
         line_start = self._get_line_start(stmt_start)
-        prev_word = self.source_code[line_start:stmt_start].strip()
+        prev_word = self.source[line_start:stmt_start].strip()
         return prev_word == 'from'
 
     def is_a_name_after_from_import(self, offset):
         try:
-            last_from = self.source_code.rindex('from ', 0, offset)
-            from_import = self.source_code.index(' import ', last_from)
+            last_from = self.source.rindex('from ', 0, offset)
+            from_import = self.source.index(' import ', last_from)
             from_names = from_import + 8
         except ValueError:
             return False
 
     def is_function_keyword_parameter(self, offset):
         word_end = self._find_word_end(offset)
-        if word_end + 1 == len(self.source_code):
+        if word_end + 1 == len(self.source):
             return False
         next_char = self._find_first_non_space_char(word_end + 1)
-        if next_char + 2 >= len(self.source_code) or \
-           self.source_code[next_char] != '=' or \
-           self.source_code[next_char + 1] == '=':
+        if next_char + 2 >= len(self.source) or \
+           self.source[next_char] != '=' or \
+           self.source[next_char + 1] == '=':
             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.source_code[prev_char] not in ',(':
+        if prev_char - 1 < 0 or self.source[prev_char] not in ',(':
             return False
         return True
 
             current_offset = self._find_word_start(current_offset) - 1
         current_offset = self._find_last_non_space_char(current_offset)
         if current_offset <= stop_searching or \
-           self.source_code[current_offset] not in '(,':
+           self.source[current_offset] not in '(,':
             return False
         parens_start = self.find_parens_start_from_inside(offset, stop_searching)
         if stop_searching < parens_start:
         current_offset = offset
         opens = 1
         while current_offset > stop_searching:
-            if self.source_code[current_offset] == '(':
+            if self.source[current_offset] == '(':
                 opens -= 1
             if opens == 0:
                 break
-            if self.source_code[current_offset] == ')':
+            if self.source[current_offset] == ')':
                 opens += 1
             current_offset -= 1
         return current_offset
         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.source_code) and \
-              (self.source_code[current_char] != '=' or \
-               self.source_code[current_char + 1] == '=') and \
+        while current_char + 1 < len(self.source) and \
+              (self.source[current_char] != '=' or \
+               self.source[current_char + 1] == '=') and \
               current_char < next_char + 3:
             current_char += 1
-        operation = self.source_code[next_char:current_char + 1]
+        operation = self.source[next_char:current_char + 1]
         return operation
 
     def get_primary_range(self, offset):
         if self.is_a_function_being_called(offset) or \
            self.is_a_class_or_function_name_in_header(offset):
             end = self._find_word_end(offset)
-            start_parens = self.source_code.index('(', end)
+            start_parens = self.source.index('(', end)
             index = start_parens
             open_count = 0
-            while index < len(self.source_code):
-                if self.source_code[index] == '(':
+            while index < len(self.source):
+                if self.source[index] == '(':
                     open_count += 1
-                if self.source_code[index] == ')':
+                if self.source[index] == ')':
                     open_count -= 1
                 if open_count == 0:
                     return (start_parens, index + 1)

rope/ide/generate.py

 def create_generate(kind, project, resource, offset):
     """A factory for creating `Generate` objects
 
-    `kind` can be ``variable``, ``function``, ``class``, ``module``
-    or ``package``.
+    `kind` can be 'variable', 'function', 'class', 'module' or
+    'package'.
 
     """
     generate = eval('Generate' + kind.title())
         finder = codeanalyze.WordRangeFinder(source)
         if finder.is_a_function_being_called(self.offset):
             start, end = finder.get_primary_range(self.offset)
-            parens_start, parens_end = finder.get_word_parens_range(self.offset)
+            parens_start, parens_end = finder.get_word_parens_range(end - 1)
             call = source[start:parens_end]
             parser = functionutils._FunctionParser(call, False)
             args, keywords = parser.get_parameters()

rope/refactor/inline.py

         call_info = rope.refactor.functionutils.CallInfo.read(
             primary, pyname, self.definition_info, call)
         paramdict = self.definition_params
-        mapping = rope.refactor.functionutils.ArgumentMapping(self.definition_info,
-                                                              call_info)
+        mapping = rope.refactor.functionutils.ArgumentMapping(
+            self.definition_info, call_info)
         for param_name, value in mapping.param_dict.items():
             paramdict[param_name] = value
         header = ''

ropetest/codeanalyzetest.py

         word_finder = WordRangeFinder(code)
         self.assertFalse(word_finder.is_import_statement(code.rindex('d') + 1))
 
+    def test_word_parens_range(self):
+        code = 's = str()\ns.title()\n'
+        word_finder = WordRangeFinder(code)
+        result = word_finder.get_word_parens_range(code.rindex('()') - 1)
+        self.assertEquals((len(code) - 3, len(code) - 1), result)
+
 
 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.