10 import contextlib |
10 import contextlib |
11 import os |
11 import os |
12 import uuid |
12 import uuid |
13 |
13 |
14 from PyQt6.QtCore import pyqtSlot, QCoreApplication, QTimer |
14 from PyQt6.QtCore import pyqtSlot, QCoreApplication, QTimer |
|
15 from PyQt6.QtWidgets import QInputDialog, QLineEdit, QDialog |
15 |
16 |
16 from EricWidgets.EricApplication import ericApp |
17 from EricWidgets.EricApplication import ericApp |
|
18 from EricWidgets import EricMessageBox |
17 |
19 |
18 from EricNetwork.EricJsonServer import EricJsonServer |
20 from EricNetwork.EricJsonServer import EricJsonServer |
19 |
21 |
20 from QScintilla.Editor import Editor |
22 from QScintilla.Editor import Editor |
21 |
23 |
22 import Preferences |
24 import Preferences |
23 import Globals |
25 import Globals |
|
26 |
|
27 from .RefactoringPreviewDialog import RefactoringPreviewDialog |
24 |
28 |
25 |
29 |
26 class JediServer(EricJsonServer): |
30 class JediServer(EricJsonServer): |
27 """ |
31 """ |
28 Class implementing the interface to the jedi library. |
32 Class implementing the interface to the jedi library. |
84 "DocumentationResult": self.__processDocumentationResult, |
88 "DocumentationResult": self.__processDocumentationResult, |
85 "HoverHelpResult": self.__processHoverHelpResult, |
89 "HoverHelpResult": self.__processHoverHelpResult, |
86 "GotoDefinitionResult": self.__processGotoDefinitionResult, |
90 "GotoDefinitionResult": self.__processGotoDefinitionResult, |
87 "GotoReferencesResult": self.__processGotoReferencesResult, |
91 "GotoReferencesResult": self.__processGotoReferencesResult, |
88 |
92 |
|
93 "RenameVariableDiff": self.__showRenameVariableDiff, |
|
94 "RefactoringApplyResult": self.__checkRefactoringResult, |
|
95 |
89 "ClientException": self.__processClientException, |
96 "ClientException": self.__processClientException, |
90 } |
97 } |
91 |
98 |
92 # temporary store for editor references indexed by Uuid |
99 # temporary store for editor references indexed by Uuid |
93 self.__editors = {} |
100 self.__editors = {} |
476 ericApp().getObject("UserInterface").statusBar().showMessage( |
483 ericApp().getObject("UserInterface").statusBar().showMessage( |
477 self.tr('Jedi: No mouse hover help found'), 5000) |
484 self.tr('Jedi: No mouse hover help found'), 5000) |
478 |
485 |
479 with contextlib.suppress(KeyError): |
486 with contextlib.suppress(KeyError): |
480 del self.__editors[euuid] |
487 del self.__editors[euuid] |
|
488 |
|
489 ####################################################################### |
|
490 ## Refactoring methods below |
|
491 ####################################################################### |
|
492 |
|
493 @pyqtSlot() |
|
494 def refactoringRenameVariable(self): |
|
495 """ |
|
496 Public slot to rename the selected variable. |
|
497 """ |
|
498 editor = self.__vm.activeWindow() |
|
499 if editor: |
|
500 idString = self.__idString(editor) |
|
501 if not idString: |
|
502 return |
|
503 |
|
504 newName, ok = QInputDialog.getText( |
|
505 None, |
|
506 self.tr("Rename Variable"), |
|
507 self.tr("Enter the new name for the variable:"), |
|
508 QLineEdit.EchoMode.Normal |
|
509 ) |
|
510 |
|
511 if ok and newName: |
|
512 filename = editor.getFileName() |
|
513 line, index = editor.getCursorPosition() |
|
514 line += 1 # jedi line numbers are 1 based |
|
515 source = editor.text() |
|
516 |
|
517 self.__ensureActive(idString) |
|
518 |
|
519 euuid = str(uuid.uuid4()) |
|
520 self.__editors[euuid] = editor |
|
521 |
|
522 self.sendJson("renameVariable", { |
|
523 "FileName": filename, |
|
524 "Source": source, |
|
525 "Line": line, |
|
526 "Index": index, |
|
527 "Uuid": euuid, |
|
528 "NewName": newName, |
|
529 }, idString=idString) |
|
530 |
|
531 def __showRenameVariableDiff(self, result): |
|
532 """ |
|
533 Private method to show the diff of the Rename Variable refactoring. |
|
534 |
|
535 @param result dictionary containing the result data |
|
536 @type dict |
|
537 """ |
|
538 if "Error" not in result: |
|
539 # ignore errors silently |
|
540 euuid = result["Uuid"] |
|
541 diff = result["Diff"] |
|
542 dlg = RefactoringPreviewDialog(self.tr("Rename Variable"), diff) |
|
543 if dlg.exec() == QDialog.DialogCode.Accepted: |
|
544 self.__applyRefactoring(euuid) |
|
545 else: |
|
546 self.__cancelRefactoring(euuid) |
|
547 |
|
548 def __applyRefactoring(self, uid): |
|
549 """ |
|
550 Private method to apply a given refactoring. |
|
551 |
|
552 @param uid UID of the calculated refactoring |
|
553 @type str |
|
554 """ |
|
555 with contextlib.suppress(KeyError): |
|
556 editor = self.__editors[uid] |
|
557 idString = self.__idString(editor) |
|
558 |
|
559 self.sendJson("applyRefactoring", { |
|
560 "Uuid": uid, |
|
561 }, idString=idString) |
|
562 |
|
563 del self.__editors[uid] |
|
564 |
|
565 def __cancelRefactoring(self, uid): |
|
566 """ |
|
567 Private method to cancel a given refactoring. |
|
568 |
|
569 @param uid UID of the calculated refactoring |
|
570 @type str |
|
571 """ |
|
572 with contextlib.suppress(KeyError): |
|
573 editor = self.__editors[uid] |
|
574 idString = self.__idString(editor) |
|
575 |
|
576 self.sendJson("cancelRefactoring", { |
|
577 "Uuid": uid, |
|
578 }, idString=idString) |
|
579 |
|
580 del self.__editors[uid] |
|
581 |
|
582 def __checkRefactoringResult(self, result): |
|
583 """ |
|
584 Private method to check the refactoring result for errors. |
|
585 |
|
586 @param result dictionary containing the result data |
|
587 @type dict |
|
588 """ |
|
589 if "Error" in result: |
|
590 EricMessageBox.critical( |
|
591 self, |
|
592 self.tr("Apply Refactoring"), |
|
593 self.tr("<p>The refactoring could not be applied.</p>" |
|
594 "<p>Reason: {0}</p>").format(result["ErrorString"]) |
|
595 ) |
481 |
596 |
482 ####################################################################### |
597 ####################################################################### |
483 ## Methods below handle the network connection |
598 ## Methods below handle the network connection |
484 ####################################################################### |
599 ####################################################################### |
485 |
600 |