1. zjes
  2. rope_py3k

Commits

Ali Gholami Rudi  committed df3fd72

Not renaming names in comments and strings in refactorings

  • Participants
  • Parent commits 45a713a
  • Branches trunk

Comments (0)

Files changed (6)

File docs/done.txt

View file
 ===========
 
 
+- Not renaming names in strings and comments in refactorings : July 22, 2006
+
+
 - Separating entering and correcting indentation : July 22, 2006
 
 

File docs/stories.txt

View file
   * Sorting proposals
 
 
-* Not renaming names in strings and comments in refactorings @ 4h
-
-
 Remaining Stories
 -----------------
 * Change program goals and description; principles.txt

File docs/workingon.txt

View file
 * Eliminate FilteredPackage
 * Each PyPackage should have
 
+
   * The list of available children; derived from `__init__.py` or imports/froms
   * An import_name function that throws exception if the name cannot be found
 

File rope/indenter.py

View file
     def correct_indentation(self, lineno):
         pass
         
+    def entering_new_line(self, lineno):
+        pass
+
 
 class PythonCodeIndenter(TextIndenter):
     def __init__(self, editor):

File rope/refactoring.py

View file
 
     def __init__(self, pycore):
         self.pycore = pycore
-    
+        self.comment_pattern = PythonRefactoring.any("comment", [r"#[^\n]*"])
+        sqstring = r"(\b[rR])?'[^'\\\n]*(\\.[^'\\\n]*)*'?"
+        dqstring = r'(\b[rR])?"[^"\\\n]*(\\.[^"\\\n]*)*"?'
+        sq3string = r"(\b[rR])?'''[^'\\]*((\\.|'(?!''))[^'\\]*)*(''')?"
+        dq3string = r'(\b[rR])?"""[^"\\]*((\\.|"(?!""))[^"\\]*)*(""")?'
+        self.string_pattern = PythonRefactoring.any("string",
+                                                    [sq3string, dq3string, sqstring, dqstring])
+
+    @staticmethod
+    def any(name, list):
+        return "(?P<%s>" % name + "|".join(list) + ")"
+
     def rename(self, source_code, offset, new_name):
         result = []
         module_scope = self.pycore.get_string_scope(source_code)
         old_pyname = pyname_finder.get_pyname_at(offset)
         if old_pyname is None:
             return source_code
-        pattern = re.compile('\\b' + old_name + '\\b')
+        occurance_pattern = PythonRefactoring.any('occurance', ['\\b' + old_name + '\\b'])
+        pattern = re.compile(occurance_pattern + "|" + \
+                             self.comment_pattern + "|" + self.string_pattern)
         last_modified_char = 0
         scope_start, scope_end = self._get_scope_range(source_code, offset, module_scope,
                                                        old_pyname.get_definition_location()[1])
         for match in pattern.finditer(source_code[scope_start:scope_end]):
-            match_start = scope_start + match.start()
-            match_end = scope_start + match.end()
-            new_pyname = None
-            try:
-                new_pyname = pyname_finder.get_pyname_at(match_start + 1)
-            except SyntaxError:
-                pass
-            if new_pyname == old_pyname:
-                result.append(source_code[last_modified_char:match_start] + new_name)
-                last_modified_char = match_end
+            for key, value in match.groupdict().items():
+                if value and key == "occurance":
+                    match_start = scope_start + match.start(key)
+                    match_end = scope_start + match.end(key)
+                    new_pyname = None
+                    try:
+                        new_pyname = pyname_finder.get_pyname_at(match_start + 1)
+                    except SyntaxError:
+                        pass
+                    if new_pyname == old_pyname:
+                        result.append(source_code[last_modified_char:match_start] + new_name)
+                        last_modified_char = match_end
         result.append(source_code[last_modified_char:])
         return ''.join(result)
 

File ropetest/refactoringtest.py

View file
         refactored = self.refactoring.rename("replace = True\n'ali'.\\\nreplace\n", 2, 'is_replace')
         self.assertEquals("is_replace = True\n'ali'.\\\nreplace\n", refactored)
 
-    # TODO: Not renaming names in strings
-    def xxx_test_not_renaming_string_contents(self):
+    def test_not_renaming_string_contents(self):
         refactored = self.refactoring.rename("a_var = 20\na_string='a_var'\n", 2, 'new_var')
         self.assertEquals("new_var = 20\na_string='a_var'\n", refactored)
 
-    # TODO: Not renaming names in comments
-    def xxx_test_not_renaming_comment_contents(self):
+    def test_not_renaming_comment_contents(self):
         refactored = self.refactoring.rename("a_var = 20\n# a_var\n", 2, 'new_var')
         self.assertEquals("new_var = 20\n# a_var\n", refactored)