Commits

Anonymous committed 39c7dd3

Handling augmented assignments

  • Participants
  • Parent commits 8bb9881

Comments (0)

Files changed (7)

File docs/issues.txt

 ===============
 
 * Showing available refactorings for a place
+* Designing refactorings to be used on IDEs
 * Rope's preference saving system: format, location
 
 * Indexing source files for faster occurrence finding

File docs/workingon.txt

-Change Local Variable to Field Refactoring
-==========================================
-
-- Check for being a local variable in side a method
-- Check that there is no other attribute with the same name
-- Other names than `self` first parameter
-- Adding `localtofield` module
-- Adding to ui
-
-* Refactoring common testcase setUps and tearDowns into a decorators;
-  We'll miss code assists in tests.
-
-
 Encapsulate Field
 =================
 
+- `AugAssign` nodes
+
 * Tuple assignments
-* `AugAssign` nodes
 
 * Handling `AssList` for inline variable and encapsulate field
 * Changing refactor modules to use `sourcetools.add_methods`

File rope/codeanalyze.py

         return current_offset
     
     def is_assigned_here(self, offset):
+        operation = self.get_assignment_type(offset)
+        operations = ('=', '-=', '+=', '*=', '/=', '%=', '**=',
+                      '>>=', '<<=', '&=', '^=', '|=')
+        return operation in operations
+
+    def get_assignment_type(self, offset):
         word_end = self._find_word_end(offset)
         next_char = self._find_first_non_space_char(word_end + 1)
-        if next_char < len(self.source_code) and self.source_code[next_char] == '=' and \
-           len(self.source_code) > next_char + 1 and \
-           self.source_code[next_char + 1] != '=':
-            return True
-        return False
+        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 \
+              current_char < next_char + 3:
+            current_char += 1
+        operation = self.source_code[next_char:current_char + 1]
+        return operation
 
 
 class StatementEvaluator(object):

File rope/refactor/encapsulate_field.py

             self._manage_writes(start, result)
             result.append(self.source[self.last_modified:start])
             if occurrence.is_written():
-                result.append(self.setter + '(')
+                assignment_type = occurrence.get_assignment_type()
+                if assignment_type == '=':
+                    result.append(self.setter + '(')
+                else:
+                    var_name = self.source[occurrence.get_primary_range()[0]:
+                                           start] + self.getter + '()'
+                    result.append(self.setter + '(' + var_name + ' %s ' % assignment_type[:-1])
                 if line_finder is None:
                     line_finder = rope.codeanalyze.LogicalLineFinder(self.lines)
                 current_line = self.lines.get_line_number(start)

File rope/refactor/occurrences.py

     
     def is_written(self):
         return self.tools.word_finder.is_assigned_here(self.offset)
+    
+    def get_assignment_type(self):
+        if not self.is_written():
+            return None
+        return self.tools.word_finder.get_assignment_type(self.offset)
 
-    def is_read(self):
-        # TODO: Implement it
-        raise NotImplementedError()
-    
 
 class FilteredOccurrenceFinder(object):
     

