RefactoringRope/CodeAssistServer.py

changeset 293
dd1c7ed6d880
parent 291
da88cb84ae30
child 294
78e4a6823a98
--- a/RefactoringRope/CodeAssistServer.py	Sun Nov 11 19:43:10 2018 +0100
+++ b/RefactoringRope/CodeAssistServer.py	Sun Nov 11 19:43:56 2018 +0100
@@ -82,6 +82,7 @@
             "CompletionsResult": self.__processCompletionsResult,
             "CallTipsResult": self.__processCallTipsResult,
             "DocumentationResult": self.__processDocumentationResult,
+            "GotoDefinitionResult": self.__gotoDefinitionResult,
             
             "ClientException": self.__processClientException,
         }
@@ -104,6 +105,15 @@
         # Python 3
         self.__ensureActive("Python3")
     
+    def setAsyncCompletions(self, asynchronous):
+        """
+        Public method to set the asynchronous completions flag.
+        
+        @param asynchronous flag indicating asynchronous completions
+        @type bool
+        """
+        self.__asyncCompletions = asynchronous
+    
     def __updateEditorLanguageMapping(self):
         """
         Private method to update the editor language to connection mapping.
@@ -239,6 +249,9 @@
         @return list of possible completions
         @rtype list of str
         """
+        if not self.__plugin.getPreferences("CodeAssistEnabled"):
+            return []
+        
         # reset the completions buffer
         self.__completions = None
         
@@ -270,6 +283,9 @@
         @param acText text to be completed
         @type str
         """
+        if not self.__plugin.getPreferences("CodeAssistEnabled"):
+            return
+        
         idString = self.__idString(editor)
         if not idString:
             return
@@ -332,6 +348,9 @@
         @return list of possible calltips
         @rtype list of str
         """
+        if not self.__plugin.getPreferences("CodeAssistCalltipsEnabled"):
+            return []
+        
         # reset the calltips buffer
         self.__calltips = None
         
@@ -485,6 +504,59 @@
         else:
             self.__documentationViewer.documentationReady(docu)
     
+    def gotoDefinition(self, editor):
+        """
+        Public slot to find the definition for the word at the cursor position
+        and go to it.
+        
+        Note: This is executed upon a mouse click sequence.
+        
+        @param editor reference to the calling editor
+        @type QScintilla.Editor.Editor
+        """
+        if not self.__plugin.getPreferences("MouseClickEnabled"):
+            return
+        
+        idString = self.__idString(editor)
+        if not idString:
+            return
+        
+        filename = editor.getFileName()
+        source = editor.text()
+        line, index = editor.getCursorPosition()
+        offset = len("".join(source.splitlines(True)[:line])) + index
+        
+        self.__ensureActive(idString)
+        self.sendJson("gotoDefinition", {
+            "FileName": filename,
+            "Offset": offset,
+            "Source": editor.text(),
+            "SysPath": sys.path,
+        }, idString=idString)
+    
+    def __gotoDefinitionResult(self, result):
+        """
+        Private method to handle the "Goto Definition" result sent by
+        the client.
+        
+        @param result dictionary containing the result data
+        @type dict
+        """
+        if "Error" not in result:
+            # ignore errors silently
+            if "Location" in result:
+                location = result["Location"]
+                try:
+                    self.__vm.openSourceFile(
+                        location["ModulePath"], location["Line"], addNext=True)
+                except TypeError:
+                    # backward compatibility; <= 17.03
+                    self.__vm.openSourceFile(
+                        location["ModulePath"], location["Line"], next=True)
+            else:
+                e5App().getObject("UserInterface").statusBar().showMessage(
+                    self.tr('Code Assist: No definition found'), 5000)
+    
     #######################################################################
     ## Methods below handle the network connection
     #######################################################################
@@ -797,74 +869,74 @@
     ## Methods below handle setting/unsetting the hook methods
     #######################################################################
     
