Commits

Ali Gholami Rudi  committed c57df67

Adding imports in restructuring dialog

  • Participants
  • Parent commits 7b1242b

Comments (0)

Files changed (7)

File docs/dev/issues.txt

 * Better import handling
 
 
-Importing Modules In Restructurings
-===================================
-
-Adding a new argument to `Restructuring.get_changes()`::
-
-  imports = ['import mod1', 'from mod1 import AClass']
-  restructuring.get_changes(checks, imports)
-
-Adding a new name to code template::
-
-  replacement = '${?a} = ${^AClass}()'
-
-
 Cache Invalidation And Parameter Cache
 ======================================
 

File docs/dev/workingon.txt

 ==================================
 
 - Adding an ``imports`` argument to `Restructuring.get_changes()`
+- Adding imports in restructuring dialog
 
-* Handling the GUI
+* Helping users make less errors in the GUI
+* Supporting ``$?i``
+* Restructuring should be available even if no python editor is open
+* Better help in restructuring dialog
+
+* Refactor `rope.base.refactor.importutils`
+* Not holding current_folder in `FromImport`?
+* Performing import action on individual imports
+* What to do with files that cannot be compiled when refactoring?
 
 
 Small Stories

File docs/user/overview.txt

 Consider the name of the module containing our `pow` function is
 `mod`.  ``checks`` can be::
 
-  check1: '?pow_func' -> 'mod.pow'
+  check1: '?pow_func' == 'mod.pow'
 
 For performing this refactoring using rope library see `library.txt`_.
 
   pattern: '${?inst}.f(${?p1}, ${?p2})'
   goal: '${?inst}.f1(${?p1}); ${?inst}.f2(${?p2})\n'
   
-  check1: '?inst.type' -> 'mod.A'
+  check1: '?inst.type' == 'mod.A'
 
 After performing we will have::
 
   pattern = '${?x}.set(${?y})'
   goal = '${?x} = ${?y}'
 
-  check: '?x.type' -> 'mod.A'
+  check: '?x.type' == 'mod.A'
 
 The names in checks as you see should be the name of a wild card
 pattern like ``?x`` or ``?y`` in the above example.  They can have a

File rope/refactor/importutils/actions.py

 
     def dispatch(self, import_):
         try:
-            method = getattr(self, 'visit' + import_.import_info.__class__.__name__)
+            method_name = 'visit' + import_.import_info.__class__.__name__
+            method = getattr(self, method_name)
             return method(import_, import_.import_info)
         except exceptions.ModuleNotFoundError:
             pass

File rope/refactor/importutils/importinfo.py

 
 class FromImport(ImportInfo):
 
-    def __init__(self, module_name, level, names_and_aliases, current_folder, pycore):
+    def __init__(self, module_name, level,
+                 names_and_aliases, current_folder, pycore):
         self.module_name = module_name
         self.level = level
         self.names_and_aliases = names_and_aliases
 
     def get_imported_resource(self):
         if self.level == 0:
-            return self.pycore.find_module(self.module_name,
-                                           current_folder=self.current_folder)
+            return self.pycore.find_module(
+                self.module_name, current_folder=self.current_folder)
         else:
             return self.pycore.find_relative_module(
                 self.module_name, self.current_folder, self.level)
         return len(self.names_and_aliases) == 0
 
     def is_star_import(self):
-        return len(self.names_and_aliases) > 0 and self.names_and_aliases[0][0] == '*'
+        return len(self.names_and_aliases) > 0 and \
+               self.names_and_aliases[0][0] == '*'
 
 
 class EmptyImport(ImportInfo):

File rope/refactor/importutils/module_imports.py

     def get_changed_source(self):
         imports = self.get_import_statements()
         after_removing = self._remove_imports(imports)
-        imports = [stmt for stmt in imports if not stmt.import_info.is_empty()]
+        imports = [stmt for stmt in imports
+                   if not stmt.import_info.is_empty()]
 
         first_non_blank = self._first_non_blank_line(after_removing, 0)
         first_import = self._first_import_line() - 1
             result.append('\n' * self.separating_lines)
 
         # Writing the body
-        first_after_imports = self._first_non_blank_line(after_removing, first_import)
+        first_after_imports = self._first_non_blank_line(after_removing,
+                                                         first_import)
         result.extend(after_removing[first_after_imports:])
         return ''.join(result)
 
                 after_removing.append('')
         after_removing.extend(lines[last_index:])
         return after_removing
-    
+
     def _first_non_blank_line(self, lines, lineno):
         result = lineno
         for line in lines[lineno:]:
         imports = self.get_import_statements()
         added_imports = []
         for import_stmt in imports:
-            visitor = actions.AddingVisitor(self.pycore, import_stmt.import_info)
+            visitor = actions.AddingVisitor(self.pycore,
+                                            import_stmt.import_info)
             for added_import in added_imports:
                 if added_import.accept(visitor):
                     import_stmt.empty_import()
             start_line, end_line, self._get_text(start_line, end_line),
             blank_lines=self._count_empty_lines_before(start_line))
         self.imports.append(import_statement)
