Wed, 27 Sep 2017 18:42:35 +0200
Implemented the distributed code assist calltips method.
RefactoringRope/CodeAssistClient.py | file | annotate | diff | comparison | revisions | |
RefactoringRope/CodeAssistServer.py | file | annotate | diff | comparison | revisions |
--- a/RefactoringRope/CodeAssistClient.py Wed Sep 27 18:25:08 2017 +0200 +++ b/RefactoringRope/CodeAssistClient.py Wed Sep 27 18:42:35 2017 +0200 @@ -59,6 +59,7 @@ self.__methodMapping = { "getCompletions": self.__getCompletions, + "getCallTips": self.__getCallTips, } self.__projectpath = projectPath @@ -135,6 +136,43 @@ result.update(errorDict) self.sendJson("CompletionsResult", result) + + def __getCallTips(self, params): + """ + Private method to calculate possible completions. + + @param params dictionary containing the method parameters + @type dict + """ + filename = params["FileName"] + source = params["Source"] + offset = params["Offset"] + maxfixes = params["MaxFixes"] + + if filename: + resource = rope.base.libutils.path_to_resource( + self.__project, filename) + else: + resource = None + + errorDict = {} + calltips = [] + + try: + cts = rope.contrib.codeassist.get_calltip( + self.__project, source, offset, resource, maxfixes=maxfixes, + remove_self=True) + if cts is not None: + calltips = [cts] + except Exception as err: + errorDict = self.__handleRopeError(err) + + result = { + "CallTips": calltips, + } + result.update(errorDict) + + self.sendJson("CallTipsResult", result) if __name__ == '__main__':
--- a/RefactoringRope/CodeAssistServer.py Wed Sep 27 18:25:08 2017 +0200 +++ b/RefactoringRope/CodeAssistServer.py Wed Sep 27 18:42:35 2017 +0200 @@ -50,6 +50,7 @@ self.__methodMapping = { "CompletionsResult": self.__processCompletionsResult, + "CallTipsResult": self.__processCallTipsResult, } # Python 2 @@ -140,39 +141,58 @@ else: self.__completions = result["Completions"] - # TODO: port this to the distributed variant def getCallTips(self, pos, editor): """ Public method to calculate calltips. - @param pos position in the text for the calltip (integer) + @param pos position in the text for the calltip + @type int @param editor reference to the editor object, that called this method - QScintilla.Editor) - @return list of possible calltips (list of strings) + @type QScintilla.Editor + @return list of possible calltips + @rtype list of str """ - print("rope: getCallTips") - return [] -## filename = editor.getFileName() -## if filename: -## resource = rope.base.libutils.path_to_resource( -## self.__project, filename) -## else: -## resource = None -## source = editor.text() -## maxfixes = self.__plugin.getPreferences("CalltipsMaxFixes") -## try: -## line, index = editor.lineIndexFromPosition(pos) -## offset = len("".join(source.splitlines(True)[:line])) + index -## cts = rope.contrib.codeassist.get_calltip( -## self.__project, source, offset, resource, maxfixes=maxfixes, -## remove_self=True) -## if cts is not None: -## cts = [cts] -## else: -## cts = [] -## except Exception: -## cts = [] -## return cts + # reset the calltips buffer + self.__calltips = None + + language = editor.getLanguage() + if language not in self.__editorLanguageMapping: + return [] + idString = self.__editorLanguageMapping[language] + + filename = editor.getFileName() + source = editor.text() + line, index = editor.lineIndexFromPosition(pos) + offset = len("".join(source.splitlines(True)[:line])) + index + maxfixes = self.__plugin.getPreferences("CalltipsMaxFixes") + + self.sendJson("getCallTips", { + "FileName": filename, + "Source": source, + "Offset": offset, + "MaxFixes": maxfixes, + }, idString=idString) + + # emulate the synchronous behaviour + timer = QTimer() + timer.setSingleShot(True) + timer.start(5000) # 5s timeout + while self.__calltips is None and timer.isActive(): + QCoreApplication.processEvents() + + return [] if self.__calltips is None else self.__calltips + + def __processCallTipsResult(self, result): + """ + Private method to process the calltips sent by the client. + + @param result dictionary containg the result sent by the client + @type dict + """ + if "Error" in result: + self.__calltips = [] + else: + self.__calltips = result["CallTips"] # TODO: port this to the distributed variant def reportChanged(self, filename, oldSource):