-    def connectEditor(self, editor):
-        """
-        Public method to connect an editor.
-        
-        @param editor reference to the editor
-        @type QScintilla.Editor.Editor
-        """
-        # TODO: special treatment for project files
-        if self.isSupportedLanguage(editor.getLanguage()):
-            if self.__plugin.getPreferences("CodeAssistEnabled") and \
-               editor.getCompletionListHook("rope") is None:
-                self.__setAutoCompletionHook(editor)
-            if self.__plugin.getPreferences("CodeAssistCalltipsEnabled") and \
-               editor.getCallTipHook("rope") is None:
-                self.__setCalltipsHook(editor)
-        else:
-            self.disconnectEditor(editor)
-    
-    def disconnectEditor(self, editor):
-        """
-        Public method to disconnect an editor.
-        
-        @param editor reference to the editor
-        @type QScintilla.Editor.Editor
-        """
-        if editor.getCompletionListHook("rope"):
-            self.__unsetAutoCompletionHook(editor)
-        if editor.getCallTipHook("rope"):
-            self.__unsetCalltipsHook(editor)
-    
-    def __setAutoCompletionHook(self, editor):
-        """
-        Private method to set the auto-completion hook.
-        
-        @param editor reference to the editor
-        @type QScintilla.Editor.Editor
-        """
-        try:
-            editor.addCompletionListHook("rope", self.requestCompletions, True)
-            self.__asyncCompletions = True
-        except TypeError:
-            # backward compatibility for eric6 before 17.11
-            editor.addCompletionListHook("rope", self.getCompletions)
-            self.__asyncCompletions = False
-    
-    def __unsetAutoCompletionHook(self, editor):
-        """
-        Private method to unset the auto-completion hook.
-        
-        @param editor reference to the editor
-        @type QScintilla.Editor.Editor
-        """
-        editor.removeCompletionListHook("rope")
-    
-    def __setCalltipsHook(self, editor):
-        """
-        Private method to set the calltip hook.
-        
-        @param editor reference to the editor
-        @type QScintilla.Editor.Editor
-        """
-        editor.addCallTipHook("rope", self.getCallTips)
-    
-    def __unsetCalltipsHook(self, editor):
-        """
-        Private method to unset the calltip hook.
-        
-        @param editor reference to the editor
-        @type QScintilla.Editor.Editor
-        """
-        editor.removeCallTipHook("rope")
+##    def connectEditor(self, editor):
+##        """
+##        Public method to connect an editor.
+##        
+##        @param editor reference to the editor
+##        @type QScintilla.Editor.Editor
+##        """
+##        # TODO: special treatment for project files
+##        if self.isSupportedLanguage(editor.getLanguage()):
+##            if self.__plugin.getPreferences("CodeAssistEnabled") and \
+##               editor.getCompletionListHook("rope") is None:
+##                self.__setAutoCompletionHook(editor)
+##            if self.__plugin.getPreferences("CodeAssistCalltipsEnabled") and \
+##               editor.getCallTipHook("rope") is None:
+##                self.__setCalltipsHook(editor)
+##        else:
+##            self.disconnectEditor(editor)
+##    
+##    def disconnectEditor(self, editor):
+##        """
+##        Public method to disconnect an editor.
+##        
+##        @param editor reference to the editor
+##        @type QScintilla.Editor.Editor
+##        """
+##        if editor.getCompletionListHook("rope"):
+##            self.__unsetAutoCompletionHook(editor)
+##        if editor.getCallTipHook("rope"):
+##            self.__unsetCalltipsHook(editor)
+##    
+##    def __setAutoCompletionHook(self, editor):
+##        """
+##        Private method to set the auto-completion hook.
+##        
+##        @param editor reference to the editor
+##        @type QScintilla.Editor.Editor
+##        """
+##        try:
+##            editor.addCompletionListHook("rope", self.requestCompletions, True)
+##            self.__asyncCompletions = True
+##        except TypeError:
+##            # backward compatibility for eric6 before 17.11
+##            editor.addCompletionListHook("rope", self.getCompletions)
+##            self.__asyncCompletions = False
+##    
+##    def __unsetAutoCompletionHook(self, editor):
+##        """
+##        Private method to unset the auto-completion hook.
+##        
+##        @param editor reference to the editor
+##        @type QScintilla.Editor.Editor
+##        """
+##        editor.removeCompletionListHook("rope")
+##    
+##    def __setCalltipsHook(self, editor):
+##        """
+##        Private method to set the calltip hook.
+##        
+##        @param editor reference to the editor
+##        @type QScintilla.Editor.Editor
+##        """
+##        editor.addCallTipHook("rope", self.getCallTips)
+##    
+##    def __unsetCalltipsHook(self, editor):
+##        """
+##        Private method to unset the calltip hook.
+##        
+##        @param editor reference to the editor
+##        @type QScintilla.Editor.Editor
+##        """
+##        editor.removeCallTipHook("rope")

eric ide

mercurial