RefactoringRope/Refactoring.py

changeset 5
49a4abfc1f89
parent 4
2e2463ef1aae
child 6
07dabc6bb157
equal deleted inserted replaced
4:2e2463ef1aae 5:49a4abfc1f89
15 import rope.base.libutils 15 import rope.base.libutils
16 import rope.base.project 16 import rope.base.project
17 import rope.base.exceptions 17 import rope.base.exceptions
18 18
19 import rope.refactor.rename 19 import rope.refactor.rename
20 ##import rope.refactor.extract 20 import rope.refactor.extract
21 ##import rope.refactor.usefunction 21 ##import rope.refactor.usefunction
22 ##import rope.refactor.inline 22 ##import rope.refactor.inline
23 ##import rope.refactor.move 23 ##import rope.refactor.move
24 ##import rope.refactor.change_signature 24 ##import rope.refactor.change_signature
25 ##import rope.refactor.introduce_factory 25 ##import rope.refactor.introduce_factory
48 from HelpDialog import HelpDialog 48 from HelpDialog import HelpDialog
49 from MatchesDialog import MatchesDialog 49 from MatchesDialog import MatchesDialog
50 from RenameDialog import RenameDialog 50 from RenameDialog import RenameDialog
51 from ChangeOccurrencesDialog import ChangeOccurrencesDialog 51 from ChangeOccurrencesDialog import ChangeOccurrencesDialog
52 from HistoryDialog import HistoryDialog 52 from HistoryDialog import HistoryDialog
53 from ExtractDialog import ExtractDialog
53 54
54 import Utilities 55 import Utilities
55 56
56 57
57 class Refactoring(QObject): 58 class Refactoring(QObject):
167 self.connect(self.refactoringChangeOccurrencesAct, 168 self.connect(self.refactoringChangeOccurrencesAct,
168 SIGNAL('triggered()'), self.__changeOccurrences) 169 SIGNAL('triggered()'), self.__changeOccurrences)
169 self.actions.append(self.refactoringChangeOccurrencesAct) 170 self.actions.append(self.refactoringChangeOccurrencesAct)
170 171
171 ##################################################### 172 #####################################################
173 ## Extract refactoring actions
174 #####################################################
175
176 self.refactoringExtractMethodAct = E5Action(
177 self.trUtf8('Extract method'),
178 self.trUtf8('Extract &Method'),
179 0, 0,
180 self,'refactoring_extract_method')
181 self.refactoringExtractMethodAct.setStatusTip(self.trUtf8(
182 'Extract the highlighted area as a method'))
183 self.refactoringExtractMethodAct.setWhatsThis(self.trUtf8(
184 """<b>Extract method</b>"""
185 """<p>Extract the highlighted area as a method or function.</p>"""
186 ))
187 if self.__newStyle:
188 self.refactoringExtractMethodAct.triggered[()].connect(
189 self.__extractMethod)
190 else:
191 self.connect(self.refactoringExtractMethodAct,
192 SIGNAL('triggered()'), self.__extractMethod)
193 self.actions.append(self.refactoringExtractMethodAct)
194
195 self.refactoringExtractLocalVariableAct = E5Action(
196 self.trUtf8('Extract local variable'),
197 self.trUtf8('&Extract Local Variable'),
198 0, 0,
199 self,'refactoring_extract_variable')
200 self.refactoringExtractLocalVariableAct.setStatusTip(self.trUtf8(
201 'Extract the highlighted area as a local variable'))
202 self.refactoringExtractLocalVariableAct.setWhatsThis(self.trUtf8(
203 """<b>Extract local variable</b>"""
204 """<p>Extract the highlighted area as a local variable.</p>"""
205 ))
206 if self.__newStyle:
207 self.refactoringExtractLocalVariableAct.triggered[()].connect(
208 self.__extractLocalVariable)
209 else:
210 self.connect(self.refactoringExtractLocalVariableAct,
211 SIGNAL('triggered()'), self.__extractLocalVariable)
212 self.actions.append(self.refactoringExtractLocalVariableAct)
213
214 #####################################################
172 ## Undo/Redo actions 215 ## Undo/Redo actions
173 ##################################################### 216 #####################################################
174 217
175 self.refactoringUndoAct = E5Action(self.trUtf8('Undo'), 218 self.refactoringUndoAct = E5Action(self.trUtf8('Undo'),
176 self.trUtf8('&Undo'), 219 self.trUtf8('&Undo'),
438 self.connect(smenu, SIGNAL("aboutToShow()"), 481 self.connect(smenu, SIGNAL("aboutToShow()"),
439 self.__showRefactoringMenu) 482 self.__showRefactoringMenu)
440 smenu.addAction(self.refactoringRenameAct) 483 smenu.addAction(self.refactoringRenameAct)
441 smenu.addAction(self.refactoringRenameLocalAct) 484 smenu.addAction(self.refactoringRenameLocalAct)
442 smenu.addAction(self.refactoringChangeOccurrencesAct) 485 smenu.addAction(self.refactoringChangeOccurrencesAct)
486 smenu.addSeparator()
487 smenu.addAction(self.refactoringExtractMethodAct)
488 ## smenu.addAction(self.refactoringMoveMethodAct)
489 smenu.addSeparator()
490 ## smenu.addAction(self.refactoringInlineAct)
491 ## smenu.addSeparator()
492 smenu.addAction(self.refactoringExtractLocalVariableAct)
443 smenu.addSeparator() 493 smenu.addSeparator()
444 smenu.addAction(self.refactoringRenameModuleAct) 494 smenu.addAction(self.refactoringRenameModuleAct)
445 smenu.addSeparator() 495 smenu.addSeparator()
446 496
447 smenu.addSeparator() 497 smenu.addSeparator()
716 self.handleRopeError(err, title) 766 self.handleRopeError(err, title)
717 return 767 return
718 768
719 self.dlg = ChangeOccurrencesDialog(self, title, renamer, 769 self.dlg = ChangeOccurrencesDialog(self, title, renamer,
720 parent=self.__ui) 770 parent=self.__ui)
771 self.dlg.show()
772
773 #####################################################
774 ## Extract refactorings
775 #####################################################
776
777 def __extractMethod(self):
778 """
779 Private slot to handle the Extract Method action.
780 """
781 self.__doExtract(self.trUtf8("Extract Method"), "method")
782
783 def __extractLocalVariable(self):
784 """
785 Private slot to handle the Extract Local Variable action.
786 """
787 self.__doExtract(self.trUtf8("Extract Local Variable"), "variable")
788
789 def __doExtract(self, title, kind):
790 """
791 Private method to perform the extract refactoring.
792
793 @param title title of the refactoring (QString)
794 @param kind kind of extraction to be done (string,
795 "method" or "variable")
796 """
797 aw = e5App().getObject("ViewManager").activeWindow()
798
799 if aw is None:
800 return
801
802 if not aw.hasSelectedText():
803 # no selection available
804 E5MessageBox.warning(self.__ui, title,
805 self.trUtf8("Highlight the region of code you want to extract"
806 " and try again."))
807 return
808
809 if not self.confirmBufferIsSaved(aw):
810 return
811
812 filename = aw.getFileName()
813 startline, startcolumn, endline, endcolumn = aw.getSelection()
814 startOffset = aw.positionFromLineIndex(startline, startcolumn)
815 endOffset = aw.positionFromLineIndex(endline, endcolumn)
816
817 resource = rope.base.libutils.path_to_resource(
818 self.__project, filename)
819 try:
820 if kind == "variable":
821 extractor = rope.refactor.extract.ExtractVariable(
822 self.__project, resource, startOffset, endOffset)
823 elif kind == "method":
824 extractor = rope.refactor.extract.ExtractMethod(
825 self.__project, resource, startOffset, endOffset)
826 else:
827 raise Exception("Invalid extraction kind <{0}>.".format(kind))
828 except Exception as err:
829 self.handleRopeError(err, title)
830 return
831
832 self.dlg = ExtractDialog(self, title, extractor, parent = self.__ui)
721 self.dlg.show() 833 self.dlg.show()
722 834
723 ##################################################### 835 #####################################################
724 ## Undo/Redo refactorings 836 ## Undo/Redo refactorings
725 ##################################################### 837 #####################################################

eric ide

mercurial