RefactoringRope/Refactoring.py

changeset 10
0fdfae822ca7
parent 8
fc14525f0501
child 11
562f9758d2e1
equal deleted inserted replaced
9:8cee612bcc28 10:0fdfae822ca7
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
26 ##import rope.refactor.introduce_parameter 26 import rope.refactor.introduce_parameter
27 ##import rope.refactor.method_object 27 ##import rope.refactor.method_object
28 ##import rope.refactor.encapsulate_field 28 ##import rope.refactor.encapsulate_field
29 ##import rope.refactor.localtofield 29 ##import rope.refactor.localtofield
30 ##import rope.refactor.topackage 30 ##import rope.refactor.topackage
31 ##from rope.refactor.importutils import ImportOrganizer 31 ##from rope.refactor.importutils import ImportOrganizer
53 from ExtractDialog import ExtractDialog 53 from ExtractDialog import ExtractDialog
54 from InlineDialog import InlineDialog 54 from InlineDialog import InlineDialog
55 from MoveMethodDialog import MoveMethodDialog 55 from MoveMethodDialog import MoveMethodDialog
56 from MoveModuleDialog import MoveModuleDialog 56 from MoveModuleDialog import MoveModuleDialog
57 from UseFunctionDialog import UseFunctionDialog 57 from UseFunctionDialog import UseFunctionDialog
58 from IntroduceFactoryDialog import IntroduceFactoryDialog
59 from IntroduceParameterDialog import IntroduceParameterDialog
58 60
59 import Utilities 61 import Utilities
60 62
61 63
62 class Refactoring(QObject): 64 class Refactoring(QObject):
260 """<p>Tries to use a function wherever possible.</p>""" 262 """<p>Tries to use a function wherever possible.</p>"""
261 )) 263 ))
262 self.refactoringUseFunctionAct.triggered[()].connect( 264 self.refactoringUseFunctionAct.triggered[()].connect(
263 self.__useFunction) 265 self.__useFunction)
264 self.actions.append(self.refactoringUseFunctionAct) 266 self.actions.append(self.refactoringUseFunctionAct)
267
268 #####################################################
269 ## Introduce refactorings actions
270 #####################################################
271
272 self.refactoringIntroduceFactoryAct = E5Action(
273 self.trUtf8('Introduce Factory Method'),
274 self.trUtf8('Introduce &Factory Method'),
275 0, 0,
276 self,'refactoring_introduce_factory_method')
277 self.refactoringIntroduceFactoryAct.setStatusTip(self.trUtf8(
278 'Introduce a factory method or function'))
279 self.refactoringIntroduceFactoryAct.setWhatsThis(self.trUtf8(
280 """<b>Introduce Factory Method</b>"""
281 """<p>Introduce a factory method or function.</p>"""
282 ))
283 self.refactoringIntroduceFactoryAct.triggered[()].connect(
284 self.__introduceFactoryMethod)
285 self.actions.append(self.refactoringIntroduceFactoryAct)
286
287 self.refactoringIntroduceParameterAct = E5Action(
288 self.trUtf8('Introduce Parameter'),
289 self.trUtf8('Introduce &Parameter'),
290 0, 0,
291 self,'refactoring_introduce_parameter_method')
292 self.refactoringIntroduceParameterAct.setStatusTip(self.trUtf8(
293 'Introduce a parameter in a function'))
294 self.refactoringIntroduceParameterAct.setWhatsThis(self.trUtf8(
295 """<b>Introduce Parameter</b>"""
296 """<p>Introduce a parameter in a function.</p>"""
297 ))
298 self.refactoringIntroduceParameterAct.triggered[()].connect(
299 self.__introduceParameter)
300 self.actions.append(self.refactoringIntroduceParameterAct)
265 301
266 ##################################################### 302 #####################################################
267 ## Undo/Redo actions 303 ## Undo/Redo actions
268 ##################################################### 304 #####################################################
269 305
491 smenu.addSeparator() 527 smenu.addSeparator()
492 smenu.addAction(self.refactoringExtractLocalVariableAct) 528 smenu.addAction(self.refactoringExtractLocalVariableAct)
493 smenu.addSeparator() 529 smenu.addSeparator()
494 smenu.addAction(self.refactoringUseFunctionAct) 530 smenu.addAction(self.refactoringUseFunctionAct)
495 smenu.addSeparator() 531 smenu.addSeparator()
532 ## smenu.addAction(self.refactoringChangeSignatureAct)
533 ## smenu.addAction(self.refactoringInlineArgumentDefaultAct)
534 smenu.addSeparator()
535 ## smenu.addAction(self.refactoringRestructureAct)
536 smenu.addSeparator()
537 smenu.addAction(self.refactoringIntroduceFactoryAct)
538 smenu.addAction(self.refactoringIntroduceParameterAct)
539 ## smenu.addAction(self.refactoringMethodToMethodObjectAct)
540 smenu.addSeparator()
541 ## smenu.addAction(self.refactoringEncapsulateAttributeAct)
542 ## smenu.addAction(self.refactoringLocalVariableToAttributeAct)
543 smenu.addSeparator()
496 smenu.addAction(self.refactoringRenameModuleAct) 544 smenu.addAction(self.refactoringRenameModuleAct)
497 smenu.addAction(self.refactoringMoveModuleAct) 545 smenu.addAction(self.refactoringMoveModuleAct)
546 ## smenu.addAction(self.refactoringTransformModuleAct)
498 smenu.addSeparator() 547 smenu.addSeparator()
499 548
500 smenu.addSeparator() 549 smenu.addSeparator()
501 smenu.addAction(self.refactoringUndoAct) 550 smenu.addAction(self.refactoringUndoAct)
502 smenu.addAction(self.refactoringRedoAct) 551 smenu.addAction(self.refactoringRedoAct)
791 840
792 def __doExtract(self, title, kind): 841 def __doExtract(self, title, kind):
793 """ 842 """
794 Private method to perform the extract refactoring. 843 Private method to perform the extract refactoring.
795 844
796 @param title title of the refactoring (QString) 845 @param title title of the refactoring (string)
797 @param kind kind of extraction to be done (string, 846 @param kind kind of extraction to be done (string,
798 "method" or "variable") 847 "method" or "variable")
799 """ 848 """
800 aw = e5App().getObject("ViewManager").activeWindow() 849 aw = e5App().getObject("ViewManager").activeWindow()
801 850
979 except Exception as err: 1028 except Exception as err:
980 self.handleRopeError(err, title) 1029 self.handleRopeError(err, title)
981 return 1030 return
982 1031
983 self.dlg = UseFunctionDialog(self, title, user, parent = self.__ui) 1032 self.dlg = UseFunctionDialog(self, title, user, parent = self.__ui)
1033 self.dlg.show()
1034
1035 #####################################################
1036 ## Introduce refactorings
1037 #####################################################
1038
1039 def __introduceFactoryMethod(self):
1040 """
1041 Private slot to introduce a factory method or global function.
1042 """
1043 aw = e5App().getObject("ViewManager").activeWindow()
1044
1045 if aw is None:
1046 return
1047
1048 title = self.trUtf8("Introduce Factory Method")
1049 if not aw.hasSelectedText():
1050 # no selection available
1051 E5MessageBox.warning(self.__ui, title,
1052 self.trUtf8("Highlight the class to introduce a factory"
1053 " method for and try again."))
1054 return
1055
1056 if not self.confirmAllBuffersSaved():
1057 return
1058
1059 filename = aw.getFileName()
1060 line, index, line1, index1 = aw.getSelection()
1061 offset = aw.positionFromLineIndex(line, index)
1062
1063 resource = rope.base.libutils.path_to_resource(
1064 self.__project, filename)
1065 try:
1066 introducer = \
1067 rope.refactor.introduce_factory.IntroduceFactoryRefactoring(
1068 self.__project, resource, offset)
1069 except Exception as err:
1070 self.handleRopeError(err, title)
1071 return
1072
1073 self.dlg = IntroduceFactoryDialog(self, title, introducer,
1074 parent=self.__ui)
1075 self.dlg.show()
1076
1077 def __introduceParameter(self):
1078 """
1079 Private slot to introduce a parameter in a function.
1080 """
1081 aw = e5App().getObject("ViewManager").activeWindow()
1082
1083 if aw is None:
1084 return
1085
1086 title = self.trUtf8("Introduce Parameter")
1087 if not aw.hasSelectedText():
1088 # no selection available
1089 E5MessageBox.warning(self.__ui, title,
1090 self.trUtf8("Highlight the code for the new parameter"
1091 " and try again."))
1092 return
1093
1094 if not self.confirmAllBuffersSaved():
1095 return
1096
1097 filename = aw.getFileName()
1098 line, index, line1, index1 = aw.getSelection()
1099 offset = aw.positionFromLineIndex(line, index)
1100
1101 resource = rope.base.libutils.path_to_resource(
1102 self.__project, filename)
1103 try:
1104 introducer = rope.refactor.introduce_parameter.IntroduceParameter(
1105 self.__project, resource, offset)
1106 except Exception as err:
1107 self.handleRopeError(err, title)
1108 return
1109
1110 self.dlg = IntroduceParameterDialog(self, title, introducer,
1111 parent=self.__ui)
984 self.dlg.show() 1112 self.dlg.show()
985 1113
986 ##################################################### 1114 #####################################################
987 ## Undo/Redo refactorings 1115 ## Undo/Redo refactorings
988 ##################################################### 1116 #####################################################

eric ide

mercurial