File rope/ui/refactor.py

 
 actions = []
 actions.append(SimpleAction('Rename Refactoring', ConfirmAllEditorsAreSaved(rename), 'M-R',
-                            MenuAddress(['Refactor', 'Rename'], 'r'), ['python']))
+                            MenuAddress(['Refactor', 'Rename'], 'n'), ['python']))
 actions.append(SimpleAction('Extract Method', ConfirmAllEditorsAreSaved(extract_method), 'M-M',
-                            MenuAddress(['Refactor', 'Extract Method'], 'e'), ['python']))
+                            MenuAddress(['Refactor', 'Extract Method'], 'x'), ['python']))
 actions.append(SimpleAction('Move Refactoring', ConfirmAllEditorsAreSaved(move), 'M-V',
-                            MenuAddress(['Refactor', 'Move'], 'v'), ['python']))
+                            MenuAddress(['Refactor', 'Move'], 'm'), ['python']))
 actions.append(SimpleAction('Inline Local Variable', ConfirmAllEditorsAreSaved(inline), 'M-I',
                             MenuAddress(['Refactor', 'Inline Local Variable'], 'i'), ['python']))
 actions.append(SimpleAction('Extract Local Variable', ConfirmAllEditorsAreSaved(extract_variable), None,
                             MenuAddress(['Refactor', 'Extract Local Variable'], 'l'), ['python']))
 actions.append(SimpleAction('Rename in File', ConfirmAllEditorsAreSaved(local_rename), None,
-                            MenuAddress(['Refactor', 'Rename in File'], 'f'), ['python']))
+                            MenuAddress(['Refactor', 'Rename in File'], 'e'), ['python']))
 actions.append(SimpleAction('Introduce Factory Method', 
                             ConfirmAllEditorsAreSaved(introduce_factory), None,
-                            MenuAddress(['Refactor', 'Introduce Factory Method'], 'c', 1),
+                            MenuAddress(['Refactor', 'Introduce Factory Method'], 'f', 1),
                             ['python']))
 actions.append(SimpleAction('Encapsulate Field', 
                             ConfirmAllEditorsAreSaved(encapsulate_field), None,
-                            MenuAddress(['Refactor', 'Encapsulate Field'], 'n', 1),
+                            MenuAddress(['Refactor', 'Encapsulate Field'], 's', 1),
                             ['python']))
 actions.append(SimpleAction('Convert Local Variable to Field', 
                             ConfirmAllEditorsAreSaved(convert_local_to_field), None,
-                            MenuAddress(['Refactor', 'Convert Local Variable to Field'], None, 1),
+                            MenuAddress(['Refactor', 'Convert Local Variable to Field'], 'b', 1),
                             ['python']))
 actions.append(SimpleAction('Transform Module to Package', 
                             ConfirmAllEditorsAreSaved(transform_module_to_package), None,
                             MenuAddress(['Refactor', 'Organize Imports'], 'o', 2), ['python']))
 actions.append(SimpleAction('Expand Star Imports', 
                             ConfirmAllEditorsAreSaved(expand_star_imports), None,
-                            MenuAddress(['Refactor', 'Expand Star Imports'], 'x', 2),
+                            MenuAddress(['Refactor', 'Expand Star Imports'], 'p', 2),
                             ['python']))
 actions.append(SimpleAction('Transform Relatives to Absolute', 
                             ConfirmAllEditorsAreSaved(transform_relatives_to_absolute), None,
                             ['python']))
 actions.append(SimpleAction('Transform Froms to Imports', 
                             ConfirmAllEditorsAreSaved(transform_froms_to_imports), None,
-                            MenuAddress(['Refactor', 'Transform Froms to Imports'], 's', 2),
+                            MenuAddress(['Refactor', 'Transform Froms to Imports'], 'r', 2),
                             ['python']))
 actions.append(SimpleAction('Undo Refactoring', 
                             ConfirmAllEditorsAreSaved(undo_refactoring), None,

File ropetest/refactor/__init__.py

         self.mod1.write('attr = 10')
         self.refactoring.encapsulate_field(self.mod1, self.mod1.read().index('attr') + 1)
 
+    def test_changing_augmented_assignments(self):
+        self.mod1.write('import mod\na_var = mod.A()\na_var.attr += 1\n')
+        self.mod.write(self.a_class)
+        self.refactoring.encapsulate_field(self.mod, self.mod.read().index('attr') + 1)
+        self.assertEquals(
+            'import mod\na_var = mod.A()\na_var.set_attr(a_var.get_attr() + 1)\n',
+            self.mod1.read())
+    
+    def test_changing_augmented_assignments2(self):
+        self.mod1.write('import mod\na_var = mod.A()\na_var.attr <<= 1\n')
+        self.mod.write(self.a_class)
+        self.refactoring.encapsulate_field(self.mod, self.mod.read().index('attr') + 1)
+        self.assertEquals(
+            'import mod\na_var = mod.A()\na_var.set_attr(a_var.get_attr() << 1)\n',
+            self.mod1.read())
+    
 
 class LocalToFieldTest(unittest.TestCase):