1. Leandro Lameiro
  2. gedit-python-refactoring

Commits

lameiro  committed 4c3ef00

Initial import.

  • Participants
  • Branches default

Comments (0)

Files changed (1)

File rope_gedit.py

View file
+import gedit
+import gtk
+import ropemode.environment
+import ropemode.interface
+import ropemode.refactor
+
+
+class Progress(object):
+    def __init__(self, message):
+        self.message = message
+        print 'Progress <%s> created!' % self.message
+        
+    def done(self):
+        print 'Progress <%s> done!' % self.message
+        
+    def update(self, percent):
+        print 'Progress <%s> at <%d>' % (self.message, percent)
+
+class GEditEnv(ropemode.environment.Environment):
+    def __init__(self, gedit_controller):
+        self.gedit_controller = gedit_controller
+        self.guess_project = True
+        self.enable_autoimport = True
+        self.autoimport_underlineds = False
+    
+    def filename(self):
+        return self.gedit_controller.filename()
+        
+    def message(self, msg):
+        print ("ROPE MSG: %s" % msg)
+        
+    def create_progress(self, message):
+        return Progress(message)
+        
+    def ask(self, *args, **kwargs):
+        print args
+        print kwargs
+        return self.gedit_controller.ask(kwargs)
+        
+        
+    def ask_values(self, *args, **kwargs):        
+        print args
+        print kwargs
+        #TODO: really ask!
+        return 'perform'
+        
+    def ask_directory(self, *args, **kwargs):
+        print args
+        print kwargs
+        baz
+        
+    def get(self, name, default=None):
+        print 'Getting property name', name
+        return getattr(self, name, default)
+        
+    def get_region(self):
+        return self.gedit_controller.get_region()
+        
+    def ask_completion(self, prompt, names, starting):
+        print prompt
+        print name
+        print starting
+        
+
+class GEditWindowController(object):
+    def __init__(self, window):
+        self.window = window
+    
+    def filename(self):
+        active_document = self.window.get_active_document()
+        if active_document:
+            return active_document.get_uri()[7:]
+            
+    def ask(self, options):
+        def responseToDialog(entry, dialog, response):  
+            dialog.response(response)  
+        
+        #base this on a message dialog  
+        dialog = gtk.MessageDialog(  
+                                    None,  
+                                    gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,  
+                                    gtk.MESSAGE_QUESTION,  
+                                    gtk.BUTTONS_OK,  
+                                    None
+                                   )  
+        dialog.set_markup(options['prompt'])
+        #create the text input field  
+        entry = gtk.Entry()
+        if options['default'] != None:
+            entry.props.text = options['default']
+        #allow the user to press enter to do ok  
+        entry.connect("activate", responseToDialog, dialog, gtk.RESPONSE_OK)  
+        #create a horizontal box to pack the entry and a label  
+        hbox = gtk.HBox()  
+        #hbox.pack_start(gtk.Label("Name:"), False, 5, 5)  
+        hbox.pack_end(entry)  
+        #some secondary text  
+        #dialog.format_secondary_markup("This will be used for <i>identification</i> purposes")  
+        #add it and show it  
+        dialog.vbox.pack_end(hbox, True, True, 0)  
+        dialog.show_all()  
+        #go go go  
+        dialog.run()  
+        text = entry.get_text()  
+        dialog.destroy()  
+     
+        return text
+
+    def get_region(self):
+        active_document = self.window.get_active_document()
+        start_iter, end_iter = active_document.get_selection_bounds()
+        return start_iter.get_offset(), end_iter.get_offset()
+
+
+class RefactorWindowHelper(object):
+    def __init__(self, plugin, window):
+        self._window = window
+        self._env = GEditEnv(GEditWindowController(window))
+        self._interface = ropemode.interface.RopeMode(env=self._env)
+        self._interface.init()
+        self._insert_menu()
+
+    def deactivate(self):
+        self._remove_menu()
+        self._action_group = None
+        self._window = None
+        self._plugin = None
+
+    def _insert_menu(self):
+        manager = self._window.get_ui_manager()
+        self._action_group = gtk.ActionGroup("RefactorActions")
+        
+        ui_str = """
+        <ui>
+          <menubar name="MenuBar">
+            <menu name="EditMenu" action="Edit">
+              <placeholder name="EditOps_4">
+                    <menuitem name="Extract Variable" action="ExtractVariable"/>
+                    <menuitem name="Extract Variable" action="ExtractMethod"/>
+                    <menuitem name="Organize Imports" action="OrganizeImports"/>
+              </placeholder>
+            </menu>
+          </menubar>
+        </ui>
+        """
+
+        
+        # Rename these local variables to avoid confusion
+        extract_variable_action = ("ExtractVariable", None, "Extract Variable", "<control><alt>V", "Extract selected statement to a variable", self.extract_variable_action)
+        extract_method_action   = ("ExtractMethod", None, "Extract Method",     "<control><alt>M", "Extract selected statement to a method", self.extract_method_action)
+        organize_imports_action = ("OrganizeImports", None, "Organize Imports", "<control><alt>O", "Organizes imports", self.organize_imports_action)
+        
+        self._action_group.add_actions([extract_variable_action, extract_method_action, organize_imports_action], self._window)
+
+        manager.insert_action_group(self._action_group, -1)
+        self._ui_id = manager.add_ui_from_string(ui_str)
+
+    def _remove_menu(self):
+        manager = self._window.get_ui_manager()
+        manager.remove_ui(self._ui_id)
+        manager.remove_action_group(self._action_group)
+        manager.ensure_update()
+
+    def update_ui(self):
+        return 0
+        doc = self._window.get_active_document()
+        if doc:
+            lang = doc.get_language()
+            if lang is not None:
+                sensitive = get_comment_tags(lang) != (None, None)
+                self._action_group.set_sensitive(sensitive)
+                return
+        self._action_group.set_sensitive(False)
+        
+        
+    def reload_document(self):
+        active_document = self._window.get_active_document()
+        active_line = active_document.get_iter_at_mark(active_document.get_insert()).get_line() + 1
+        print 'active line =', active_line
+        active_document.load(active_document.get_uri(), active_document.get_encoding(), active_line, False)
+        
+    def extract_variable_action(self, action, window):
+        print ('Called extract_variable_action')
+        extractor = ropemode.refactor.ExtractVariable(self._interface, self._env)
+        extractor.show()
+        self.reload_document()
+    
+    def extract_method_action(self, action, window):
+        print ('Called extract_method_action')
+        extractor = ropemode.refactor.ExtractMethod(self._interface, self._env)
+        extractor.show()
+        self.reload_document()
+
+        
+    def organize_imports_action(self, action, window):
+        print ('Called organize_imports_action')
+        organizer = ropemode.refactor.OrganizeImports(self._interface, self._env)
+        organizer.show()
+        self.reload_document()
+        
+    
+
+class RefactorPlugin(gedit.Plugin):
+    def __init__(self):
+        gedit.Plugin.__init__(self)
+        self.windows = {}
+
+    def activate(self, window):
+        #print ('Instantiating new refactor window helper')
+        self.windows[window] = RefactorWindowHelper(self, window)
+
+    def deactivate(self, window):
+        rope_window_helper = self.windows[window]
+        rope_window_helper.deactivate()
+        del self.windows[window]
+        
+    def update_ui(self, window):
+        self.windows[window].update_ui()
+        
+