--- a/RefactoringRope/CodeAssistClient.py Tue Sep 26 19:05:18 2017 +0200 +++ b/RefactoringRope/CodeAssistClient.py Wed Sep 27 18:25:08 2017 +0200 @@ -30,6 +30,17 @@ """ Class implementing the code assist client interface to rope. """ + PictureIDs = { + "class": "?{0}".format(1), # Editor.ClassID + "instance": "?{0}".format(1), # Editor.ClassID + "function": "?{0}".format(4), # Editor.MethodID + "module": "?{0}".format(7), # Editor.AttributeID + "None": "", + } + # The various ID values are a copy of the ones found in the Editor + # class in order to make this module/script independent from an + # installed eric + def __init__(self, host, port, idString, projectPath): """ Constructor @@ -47,6 +58,7 @@ super(CodeAssistClient, self).__init__(host, port, idString) self.__methodMapping = { + "getCompletions": self.__getCompletions, } self.__projectpath = projectPath @@ -63,6 +75,66 @@ @type dict """ self.__methodMapping[method](params) + + def __handleRopeError(self, err): + """ + Private method to process a rope error. + + @param err rope exception object + @type Exception + @return dictionary containing the error information + @rtype dict + """ + ropeError = str(type(err)).split()[-1] + ropeError = ropeError[1:-2].split('.')[-1] + errorDict = { + "Error": ropeError, + "ErrorString": str(err), + } + if ropeError == 'ModuleSyntaxError': + errorDict["ErrorFile"] = err.filename + errorDict["ErrorLine"] = err.lineno + + return errorDict + + def __getCompletions(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 = {} + completions = [] + + try: + proposals = rope.contrib.codeassist.code_assist( + self.__project, source, offset, resource, maxfixes=maxfixes) + proposals = rope.contrib.codeassist.sorted_proposals(proposals) + # TODO: extend this to include PictureIDs for protected and + # private stuff (i.e. names starting with _ or __) + completions = [proposal.name + self.PictureIDs[proposal.type] + for proposal in proposals] + except Exception as err: + errorDict = self.__handleRopeError(err) + + result = { + "Completions": completions, + } + result.update(errorDict) + + self.sendJson("CompletionsResult", result) if __name__ == '__main__':