Anonymous avatar Anonymous committed b9ec4ac

Fixed moving globals to a module with only docstrings

Comments (0)

Files changed (9)

docs/dev/issues.txt

 * `Memory management`_
 * `Getting ready for Python 3.0`_
 * Should `rope.base` be thread safe? which parts?
+* Better `WordRangeFinder`
 
 
 Enhancing Refactorings

docs/dev/workingon.txt

 Small Stories
 =============
 
+* Moving globals to a module with only docstrings
+* Some of the things in `codeanalyze.WordFinder` can be achieved using
+  asts; make a clone and do them there
 * Assignment to parameters in inline method
 * Assignment to parameter warning
 * Refactoring codeanalyze; lots of estimations

rope/base/codeanalyze.py

     def get_word_at(self, offset):
         offset = self._get_fixed_offset(offset)
         return self.source[self._find_word_start(offset):
-                                self._find_word_end(offset) + 1]
+                           self._find_word_end(offset) + 1]
 
     def _get_fixed_offset(self, offset):
         if offset >= len(self.source):
     def get_primary_at(self, offset):
         offset = self._get_fixed_offset(offset)
         return self.source[self._find_primary_start(offset):
-                                self._find_word_end(offset) + 1].strip()
+                           self._find_word_end(offset) + 1].strip()
 
     def get_splitted_primary_before(self, offset):
         """returns expression, starting, starting_offset
         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)

rope/base/pyobjects.py

     def get_kind(self):
         """Get function type
 
-        It returns on of 'function', 'method', 'staticmethod' or
+        It returns one of 'function', 'method', 'staticmethod' or
         'classmethod' strs.
 
         """

rope/refactor/importutils/module_imports.py

                 break
         else:
             all_imports = self.get_import_statements()
-            last_line = 1
-            blank_lines = 2
-            if all_imports:
-                last_line = all_imports[-1].end_line
-                all_imports[-1].move(last_line)
-                blank_lines = all_imports[-1].blank_lines
+            last_line = self._get_new_import_lineno()
+            blank_lines = self._get_new_import_blanks()
             all_imports.append(importinfo.ImportStatement(
                                import_info, last_line, last_line,
                                blank_lines=blank_lines))
 
+    def _get_new_import_lineno(self):
+        imports = self.get_import_statements()
+        if imports:
+            return imports[-1].end_line
+        return 1
+
+    def _get_new_import_blanks(self):
+        imports = self.get_import_statements()
+        if imports:
+            return imports[-1].blank_lines
+        return 2
+
     def filter_names(self, can_select):
         visitor = actions.RemovingVisitor(
             self.pycore, self._current_folder(), can_select)
         self.separating_lines = 2
 
     def _first_import_line(self):
-        last_index = 1
-        # Getting the line of the first import fails when the first
-        # import is not in the first non doc line of module
         nodes = self.pymodule.get_ast().body
         first_child = 0
         if self.pymodule.get_doc() is not None:
             first_child = 1
         if len(nodes) > first_child:
             last_index = nodes[first_child].lineno
+        else:
+            last_index = self.pymodule.lines.length()
         return last_index
 
     def _compare_imports(self, stmt1, stmt2):

rope/refactor/inline.py

             raise rope.base.exceptions.RefactoringError(
                 'Cannot inline functions with list and keyword arguements.')
         if self.pyfunction.get_kind() == 'classmethod':
-            paramdict[definition_info.args_with_defaults[0][0]] = self.pyfunction.parent.get_name()
+            paramdict[definition_info.args_with_defaults[0][0]] = \
+                self.pyfunction.parent.get_name()
         return paramdict
 
     def get_function_name(self):

rope/refactor/occurrences.py

     def _get_occurrence_pattern(self, name):
         occurrence_pattern = OccurrenceFinder.any('occurrence',
                                                  ['\\b' + name + '\\b'])
-        pattern = re.compile(occurrence_pattern + "|" + self.comment_pattern +
-                             "|" + self.string_pattern)
+        pattern = re.compile(occurrence_pattern + '|' + self.comment_pattern +
+                             '|' + self.string_pattern)
         return pattern
 
     @staticmethod
     def any(name, list_):
-        return "(?P<%s>" % name + "|".join(list_) + ")"
+        return '(?P<%s>' % name + '|'.join(list_) + ')'
 
 
 class Occurrence(object):

rope/ui/uihelpers.py

 class ProgressBar(object):
 
     def __init__(self, parent):
-        self.text = Tkinter.Label(parent, width=80, justify=Tkinter.LEFT)
+        self.text = Tkinter.Label(parent, width=79, justify=Tkinter.LEFT)
         self.canvas = canvas = Tkinter.Canvas(parent, height=20)
         self.color = 'blue'
         self.back_color = canvas['bg']
         self.percent = 0
-        canvas.create_rectangle(0, 0, canvas['width'], canvas['height'], fill='')
+        canvas.create_rectangle(0, 0, canvas['width'],
+                                canvas['height'], fill='')
         canvas.create_rectangle(0, 0, 0, canvas['height'],
                                 fill=self.color, outline=self.color)
         self.text.grid(row=0)

ropetest/refactor/movetest.py

             'class B(object):\n\n    def new_method(self, p):\n        return p\n',
             self.mod2.read())
 
+    def test_moving_globals_to_a_module_with_only_docstrings(self):
+        self.mod1.write('import sys\n\n\ndef f():\n    print(sys.version)\n')
+        self.mod2.write('"""doc\n\nMore docs ...\n\n"""\n')
+        mover = move.create_move(self.project, self.mod1,
+                                 self.mod1.read().index('f()') + 1)
+        self.project.do(mover.get_changes(self.mod2))
+        self.assertEquals(
+            '"""doc\n\nMore docs ...\n\n"""\n'
+            'import sys\n\n\ndef f():\n    print(sys.version)\n',
+            self.mod2.read())
+
 
 if __name__ == '__main__':
     unittest.main()
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.