28 |
28 |
29 class CodeAssistClient(JsonClient): |
29 class CodeAssistClient(JsonClient): |
30 """ |
30 """ |
31 Class implementing the code assist client interface to rope. |
31 Class implementing the code assist client interface to rope. |
32 """ |
32 """ |
|
33 PictureIDs = { |
|
34 "class": "?{0}".format(1), # Editor.ClassID |
|
35 "instance": "?{0}".format(1), # Editor.ClassID |
|
36 "function": "?{0}".format(4), # Editor.MethodID |
|
37 "module": "?{0}".format(7), # Editor.AttributeID |
|
38 "None": "", |
|
39 } |
|
40 # The various ID values are a copy of the ones found in the Editor |
|
41 # class in order to make this module/script independent from an |
|
42 # installed eric |
|
43 |
33 def __init__(self, host, port, idString, projectPath): |
44 def __init__(self, host, port, idString, projectPath): |
34 """ |
45 """ |
35 Constructor |
46 Constructor |
36 |
47 |
37 @param host ip address the background service is listening |
48 @param host ip address the background service is listening |
45 @type str |
56 @type str |
46 """ |
57 """ |
47 super(CodeAssistClient, self).__init__(host, port, idString) |
58 super(CodeAssistClient, self).__init__(host, port, idString) |
48 |
59 |
49 self.__methodMapping = { |
60 self.__methodMapping = { |
|
61 "getCompletions": self.__getCompletions, |
50 } |
62 } |
51 |
63 |
52 self.__projectpath = projectPath |
64 self.__projectpath = projectPath |
53 self.__project = rope.base.project.Project(self.__projectpath) |
65 self.__project = rope.base.project.Project(self.__projectpath) |
54 self.__project.validate(self.__project.root) |
66 self.__project.validate(self.__project.root) |
61 @type str |
73 @type str |
62 @param params dictionary with method specific parameters |
74 @param params dictionary with method specific parameters |
63 @type dict |
75 @type dict |
64 """ |
76 """ |
65 self.__methodMapping[method](params) |
77 self.__methodMapping[method](params) |
|
78 |
|
79 def __handleRopeError(self, err): |
|
80 """ |
|
81 Private method to process a rope error. |
|
82 |
|
83 @param err rope exception object |
|
84 @type Exception |
|
85 @return dictionary containing the error information |
|
86 @rtype dict |
|
87 """ |
|
88 ropeError = str(type(err)).split()[-1] |
|
89 ropeError = ropeError[1:-2].split('.')[-1] |
|
90 errorDict = { |
|
91 "Error": ropeError, |
|
92 "ErrorString": str(err), |
|
93 } |
|
94 if ropeError == 'ModuleSyntaxError': |
|
95 errorDict["ErrorFile"] = err.filename |
|
96 errorDict["ErrorLine"] = err.lineno |
|
97 |
|
98 return errorDict |
|
99 |
|
100 def __getCompletions(self, params): |
|
101 """ |
|
102 Private method to calculate possible completions. |
|
103 |
|
104 @param params dictionary containing the method parameters |
|
105 @type dict |
|
106 """ |
|
107 filename = params["FileName"] |
|
108 source = params["Source"] |
|
109 offset = params["Offset"] |
|
110 maxfixes = params["MaxFixes"] |
|
111 |
|
112 if filename: |
|
113 resource = rope.base.libutils.path_to_resource( |
|
114 self.__project, filename) |
|
115 else: |
|
116 resource = None |
|
117 |
|
118 errorDict = {} |
|
119 completions = [] |
|
120 |
|
121 try: |
|
122 proposals = rope.contrib.codeassist.code_assist( |
|
123 self.__project, source, offset, resource, maxfixes=maxfixes) |
|
124 proposals = rope.contrib.codeassist.sorted_proposals(proposals) |
|
125 # TODO: extend this to include PictureIDs for protected and |
|
126 # private stuff (i.e. names starting with _ or __) |
|
127 completions = [proposal.name + self.PictureIDs[proposal.type] |
|
128 for proposal in proposals] |
|
129 except Exception as err: |
|
130 errorDict = self.__handleRopeError(err) |
|
131 |
|
132 result = { |
|
133 "Completions": completions, |
|
134 } |
|
135 result.update(errorDict) |
|
136 |
|
137 self.sendJson("CompletionsResult", result) |
66 |
138 |
67 |
139 |
68 if __name__ == '__main__': |
140 if __name__ == '__main__': |
69 if len(sys.argv) != 5: |
141 if len(sys.argv) != 5: |
70 print('Host, port, id and project path parameters are missing. Abort.') |
142 print('Host, port, id and project path parameters are missing. Abort.') |