--- a/RefactoringRope/CodeAssistServer.py Sun Oct 08 19:11:45 2017 +0200 +++ b/RefactoringRope/CodeAssistServer.py Sat Oct 14 19:31:11 2017 +0200 @@ -10,6 +10,7 @@ from __future__ import unicode_literals import os +import sys from PyQt5.QtCore import pyqtSlot, QCoreApplication, QTimer @@ -48,6 +49,8 @@ self.__asyncCompletions = False + self.__documentationViewer = None + # attributes to store the resuls of the client side self.__completions = None self.__calltips = None @@ -56,6 +59,7 @@ "Config": self.__setConfig, "CompletionsResult": self.__processCompletionsResult, "CallTipsResult": self.__processCallTipsResult, + "DocumentationResult": self.__processDocumentationResult, "ClientException": self.__processClientException, } @@ -172,7 +176,7 @@ Note: This is the synchronous variant for eric6 before 17.11. @param editor reference to the editor object, that called this method - @type QScintilla.Editor + @type QScintilla.Editor.Editor @param context flag indicating to autocomplete a context @type bool @return list of possible completions @@ -204,7 +208,7 @@ later. @param editor reference to the editor object, that called this method - @type QScintilla.Editor + @type QScintilla.Editor.Editor @param context flag indicating to autocomplete a context @type bool @param acText text to be completed @@ -228,6 +232,7 @@ "Offset": offset, "MaxFixes": maxfixes, "CompletionText": acText, + "SysPath": sys.path, }, idString=idString) def __processCompletionsResult(self, result): @@ -256,7 +261,7 @@ Public method to calculate calltips. @param editor reference to the editor object, that called this method - @type QScintilla.Editor + @type QScintilla.Editor.Editor @param pos position in the text for the calltip @type int @param commas minimum number of commas contained in the calltip @@ -284,6 +289,7 @@ "Source": source, "Offset": offset, "MaxFixes": maxfixes, + "SysPath": sys.path, }, idString=idString) # emulate the synchronous behaviour @@ -328,6 +334,59 @@ "OldSource": oldSource, }, idString=idString) + def requestCodeDocumentation(self, editor): + """ + Public method to request source code documentation for the given + editor. + + @param editor reference to the editor to get source code documentation + for + @type QScintilla.Editor.Editor + """ + language = editor.getLanguage() + if language not in self.__editorLanguageMapping: + return + idString = self.__editorLanguageMapping[language] + + filename = editor.getFileName() + source = editor.text() + line, index = editor.getCursorPosition() + offset = len("".join(source.splitlines(True)[:line])) + index + maxfixes = self.__plugin.getPreferences("CalltipsMaxFixes") + + offset = editor.positionBefore(offset) + if editor.charAt(offset) == "(": + offset = editor.positionBefore(offset) + + self.__ensureActive(idString) + self.sendJson("getDocumentation", { + "FileName": filename, + "Source": source, + "Offset": offset, + "MaxFixes": maxfixes, + "SysPath": sys.path, + }, idString=idString) + + def __processDocumentationResult(self, result): + """ + Private method to process the documentation sent by the client. + + @param result dictionary containing the result sent by the client + @type dict + """ + if "Error" not in result: + documentationDict = result["DocumentationDict"] + if "module" in documentationDict: + documentationDict["note"] = \ + self.tr("Present in {0} module").format( + documentationDict["module"]) + del documentationDict["module"] + docu = documentationDict + else: + docu = None + + self.__documentationViewer.documentationReady(docu) + ####################################################################### ## Methods below handle the network connection ####################################################################### @@ -414,6 +473,8 @@ A non-active client will be started. + @param idString id of the client to be checked + @type str @return flag indicating an active client @rtype bool """ @@ -444,11 +505,14 @@ def activate(self): """ Public method to activate the code assist server. - - Note: This method provides for some growth potential. - Currently it is empty. """ - pass + try: + self.__documentationViewer = self.__ui.documentationViewer() + self.__documentationViewer.registerProvider( + "rope", self.tr("Rope"), self.requestCodeDocumentation) + except AttributeError: + # eric6 before 17.11 doesn't have this + pass def deactivate(self): """ @@ -457,6 +521,9 @@ """ Public method to shut down the code assist server. """ + if self.__documentationViewer is not None: + self.__documentationViewer.unregisterProvider("rope") + for idString in self.connectionNames(): self.sendJson("closeProject", {}, flush=True, idString=idString) @@ -471,7 +538,7 @@ Public method to connect an editor. @param editor reference to the editor - @type QScintilla.Editor + @type QScintilla.Editor.Editor """ if self.isSupportedLanguage(editor.getLanguage()): if self.__plugin.getPreferences("CodeAssistEnabled") and \ @@ -488,7 +555,7 @@ Public method to disconnect an editor. @param editor reference to the editor - @type QScintilla.Editor + @type QScintilla.Editor.Editor """ if editor.getCompletionListHook("rope"): self.__unsetAutoCompletionHook(editor) @@ -500,7 +567,7 @@ Private method to set the auto-completion hook. @param editor reference to the editor - @type QScintilla.Editor + @type QScintilla.Editor.Editor """ try: editor.addCompletionListHook("rope", self.requestCompletions, @@ -516,7 +583,7 @@ Private method to unset the auto-completion hook. @param editor reference to the editor - @type QScintilla.Editor + @type QScintilla.Editor.Editor """ editor.removeCompletionListHook("rope") @@ -525,7 +592,7 @@ Private method to set the calltip hook. @param editor reference to the editor - @type QScintilla.Editor + @type QScintilla.Editor.Editor """ editor.addCallTipHook("rope", self.getCallTips) @@ -534,7 +601,7 @@ Private method to unset the calltip hook. @param editor reference to the editor - @type QScintilla.Editor + @type QScintilla.Editor.Editor """ editor.removeCallTipHook("rope")