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 |
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 |
32 |
32 |
33 import rope.contrib.findit |
33 import rope.contrib.findit |
34 ##import rope.contrib.finderrors |
34 ##import rope.contrib.finderrors |
35 |
35 |
36 from PyQt4.QtCore import QObject, SIGNAL |
36 from PyQt4.QtCore import QObject, SIGNAL |
37 from PyQt4.QtGui import QMenu, QApplication, QMessageBox |
37 from PyQt4.QtGui import QMenu, QApplication, QMessageBox, QDialog |
38 |
38 |
39 from E5Gui.E5Application import e5App |
39 from E5Gui.E5Application import e5App |
40 |
40 |
41 from E5Gui import E5MessageBox |
41 from E5Gui import E5MessageBox |
42 from E5Gui.E5Action import E5Action |
42 from E5Gui.E5Action import E5Action |
296 """<p>Introduce a parameter in a function.</p>""" |
300 """<p>Introduce a parameter in a function.</p>""" |
297 )) |
301 )) |
298 self.refactoringIntroduceParameterAct.triggered[()].connect( |
302 self.refactoringIntroduceParameterAct.triggered[()].connect( |
299 self.__introduceParameter) |
303 self.__introduceParameter) |
300 self.actions.append(self.refactoringIntroduceParameterAct) |
304 self.actions.append(self.refactoringIntroduceParameterAct) |
|
305 |
|
306 ##################################################### |
|
307 ## Import refactorings actions |
|
308 ##################################################### |
|
309 |
|
310 self.refactoringImportsOrganizeAct = E5Action( |
|
311 self.trUtf8('Organize Imports'), |
|
312 self.trUtf8('&Organize Imports'), |
|
313 0, 0, |
|
314 self,'refactoring_organize_imports') |
|
315 self.refactoringImportsOrganizeAct.setStatusTip(self.trUtf8( |
|
316 'Sort imports according to PEP-8')) |
|
317 self.refactoringImportsOrganizeAct.setWhatsThis(self.trUtf8( |
|
318 """<b>Organize Imports</b>""" |
|
319 """<p>Sort imports according to PEP-8.</p>""" |
|
320 )) |
|
321 self.refactoringImportsOrganizeAct.triggered[()].connect( |
|
322 self.__importsOrganize) |
|
323 self.actions.append(self.refactoringImportsOrganizeAct) |
|
324 |
|
325 self.refactoringImportsStarExpandAct = E5Action( |
|
326 self.trUtf8('Expand Star Imports'), |
|
327 self.trUtf8('E&xpand Star Imports'), |
|
328 0, 0, |
|
329 self,'refactoring_expand_star_imports') |
|
330 self.refactoringImportsStarExpandAct.setStatusTip(self.trUtf8( |
|
331 'Expand imports like "from xxx import *"')) |
|
332 self.refactoringImportsStarExpandAct.setWhatsThis(self.trUtf8( |
|
333 """<b>Expand Star Imports</b>""" |
|
334 """<p>Expand imports like "from xxx import *".</p>""" |
|
335 """<p>Select the import to act on or none to do all.""" |
|
336 """ Unused imports are deleted.</p>""" |
|
337 )) |
|
338 self.refactoringImportsStarExpandAct.triggered[()].connect( |
|
339 self.__importsExpandStar) |
|
340 self.actions.append(self.refactoringImportsStarExpandAct) |
|
341 |
|
342 self.refactoringImportsRelativeToAbsoluteAct = E5Action( |
|
343 self.trUtf8('Relative to Absolute'), |
|
344 self.trUtf8('Relative to &Absolute'), |
|
345 0, 0, |
|
346 self,'refactoring_relative_to_absolute_imports') |
|
347 self.refactoringImportsRelativeToAbsoluteAct.setStatusTip(self.trUtf8( |
|
348 'Transform relative imports to absolute ones')) |
|
349 self.refactoringImportsRelativeToAbsoluteAct.setWhatsThis(self.trUtf8( |
|
350 """<b>Relative to Absolute</b>""" |
|
351 """<p>Transform relative imports to absolute ones.</p>""" |
|
352 """<p>Select the import to act on or none to do all.""" |
|
353 """ Unused imports are deleted.</p>""" |
|
354 )) |
|
355 self.refactoringImportsRelativeToAbsoluteAct.triggered[()].connect( |
|
356 self.__importsRelativeToAbsolute) |
|
357 self.actions.append(self.refactoringImportsRelativeToAbsoluteAct) |
|
358 |
|
359 self.refactoringImportsFromsToImportsAct = E5Action( |
|
360 self.trUtf8('Froms to Imports'), |
|
361 self.trUtf8('Froms to &Imports'), |
|
362 0, 0, |
|
363 self,'refactoring_froms_to_imports') |
|
364 self.refactoringImportsFromsToImportsAct.setStatusTip(self.trUtf8( |
|
365 'Transform From imports to plain imports')) |
|
366 self.refactoringImportsFromsToImportsAct.setWhatsThis(self.trUtf8( |
|
367 """<b>Froms to Imports</b>""" |
|
368 """<p>Transform From imports to plain imports.</p>""" |
|
369 """<p>Select the import to act on or none to do all.""" |
|
370 """ Unused imports are deleted.</p>""" |
|
371 )) |
|
372 self.refactoringImportsFromsToImportsAct.triggered[()].connect( |
|
373 self.__importsFromToImport) |
|
374 self.actions.append(self.refactoringImportsFromsToImportsAct) |
|
375 |
|
376 self.refactoringImportsHandleLongAct = E5Action( |
|
377 self.trUtf8('Handle Long Imports'), |
|
378 self.trUtf8('Handle &Long Imports'), |
|
379 0, 0, |
|
380 self,'refactoring_organize_imports') |
|
381 self.refactoringImportsHandleLongAct.setStatusTip(self.trUtf8( |
|
382 'Transform long import statements to look better')) |
|
383 self.refactoringImportsHandleLongAct.setWhatsThis(self.trUtf8( |
|
384 """<b>Handle Long Imports</b>""" |
|
385 """<p>Transform long import statements to look better.</p>""" |
|
386 """<p>Select the import to act on or none to do all.""" |
|
387 """ Unused imports are deleted.</p>""" |
|
388 )) |
|
389 self.refactoringImportsHandleLongAct.triggered[()].connect( |
|
390 self.__importsHandleLong) |
|
391 self.actions.append(self.refactoringImportsHandleLongAct) |
|
392 |
|
393 ##################################################### |
|
394 ## Various refactorings actions |
|
395 ##################################################### |
|
396 |
|
397 self.refactoringRestructureAct = E5Action( |
|
398 self.trUtf8('Restructure'), |
|
399 self.trUtf8('Res&tructure'), |
|
400 0, 0, |
|
401 self,'refactoring_restructure') |
|
402 self.refactoringRestructureAct.setStatusTip(self.trUtf8( |
|
403 'Restructure code')) |
|
404 self.refactoringRestructureAct.setWhatsThis(self.trUtf8( |
|
405 """<b>Restructure</b>""" |
|
406 """<p>Restructure code. See "Rope Help" for examples.</p>""" |
|
407 )) |
|
408 self.refactoringRestructureAct.triggered[()].connect( |
|
409 self.__restructure) |
|
410 self.actions.append(self.refactoringRestructureAct) |
|
411 |
|
412 self.refactoringChangeSignatureAct = E5Action( |
|
413 self.trUtf8('Change Method Signature'), |
|
414 self.trUtf8('&Change Method Signature'), |
|
415 0, 0, |
|
416 self,'refactoring_change_method_signature') |
|
417 self.refactoringChangeSignatureAct.setStatusTip(self.trUtf8( |
|
418 'Change the signature of the selected method or function')) |
|
419 self.refactoringChangeSignatureAct.setWhatsThis(self.trUtf8( |
|
420 """<b>Change Method Signature</b>""" |
|
421 """<p>Change the signature of the selected method""" |
|
422 """ or function.</p>""" |
|
423 )) |
|
424 self.refactoringChangeSignatureAct.triggered[()].connect( |
|
425 self.__changeSignature) |
|
426 self.actions.append(self.refactoringChangeSignatureAct) |
|
427 |
|
428 self.refactoringInlineArgumentDefaultAct = E5Action( |
|
429 self.trUtf8('Inline Argument Default'), |
|
430 self.trUtf8('Inline &Argument Default'), |
|
431 0, 0, |
|
432 self,'refactoring_inline_argument_default') |
|
433 self.refactoringInlineArgumentDefaultAct.setStatusTip(self.trUtf8( |
|
434 'Inline a parameters default value')) |
|
435 self.refactoringInlineArgumentDefaultAct.setWhatsThis(self.trUtf8( |
|
436 """<b>Inline Argument Default</b>""" |
|
437 """<p>Inline a parameters default value.</p>""" |
|
438 )) |
|
439 self.refactoringInlineArgumentDefaultAct.triggered[()].connect( |
|
440 self.__inlineArgumentDefault) |
|
441 self.actions.append(self.refactoringInlineArgumentDefaultAct) |
301 |
442 |
302 ##################################################### |
443 ##################################################### |
303 ## Undo/Redo actions |
444 ## Undo/Redo actions |
304 ##################################################### |
445 ##################################################### |
305 |
446 |
1107 self.handleRopeError(err, title) |
1255 self.handleRopeError(err, title) |
1108 return |
1256 return |
1109 |
1257 |
1110 self.dlg = IntroduceParameterDialog(self, title, introducer, |
1258 self.dlg = IntroduceParameterDialog(self, title, introducer, |
1111 parent=self.__ui) |
1259 parent=self.__ui) |
|
1260 self.dlg.show() |
|
1261 |
|
1262 ##################################################### |
|
1263 ## Import refactorings |
|
1264 ##################################################### |
|
1265 |
|
1266 def __importsOrganize(self): |
|
1267 """ |
|
1268 Private slot to organize imports. |
|
1269 """ |
|
1270 self.__doImports(self.trUtf8("Organize Imports"), |
|
1271 ImportOrganizer.organize_imports) |
|
1272 |
|
1273 def __importsExpandStar(self): |
|
1274 """ |
|
1275 Private slot to expand star imports. |
|
1276 """ |
|
1277 self.__doImports(self.trUtf8("Expand Star Imports"), |
|
1278 ImportOrganizer.expand_star_imports) |
|
1279 |
|
1280 def __importsRelativeToAbsolute(self): |
|
1281 """ |
|
1282 Private slot to transform relative to absolute imports. |
|
1283 """ |
|
1284 self.__doImports(self.trUtf8("Relative to Absolute"), |
|
1285 ImportOrganizer.relatives_to_absolutes) |
|
1286 |
|
1287 def __importsFromToImport(self): |
|
1288 """ |
|
1289 Private slot to transform from imports to plain imports. |
|
1290 """ |
|
1291 self.__doImports(self.trUtf8("Froms to Imports"), |
|
1292 ImportOrganizer.froms_to_imports) |
|
1293 |
|
1294 def __importsHandleLong(self): |
|
1295 """ |
|
1296 Private slot to handle long imports. |
|
1297 """ |
|
1298 self.__doImports(self.trUtf8("Handle Long Imports"), |
|
1299 ImportOrganizer.handle_long_imports) |
|
1300 |
|
1301 def __doImports(self, title, method): |
|
1302 """ |
|
1303 Private method to perform the various imports refactorings. |
|
1304 """ |
|
1305 aw = e5App().getObject("ViewManager").activeWindow() |
|
1306 |
|
1307 if aw is None: |
|
1308 return |
|
1309 |
|
1310 if not self.confirmBufferIsSaved(aw): |
|
1311 return |
|
1312 |
|
1313 filename = aw.getFileName() |
|
1314 if aw.hasSelectedText(): |
|
1315 line, index, line1, index1 = aw.getSelection() |
|
1316 offset = aw.positionFromLineIndex(line, index) |
|
1317 else: |
|
1318 offset = None |
|
1319 |
|
1320 importOrganizer = ImportOrganizer(self.__project) |
|
1321 resource = rope.base.libutils.path_to_resource( |
|
1322 self.__project, filename) |
|
1323 try: |
|
1324 changes = method(importOrganizer, resource, offset=offset) |
|
1325 if changes is not None: |
|
1326 dlg = ConfirmationDialog(changes, self.__ui) |
|
1327 if dlg.exec_() == QDialog.Accepted: |
|
1328 self.__project.do(changes) |
|
1329 self.refreshEditors(changes) |
|
1330 if self.__e5project.isDirty(): |
|
1331 self.__e5project.saveProject() |
|
1332 else: |
|
1333 E5MessageBox.information(self.__ui, title, |
|
1334 self.trUtf8("The selected refactoring did not produce" |
|
1335 " any change.")) |
|
1336 except Exception as err: |
|
1337 self.handleRopeError(err, title) |
|
1338 |
|
1339 ##################################################### |
|
1340 ## Various refactorings |
|
1341 ##################################################### |
|
1342 |
|
1343 def __restructure(self): |
|
1344 """ |
|
1345 Private slot to restructure code. |
|
1346 """ |
|
1347 title = self.trUtf8("Restructure") |
|
1348 self.dlg = RestructureDialog(self, title, parent=self.__ui) |
|
1349 self.dlg.show() |
|
1350 |
|
1351 def __changeSignature(self): |
|
1352 """ |
|
1353 Private slot to change the signature of a method or function. |
|
1354 """ |
|
1355 aw = e5App().getObject("ViewManager").activeWindow() |
|
1356 |
|
1357 if aw is None: |
|
1358 return |
|
1359 |
|
1360 title = self.trUtf8("Change Method Signature") |
|
1361 if not aw.hasSelectedText(): |
|
1362 # no selection available |
|
1363 E5MessageBox.warning(self.__ui, title, |
|
1364 self.trUtf8("Highlight the method or function to change" |
|
1365 " and try again.")) |
|
1366 return |
|
1367 |
|
1368 if not self.confirmAllBuffersSaved(): |
|
1369 return |
|
1370 |
|
1371 filename = aw.getFileName() |
|
1372 line, index, line1, index1 = aw.getSelection() |
|
1373 offset = aw.positionFromLineIndex(line, index) |
|
1374 |
|
1375 resource = rope.base.libutils.path_to_resource( |
|
1376 self.__project, filename) |
|
1377 try: |
|
1378 changer = rope.refactor.change_signature.ChangeSignature( |
|
1379 self.__project, resource, offset) |
|
1380 except Exception as err: |
|
1381 self.handleRopeError(err, title) |
|
1382 return |
|
1383 |
|
1384 self.dlg = ChangeSignatureDialog(self, title, changer, |
|
1385 parent=self.__ui) |
|
1386 self.dlg.show() |
|
1387 |
|
1388 def __inlineArgumentDefault(self): |
|
1389 """ |
|
1390 Private slot to inline the default value of a parameter of a |
|
1391 method or function. |
|
1392 """ |
|
1393 aw = e5App().getObject("ViewManager").activeWindow() |
|
1394 |
|
1395 if aw is None: |
|
1396 return |
|
1397 |
|
1398 title = self.trUtf8("Inline Argument Default") |
|
1399 if not aw.hasSelectedText(): |
|
1400 # no selection available |
|
1401 E5MessageBox.warning(self.__ui, title, |
|
1402 self.trUtf8("Highlight the method or function to inline" |
|
1403 " a parameter's default and try again.")) |
|
1404 return |
|
1405 |
|
1406 if not self.confirmAllBuffersSaved(): |
|
1407 return |
|
1408 |
|
1409 filename = aw.getFileName() |
|
1410 line, index, line1, index1 = aw.getSelection() |
|
1411 offset = aw.positionFromLineIndex(line, index) |
|
1412 |
|
1413 resource = rope.base.libutils.path_to_resource( |
|
1414 self.__project, filename) |
|
1415 try: |
|
1416 changer = rope.refactor.change_signature.ChangeSignature( |
|
1417 self.__project, resource, offset) |
|
1418 except Exception as err: |
|
1419 self.handleRopeError(err, title) |
|
1420 return |
|
1421 |
|
1422 self.dlg = InlineArgumentDefaultDialog(self, title, changer, |
|
1423 parent=self.__ui) |
1112 self.dlg.show() |
1424 self.dlg.show() |
1113 |
1425 |
1114 ##################################################### |
1426 ##################################################### |
1115 ## Undo/Redo refactorings |
1427 ## Undo/Redo refactorings |
1116 ##################################################### |
1428 ##################################################### |