-    
+
     def _count_empty_lines_before(self, lineno):
         result = 0
         for current in range(lineno - 1, 0, -1):

File rope/ui/refactor.py

         goal = self.goal.get('1.0', 'end-1c')
         restructuring = rope.refactor.restructure.Restructure(
             self.project, pattern, goal)
-        string_checks = dict(self.checks.get_entries())
+        imports = [line for line in self.imports.get('1.0', 'end').splitlines()
+                   if line.strip]
+        string_checks = self._get_checks_dict()
         checks = restructuring.make_checks(string_checks)
-        return restructuring.get_changes(checks, task_handle=handle)
+        return restructuring.get_changes(checks, imports=imports,
+                                         task_handle=handle)
+
+    def _get_checks_dict(self):
+        checks = self.checks.get('1.0', 'end').splitlines()
+        result = {}
+        for check in checks:
+            if '==' in check:
+                splitted = check.split('==')
+                name = splitted[0].strip()
+                value = splitted[1].strip()
+                result[name] = value
+        return result
 
     def _get_dialog_frame(self):
         frame = Tkinter.Frame(self.toplevel)
         self.goal.grid(row=1, column=1, sticky=Tkinter.W)
 
         # Handling checks
-        checks_frame = Tkinter.Frame(frame)
-        list_frame = Tkinter.Frame(checks_frame)
-        self.checks = EnhancedList(list_frame, _CheckListHandle(),
-                                   get_focus=False, width=65, height=5)
-        add_check_button = Tkinter.Button(
-            checks_frame, text='Add Check', command=self._add_check, width=15)
-        remove_check_button = Tkinter.Button(
-            checks_frame, text='Remove Check',
-            command=self._remove_check, width=15)
-        list_frame.grid(row=0, columnspan=2)
-        add_check_button.grid(row=1, column=0)
-        remove_check_button.grid(row=1, column=1)
+        checks_frame = Tkinter.Frame(frame, borderwidth=1,
+                                     relief=Tkinter.RIDGE)
+        checks_help = 'Add checks here; One each line.  For instance:\n' \
+                      '?var.type == mymod.AClass'
+        checks_label = Tkinter.Label(checks_frame, text=checks_help,
+                                     justify=Tkinter.LEFT, width=70)
+        checks_label.grid(row=0)
+        self.checks = Tkinter.Text(checks_frame, height=4, width=70)
+        self.checks.grid(row=1)
         checks_frame.grid(row=2, columnspan=2)
 
-        self.goal.bind('<Alt-a>', self._add_check)
-        self.pattern.bind('<Alt-a>', self._add_check)
+        # Handling Imports
+        imports_frame = Tkinter.Frame(frame, borderwidth=1,
+                                      relief=Tkinter.RIDGE)
+        imports_help = 'Add imports here; These imports are added to ' \
+                       'changed files:'
+        imports_label = Tkinter.Label(imports_frame, text=imports_help,
+                                      justify=Tkinter.LEFT, width=70)
+        imports_label.grid(row=0)
+        self.imports = Tkinter.Text(imports_frame, height=4, width=70)
+        self.imports.grid(row=1)
+        imports_frame.grid(row=3, columnspan=2)
+
         self.pattern.focus_set()
         return frame
 
-    def _add_check(self, event=None):
-        toplevel = Tkinter.Toplevel()
-        toplevel.title('Add Check')
-        name_label = Tkinter.Label(toplevel, text='Name')
-        name_entry = Tkinter.Entry(toplevel, width=40)
-        value_label = Tkinter.Label(toplevel, text='Value')
-        value_entry = Tkinter.Entry(toplevel, width=40)
-        uihelpers.init_completing_entry(value_entry, self._complete_check)
-        name_label.grid(row=0, column=0)
-        name_entry.grid(row=0, column=1)
-        value_label.grid(row=1, column=0)
-        value_entry.grid(row=1, column=1)
-        def ok(event=None):
-            self.checks.add_entry((name_entry.get(), value_entry.get()))
-            toplevel.destroy()
-        def cancel(event=None):
-            toplevel.destroy()
-        toplevel.bind('<Return>', ok)
-        toplevel.bind('<Escape>', cancel)
-        toplevel.bind('<Control-g>', cancel)
-        name_entry.focus_set()
-        toplevel.grab_set()
-
-    def _complete_check(self, text):
-        if '.' not in text:
-            return
-        main, not_finished = text.rsplit('.', 1)
-        result = []
-        evaluated = self._evaluate(main, is_pyname=False)
-        if evaluated is None:
-            return
-        for attribute in evaluated.get_attributes():
-            if attribute.startswith(not_finished):
-                result.append(main + '.' + attribute)
-        return result
-
     def _remove_check(self):
         self.checks.remove_entry()