eric7/JediInterface/JediServer.py

branch
eric7
changeset 8668
d29c775b8bd7
parent 8666
3a62b4009df9
child 8669
c26ecdb00a8b
--- a/eric7/JediInterface/JediServer.py	Mon Oct 04 19:59:59 2021 +0200
+++ b/eric7/JediInterface/JediServer.py	Tue Oct 05 18:13:40 2021 +0200
@@ -90,7 +90,7 @@
             "GotoDefinitionResult": self.__processGotoDefinitionResult,
             "GotoReferencesResult": self.__processGotoReferencesResult,
             
-            "RenameVariableDiff": self.__showRenameVariableDiff,
+            "RefactoringDiff": self.__showRefactoringDiff,
             "RefactoringApplyResult": self.__checkRefactoringResult,
             
             "ClientException": self.__processClientException,
@@ -511,7 +511,6 @@
             if ok and newName:
                 filename = editor.getFileName()
                 line, index = editor.getCursorPosition()
-                line += 1       # jedi line numbers are 1 based
                 source = editor.text()
                 
                 self.__ensureActive(idString)
@@ -522,21 +521,89 @@
                 self.sendJson("renameVariable", {
                     "FileName": filename,
                     "Source": source,
-                    "Line": line,
+                    "Line": line + 1,
                     "Index": index,
                     "Uuid": euuid,
                     "NewName": newName,
                 }, idString=idString)
     
-    def __showRenameVariableDiff(self, result):
+    @pyqtSlot()
+    def refactoringExtractNewVariable(self):
+        """
+        Public slot to extract a statement to a new variable.
         """
-        Private method to show the diff of the Rename Variable refactoring.
+        editor = self.__vm.activeWindow()
+        if editor:
+            idString = self.__idString(editor)
+            if not idString:
+                return
+            
+            newName, ok = QInputDialog.getText(
+                None,
+                self.tr("Extract Variable"),
+                self.tr("Enter the name for the new variable:"),
+                QLineEdit.EchoMode.Normal
+            )
+            
+            if ok and newName:
+                filename = editor.getFileName()
+                sLine, sIndex, eLine, eIndex = editor.getSelection()
+                source = editor.text()
+                    
+                self.__ensureActive(idString)
+                
+                euuid = str(uuid.uuid4())
+                self.__editors[euuid] = editor
+                
+                self.sendJson("extractVariable", {
+                    "FileName": filename,
+                    "Source": source,
+                    "Line": sLine + 1,
+                    "Index": sIndex,
+                    "EndLine": eLine + 1,
+                    "EndIndex": eIndex,
+                    "Uuid": euuid,
+                    "NewName": newName,
+                }, idString=idString)
+    
+    @pyqtSlot()
+    def refactoringInlineVariable(self):
+        """
+        Public slot to inline the selected variable.
+        
+        Note: This is the opposite to Extract New Variable.
+        """
+        editor = self.__vm.activeWindow()
+        if editor:
+            idString = self.__idString(editor)
+            if not idString:
+                return
+            
+            filename = editor.getFileName()
+            line, index = editor.getCursorPosition()
+            source = editor.text()
+            
+            self.__ensureActive(idString)
+            
+            euuid = str(uuid.uuid4())
+            self.__editors[euuid] = editor
+            
+            self.sendJson("inlineVariable", {
+                "FileName": filename,
+                "Source": source,
+                "Line": line + 1,
+                "Index": index,
+                "Uuid": euuid,
+            }, idString=idString)
+    
+    def __showRefactoringDiff(self, result):
+        """
+        Private method to show the diff of a refactoring.
         
         @param result dictionary containing the result data
         @type dict
         """
         if "Error" not in result:
-            # ignore errors silently
             euuid = result["Uuid"]
             diff = result["Diff"]
             dlg = RefactoringPreviewDialog(self.tr("Rename Variable"), diff)
@@ -544,6 +611,13 @@
                 self.__applyRefactoring(euuid)
             else:
                 self.__cancelRefactoring(euuid)
+        else:
+            EricMessageBox.critical(
+                None,
+                self.tr("Refactoring"),
+                self.tr("<p>The refactoring could not be performed.</p>"
+                        "<p>Reason: {0}</p>").format(result["ErrorString"])
+            )
     
     def __applyRefactoring(self, uid):
         """
@@ -588,7 +662,7 @@
         """
         if "Error" in result:
             EricMessageBox.critical(
-                self,
+                None,
                 self.tr("Apply Refactoring"),
                 self.tr("<p>The refactoring could not be applied.</p>"
                         "<p>Reason: {0}</p>").format(result["ErrorString"])

eric ide

mercurial