RefactoringRope/RefactoringClient.py

branch
eric7
changeset 389
4f53795beff0
parent 385
9bbd74b51d88
child 409
65153bf17e8d
equal deleted inserted replaced
388:cb044ec27c24 389:4f53795beff0
9 9
10 import sys 10 import sys
11 import os 11 import os
12 import contextlib 12 import contextlib
13 13
14 modulePath = sys.argv[-1] # it is always the last parameter 14 modulePath = sys.argv[-1] # it is always the last parameter
15 sys.path.append(modulePath) 15 sys.path.append(modulePath)
16 16
17 try: 17 try:
18 import rope.base.project 18 import rope.base.project
19 import rope.base.libutils 19 import rope.base.libutils
27 27
28 class RefactoringClient(EricJsonClient): 28 class RefactoringClient(EricJsonClient):
29 """ 29 """
30 Class implementing the refactoring client interface to rope. 30 Class implementing the refactoring client interface to rope.
31 """ 31 """
32
32 def __init__(self, host, port, projectPath): 33 def __init__(self, host, port, projectPath):
33 """ 34 """
34 Constructor 35 Constructor
35 36
36 @param host ip address the background service is listening 37 @param host ip address the background service is listening
37 @type str 38 @type str
38 @param port port of the background service 39 @param port port of the background service
39 @type int 40 @type int
40 @param projectPath path to the project 41 @param projectPath path to the project
41 @type str 42 @type str
42 """ 43 """
43 super().__init__(host, port) 44 super().__init__(host, port)
44 45
45 self.__methodMapping = { 46 self.__methodMapping = {
46 "AbortAction": self.__abortAction, 47 "AbortAction": self.__abortAction,
47 "CloseProject": self.__closeProject, 48 "CloseProject": self.__closeProject,
48 "Validate": self.__validate, 49 "Validate": self.__validate,
49 "QueryReferences": self.__queryReferences, 50 "QueryReferences": self.__queryReferences,
56 "History": self.__processHistory, 57 "History": self.__processHistory,
57 "PreviewChanges": self.__previewChanges, 58 "PreviewChanges": self.__previewChanges,
58 "ApplyChanges": self.__applyChanges, 59 "ApplyChanges": self.__applyChanges,
59 "ClearChanges": self.__clearChanges, 60 "ClearChanges": self.__clearChanges,
60 "CalculateRenameChanges": self.__calculateRenameChanges, 61 "CalculateRenameChanges": self.__calculateRenameChanges,
61 "CalculateChangeOccurrencesChanges": 62 "CalculateChangeOccurrencesChanges": (
62 self.__calculateChangeOccurrencesChanges, 63 self.__calculateChangeOccurrencesChanges
64 ),
63 "CalculateExtractChanges": self.__calculateExtractChanges, 65 "CalculateExtractChanges": self.__calculateExtractChanges,
64 "RequestInlineType": self.__requestInlineType, 66 "RequestInlineType": self.__requestInlineType,
65 "CalculateInlineChanges": self.__calculateInlineChanges, 67 "CalculateInlineChanges": self.__calculateInlineChanges,
66 "RequestMoveType": self.__requestMoveType, 68 "RequestMoveType": self.__requestMoveType,
67 "CalculateMoveChanges": self.__calculateMoveChanges, 69 "CalculateMoveChanges": self.__calculateMoveChanges,
68 "RequestUseFunction": self.__requestUseFunction, 70 "RequestUseFunction": self.__requestUseFunction,
69 "CalculateUseFunctionChanges": self.__calculateUseFunctionChanges, 71 "CalculateUseFunctionChanges": self.__calculateUseFunctionChanges,
70 "CalculateIntroduceFactoryChanges": 72 "CalculateIntroduceFactoryChanges": self.__calculateIntroduceFactoryChanges,
71 self.__calculateIntroduceFactoryChanges, 73 "CalculateIntroduceParameterChanges": (
72 "CalculateIntroduceParameterChanges": 74 self.__calculateIntroduceParameterChanges
73 self.__calculateIntroduceParameterChanges, 75 ),
74 "CalculateImportsChanges": self.__calculateImportsChanges, 76 "CalculateImportsChanges": self.__calculateImportsChanges,
75 "CalculateRestructureChanges": self.__calculateRestructureChanges, 77 "CalculateRestructureChanges": self.__calculateRestructureChanges,
76 "RequestSignature": self.__requestSignature, 78 "RequestSignature": self.__requestSignature,
77 "CalculateSignatureChanges": self.__calculateSignatureChanges, 79 "CalculateSignatureChanges": self.__calculateSignatureChanges,
78 "CalculateInlineArgumentDefaultChanges": 80 "CalculateInlineArgumentDefaultChanges": (
79 self.__calculateInlineArgumentDefaultChanges, 81 self.__calculateInlineArgumentDefaultChanges
80 "CalculateModuleToPackageChanges": 82 ),
81 self.__calculateModuleToPackageChanges, 83 "CalculateModuleToPackageChanges": self.__calculateModuleToPackageChanges,
82 "RequestFieldName": self.__requestFieldName, 84 "RequestFieldName": self.__requestFieldName,
83 "CalculateEncapsulateFieldChanges": 85 "CalculateEncapsulateFieldChanges": self.__calculateEncapsulateFieldChanges,
84 self.__calculateEncapsulateFieldChanges, 86 "CalculateLocalToAttributeChanges": self.__calculateLocalToAttributeChanges,
85 "CalculateLocalToAttributeChanges": 87 "CalculateMethodObjectChanges": self.__calculateMethodObjectChanges,
86 self.__calculateLocalToAttributeChanges, 88 }
87 "CalculateMethodObjectChanges": 89
88 self.__calculateMethodObjectChanges,
89 }
90
91 from FileSystemCommands import RefactoringClientFileSystemCommands 90 from FileSystemCommands import RefactoringClientFileSystemCommands
91
92 self.__fsCommands = RefactoringClientFileSystemCommands(self) 92 self.__fsCommands = RefactoringClientFileSystemCommands(self)
93 93
94 self.__projectpath = projectPath 94 self.__projectpath = projectPath
95 self.__project = rope.base.project.Project( 95 self.__project = rope.base.project.Project(
96 self.__projectpath, fscommands=self.__fsCommands) 96 self.__projectpath, fscommands=self.__fsCommands
97 97 )
98
98 self.__progressHandle = None 99 self.__progressHandle = None
99 100
100 self.__changes = {} 101 self.__changes = {}
101 # dict storing the retrieved changes for various refactorings 102 # dict storing the retrieved changes for various refactorings
102 103
103 def handleCall(self, method, params): 104 def handleCall(self, method, params):
104 """ 105 """
105 Public method to handle a method call from the server. 106 Public method to handle a method call from the server.
106 107
107 @param method requested method name 108 @param method requested method name
108 @type str 109 @type str
109 @param params dictionary with method specific parameters 110 @param params dictionary with method specific parameters
110 @type dict 111 @type dict
111 """ 112 """
112 self.__methodMapping[method](params) 113 self.__methodMapping[method](params)
113 114
114 def __handleRopeError(self, err): 115 def __handleRopeError(self, err):
115 """ 116 """
116 Private method to process a rope error. 117 Private method to process a rope error.
117 118
118 @param err rope exception object 119 @param err rope exception object
119 @type Exception 120 @type Exception
120 @return dictionary containing the error information 121 @return dictionary containing the error information
121 @rtype dict 122 @rtype dict
122 """ 123 """
123 import traceback 124 import traceback
125
124 tb = traceback.format_tb(sys.exc_info()[2]) 126 tb = traceback.format_tb(sys.exc_info()[2])
125 ropeError = str(type(err)).split()[-1] 127 ropeError = str(type(err)).split()[-1]
126 ropeError = ropeError[1:-2].split('.')[-1] 128 ropeError = ropeError[1:-2].split(".")[-1]
127 errorDict = { 129 errorDict = {
128 "Error": ropeError, 130 "Error": ropeError,
129 "ErrorString": str(err), 131 "ErrorString": str(err),
130 "Traceback": tb, 132 "Traceback": tb,
131 } 133 }
132 if ropeError == 'ModuleSyntaxError': 134 if ropeError == "ModuleSyntaxError":
133 errorDict["ErrorFile"] = err.filename 135 errorDict["ErrorFile"] = err.filename
134 errorDict["ErrorLine"] = err.lineno 136 errorDict["ErrorLine"] = err.lineno
135 137
136 return errorDict 138 return errorDict
137 139
138 def __abortAction(self, params): 140 def __abortAction(self, params):
139 """ 141 """
140 Private method to abort the current action. 142 Private method to abort the current action.
141 143
142 @param params dictionary containing the method parameters sent by 144 @param params dictionary containing the method parameters sent by
143 the server 145 the server
144 @type dict 146 @type dict
145 """ 147 """
146 if ( 148 if self.__progressHandle is not None and not self.__progressHandle.is_stopped():
147 self.__progressHandle is not None and
148 not self.__progressHandle.is_stopped()
149 ):
150 self.__progressHandle.stop() 149 self.__progressHandle.stop()
151 150
152 def __validate(self, params): 151 def __validate(self, params):
153 """ 152 """
154 Private slot to validate the project. 153 Private slot to validate the project.
155 154
156 @param params dictionary containing the method parameters sent by 155 @param params dictionary containing the method parameters sent by
157 the server 156 the server
158 @type dict 157 @type dict
159 """ 158 """
160 self.__project.validate(self.__project.root) 159 self.__project.validate(self.__project.root)
161 160
162 def __closeProject(self, params): 161 def __closeProject(self, params):
163 """ 162 """
164 Private slot to validate the project. 163 Private slot to validate the project.
165 164
166 @param params dictionary containing the method parameters sent by 165 @param params dictionary containing the method parameters sent by
167 the server 166 the server
168 @type dict 167 @type dict
169 """ 168 """
170 self.__project.close() 169 self.__project.close()
171 170
172 def __getConfig(self, params): 171 def __getConfig(self, params):
173 """ 172 """
174 Private method to send some configuration data to the server. 173 Private method to send some configuration data to the server.
175 174
176 @param params dictionary containing the method parameters sent by 175 @param params dictionary containing the method parameters sent by
177 the server 176 the server
178 @type dict 177 @type dict
179 """ 178 """
180 result = { 179 result = {
181 "RopeFolderName": self.__project.ropefolder.real_path, 180 "RopeFolderName": self.__project.ropefolder.real_path,
182 "RopeHelpFile": os.path.join( 181 "RopeHelpFile": os.path.join(
183 os.path.dirname(__file__), "Documentation", "rope", 182 os.path.dirname(__file__), "Documentation", "rope", "overview.rst"
184 "overview.rst"), 183 ),
185 "RopeInfo": rope.INFO, 184 "RopeInfo": rope.INFO,
186 "RopeVersion": rope.VERSION, 185 "RopeVersion": rope.VERSION,
187 "RopeCopyright": rope.COPYRIGHT, 186 "RopeCopyright": rope.COPYRIGHT,
188 "PythonVersion": "Python{0}".format(sys.version_info[0]), 187 "PythonVersion": "Python{0}".format(sys.version_info[0]),
189 } 188 }
195 # read our own copy of default_config derived from the default 194 # read our own copy of default_config derived from the default
196 # settings in rope.base.prefs.Prefs 195 # settings in rope.base.prefs.Prefs
197 with open( 196 with open(
198 os.path.join(os.path.dirname(__file__), "default_config.py"), 197 os.path.join(os.path.dirname(__file__), "default_config.py"),
199 "r", 198 "r",
200 encoding="utf-8" 199 encoding="utf-8",
201 ) as f: 200 ) as f:
202 result["DefaultConfig"] = f.read() 201 result["DefaultConfig"] = f.read()
203 202
204 self.sendJson("Config", result) 203 self.sendJson("Config", result)
205 204
206 def __configChanged(self, params): 205 def __configChanged(self, params):
207 """ 206 """
208 Private method to handle a change of the configuration file. 207 Private method to handle a change of the configuration file.
209 208
210 @param params dictionary containing the method parameters sent by 209 @param params dictionary containing the method parameters sent by
211 the server 210 the server
212 @type dict 211 @type dict
213 """ 212 """
214 self.__project.close() 213 self.__project.close()
215 self.__project = rope.base.project.Project( 214 self.__project = rope.base.project.Project(
216 self.__projectpath, fscommands=self.__fsCommands) 215 self.__projectpath, fscommands=self.__fsCommands
217 216 )
217
218 def __queryReferences(self, params): 218 def __queryReferences(self, params):
219 """ 219 """
220 Private method to handle the Find References action. 220 Private method to handle the Find References action.
221 221
222 @param params dictionary containing the method parameters sent by 222 @param params dictionary containing the method parameters sent by
223 the server 223 the server
224 @type dict 224 @type dict
225 """ 225 """
226 title = params["Title"] 226 title = params["Title"]
227 filename = params["FileName"] 227 filename = params["FileName"]
228 offset = params["Offset"] 228 offset = params["Offset"]
229 229
230 errorDict = {} 230 errorDict = {}
231 occurrences = [] 231 occurrences = []
232 232
233 import rope.contrib.findit 233 import rope.contrib.findit
234 resource = rope.base.libutils.path_to_resource( 234
235 self.__project, filename) 235 resource = rope.base.libutils.path_to_resource(self.__project, filename)
236 self.__progressHandle = ProgressHandle(self, title, True) 236 self.__progressHandle = ProgressHandle(self, title, True)
237 try: 237 try:
238 occurrences = rope.contrib.findit.find_occurrences( 238 occurrences = rope.contrib.findit.find_occurrences(
239 self.__project, resource, offset, 239 self.__project,
240 unsure=True, in_hierarchy=True, 240 resource,
241 task_handle=self.__progressHandle) 241 offset,
242 unsure=True,
243 in_hierarchy=True,
244 task_handle=self.__progressHandle,
245 )
242 except Exception as err: 246 except Exception as err:
243 errorDict = self.__handleRopeError(err) 247 errorDict = self.__handleRopeError(err)
244 self.__progressHandle.reset() 248 self.__progressHandle.reset()
245 self.__progressHandle = None 249 self.__progressHandle = None
246 250
247 result = { 251 result = {
248 "Title": title, 252 "Title": title,
249 "EntriesCount": len(occurrences), 253 "EntriesCount": len(occurrences),
250 "Entries": [ 254 "Entries": [
251 [occurrence.resource.real_path, occurrence.lineno, 255 [occurrence.resource.real_path, occurrence.lineno, occurrence.unsure]
252 occurrence.unsure] for occurrence in occurrences 256 for occurrence in occurrences
253 ], 257 ],
254 } 258 }
255 result.update(errorDict) 259 result.update(errorDict)
256 260
257 self.sendJson("QueryReferencesResult", result) 261 self.sendJson("QueryReferencesResult", result)
258 262
259 def __queryDefinition(self, params): 263 def __queryDefinition(self, params):
260 """ 264 """
261 Private method to handle the Find Definition action. 265 Private method to handle the Find Definition action.
262 266
263 @param params dictionary containing the method parameters sent by 267 @param params dictionary containing the method parameters sent by
264 the server 268 the server
265 @type dict 269 @type dict
266 """ 270 """
267 title = params["Title"] 271 title = params["Title"]
268 filename = params["FileName"] 272 filename = params["FileName"]
269 offset = params["Offset"] 273 offset = params["Offset"]
270 source = params["Source"] 274 source = params["Source"]
271 275
272 errorDict = {} 276 errorDict = {}
273 location = None 277 location = None
274 278
275 import rope.contrib.findit 279 import rope.contrib.findit
276 resource = rope.base.libutils.path_to_resource( 280
277 self.__project, filename) 281 resource = rope.base.libutils.path_to_resource(self.__project, filename)
278 try: 282 try:
279 location = rope.contrib.findit.find_definition( 283 location = rope.contrib.findit.find_definition(
280 self.__project, source, offset, resource) 284 self.__project, source, offset, resource
281 except Exception as err: 285 )
282 errorDict = self.__handleRopeError(err) 286 except Exception as err:
283 287 errorDict = self.__handleRopeError(err)
288
284 result = { 289 result = {
285 "Title": title, 290 "Title": title,
286 } 291 }
287 if location is not None: 292 if location is not None:
288 result["Location"] = [ 293 result["Location"] = [location.resource.real_path, location.lineno]
289 location.resource.real_path, location.lineno 294 result.update(errorDict)
290 ] 295
291 result.update(errorDict)
292
293 self.sendJson("QueryDefinitionResult", result) 296 self.sendJson("QueryDefinitionResult", result)
294 297
295 def __queryImplementations(self, params): 298 def __queryImplementations(self, params):
296 """ 299 """
297 Private method to handle the Find Implementations action. 300 Private method to handle the Find Implementations action.
298 301
299 @param params dictionary containing the method parameters sent by 302 @param params dictionary containing the method parameters sent by
300 the server 303 the server
301 @type dict 304 @type dict
302 """ 305 """
303 title = params["Title"] 306 title = params["Title"]
304 filename = params["FileName"] 307 filename = params["FileName"]
305 offset = params["Offset"] 308 offset = params["Offset"]
306 309
307 errorDict = {} 310 errorDict = {}
308 occurrences = [] 311 occurrences = []
309 312
310 import rope.contrib.findit 313 import rope.contrib.findit
311 resource = rope.base.libutils.path_to_resource( 314
312 self.__project, filename) 315 resource = rope.base.libutils.path_to_resource(self.__project, filename)
313 self.__progressHandle = ProgressHandle(self, title, True) 316 self.__progressHandle = ProgressHandle(self, title, True)
314 try: 317 try:
315 occurrences = rope.contrib.findit.find_implementations( 318 occurrences = rope.contrib.findit.find_implementations(
316 self.__project, resource, offset, 319 self.__project, resource, offset, task_handle=self.__progressHandle
317 task_handle=self.__progressHandle) 320 )
318 except Exception as err: 321 except Exception as err:
319 errorDict = self.__handleRopeError(err) 322 errorDict = self.__handleRopeError(err)
320 self.__progressHandle.reset() 323 self.__progressHandle.reset()
321 self.__progressHandle = None 324 self.__progressHandle = None
322 325
323 result = { 326 result = {
324 "Title": title, 327 "Title": title,
325 "EntriesCount": len(occurrences), 328 "EntriesCount": len(occurrences),
326 "Entries": [ 329 "Entries": [
327 [occurrence.resource.real_path, occurrence.lineno, 330 [occurrence.resource.real_path, occurrence.lineno, occurrence.unsure]
328 occurrence.unsure] for occurrence in occurrences 331 for occurrence in occurrences
329 ], 332 ],
330 } 333 }
331 result.update(errorDict) 334 result.update(errorDict)
332 335
333 self.sendJson("QueryImplementationsResult", result) 336 self.sendJson("QueryImplementationsResult", result)
334 337
335 def __performSOA(self, params): 338 def __performSOA(self, params):
336 """ 339 """
337 Private method to perform SOA on all modules. 340 Private method to perform SOA on all modules.
338 341
339 @param params dictionary containing the method parameters sent by 342 @param params dictionary containing the method parameters sent by
340 the server 343 the server
341 @type dict 344 @type dict
342 """ 345 """
343 title = params["Title"] 346 title = params["Title"]
344 347
345 errorDict = {} 348 errorDict = {}
346 349
347 self.__progressHandle = ProgressHandle(self, title, True) 350 self.__progressHandle = ProgressHandle(self, title, True)
348 try: 351 try:
349 rope.base.libutils.analyze_modules( 352 rope.base.libutils.analyze_modules(
350 self.__project, task_handle=self.__progressHandle) 353 self.__project, task_handle=self.__progressHandle
354 )
351 except Exception as err: 355 except Exception as err:
352 errorDict = self.__handleRopeError(err) 356 errorDict = self.__handleRopeError(err)
353 self.__progressHandle.reset() 357 self.__progressHandle.reset()
354 self.__progressHandle = None 358 self.__progressHandle = None
355 359
356 result = { 360 result = {
357 "Title": title, 361 "Title": title,
358 } 362 }
359 result.update(errorDict) 363 result.update(errorDict)
360 364
361 self.sendJson("SoaFinished", result) 365 self.sendJson("SoaFinished", result)
362 366
363 def __reportChanged(self, params): 367 def __reportChanged(self, params):
364 """ 368 """
365 Private method to register some changed sources. 369 Private method to register some changed sources.
366 370
367 @param params dictionary containing the method parameters sent by 371 @param params dictionary containing the method parameters sent by
368 the server 372 the server
369 @type dict 373 @type dict
370 """ 374 """
371 filename = params["FileName"] 375 filename = params["FileName"]
372 oldSource = params["OldSource"] 376 oldSource = params["OldSource"]
373 377
374 with contextlib.suppress(Exception): 378 with contextlib.suppress(Exception):
375 rope.base.libutils.report_change( 379 rope.base.libutils.report_change(self.__project, filename, oldSource)
376 self.__project, filename, oldSource) 380
377
378 def __processHistory(self, params): 381 def __processHistory(self, params):
379 """ 382 """
380 Private method to process the various history related requests. 383 Private method to process the various history related requests.
381 384
382 @param params dictionary containing the method parameters sent by 385 @param params dictionary containing the method parameters sent by
383 the server 386 the server
384 @type dict 387 @type dict
385 """ 388 """
386 subcommand = params["Subcommand"] 389 subcommand = params["Subcommand"]
387 if subcommand == "Get": 390 if subcommand == "Get":
388 changes = {} 391 changes = {}
389 if params["Filename"]: 392 if params["Filename"]:
390 # file history 393 # file history
391 resource = rope.base.libutils.path_to_resource( 394 resource = rope.base.libutils.path_to_resource(
392 self.__project, params["Filename"]) 395 self.__project, params["Filename"]
396 )
393 undoList = [] 397 undoList = []
394 for change in reversed(self.__project.history.undo_list): 398 for change in reversed(self.__project.history.undo_list):
395 if resource in change.get_changed_resources(): 399 if resource in change.get_changed_resources():
396 undoList.append(change) 400 undoList.append(change)
397 redoList = [] 401 redoList = []
400 redoList.append(change) 404 redoList.append(change)
401 else: 405 else:
402 # project history 406 # project history
403 undoList = list(reversed(self.__project.history.undo_list)) 407 undoList = list(reversed(self.__project.history.undo_list))
404 redoList = self.__project.history.redo_list 408 redoList = self.__project.history.redo_list
405 409
406 result = {"Subcommand": "Histories"} 410 result = {
407 result["Undo"] = [] 411 "Subcommand": "Histories",
412 "Undo": [],
413 }
408 for change in undoList: 414 for change in undoList:
409 changes[id(change)] = change 415 changes[id(change)] = change
410 result["Undo"].append([str(change), id(change)]) 416 result["Undo"].append([str(change), id(change)])
411 result["Redo"] = [] 417 result["Redo"] = []
412 for change in redoList: 418 for change in redoList:
413 changes[id(change)] = change 419 changes[id(change)] = change
414 result["Redo"].append([str(change), id(change)]) 420 result["Redo"].append([str(change), id(change)])
415 421
416 self.__changes["History"] = changes 422 self.__changes["History"] = changes
417 423
418 self.sendJson("HistoryResult", result) 424 self.sendJson("HistoryResult", result)
419 425
420 elif subcommand == "GetChange": 426 elif subcommand == "GetChange":
421 result = { 427 result = {
422 "Subcommand": "ChangeDescription", 428 "Subcommand": "ChangeDescription",
423 "Description": 429 "Description": self.__changes["History"][
424 self.__changes["History"][params["Id"]].get_description() 430 params["Id"]
431 ].get_description(),
425 } 432 }
426 433
427 self.sendJson("HistoryResult", result) 434 self.sendJson("HistoryResult", result)
428 435
429 elif subcommand in ["Undo", "Redo"]: 436 elif subcommand in ["Undo", "Redo"]:
430 change = self.__changes["History"][params["Id"]] 437 change = self.__changes["History"][params["Id"]]
431 self.__progressHandle = ProgressHandle(self, change.description, 438 self.__progressHandle = ProgressHandle(self, change.description, False)
432 False)
433 if subcommand == "Undo": 439 if subcommand == "Undo":
434 self.__project.history.undo( 440 self.__project.history.undo(change, task_handle=self.__progressHandle)
435 change, task_handle=self.__progressHandle)
436 else: 441 else:
437 self.__project.history.redo( 442 self.__project.history.redo(change, task_handle=self.__progressHandle)
438 change, task_handle=self.__progressHandle)
439 self.__progressHandle.reset() 443 self.__progressHandle.reset()
440 self.__progressHandle = None 444 self.__progressHandle = None
441 445
442 result = { 446 result = {
443 "Subcommand": subcommand, 447 "Subcommand": subcommand,
444 "ChangedFiles": [ 448 "ChangedFiles": [
445 res.real_path for res in change.get_changed_resources() 449 res.real_path for res in change.get_changed_resources()
446 ], 450 ],
447 } 451 }
448 452
449 self.sendJson("HistoryResult", result) 453 self.sendJson("HistoryResult", result)
450 454
451 elif subcommand == "Clear": 455 elif subcommand == "Clear":
452 self.__project.history.clear() 456 self.__project.history.clear()
453 457
454 elif subcommand == "ClearChanges": 458 elif subcommand == "ClearChanges":
455 with contextlib.suppress(KeyError): 459 with contextlib.suppress(KeyError):
456 del self.__changes["History"] 460 del self.__changes["History"]
457 461
458 def __clearChanges(self, params): 462 def __clearChanges(self, params):
459 """ 463 """
460 Private method to clear the changes cache of a given change group. 464 Private method to clear the changes cache of a given change group.
461 465
462 @param params dictionary containing the method parameters sent by 466 @param params dictionary containing the method parameters sent by
463 the server 467 the server
464 @type dict 468 @type dict
465 """ 469 """
466 with contextlib.suppress(KeyError): 470 with contextlib.suppress(KeyError):
467 del self.__changes[params["ChangeGroup"]] 471 del self.__changes[params["ChangeGroup"]]
468 472
469 def __applyChanges(self, params): 473 def __applyChanges(self, params):
470 """ 474 """
471 Private method to apply the changes of a given change group. 475 Private method to apply the changes of a given change group.
472 476
473 @param params dictionary containing the method parameters sent by 477 @param params dictionary containing the method parameters sent by
474 the server 478 the server
475 @type dict 479 @type dict
476 """ 480 """
477 errorDict = {} 481 errorDict = {}
478 482
479 self.__progressHandle = ProgressHandle(self, params["Title"], False) 483 self.__progressHandle = ProgressHandle(self, params["Title"], False)
480 try: 484 try:
481 changes = self.__changes[params["ChangeGroup"]] 485 changes = self.__changes[params["ChangeGroup"]]
482 if changes is not None: 486 if changes is not None:
483 self.__project.do(changes, self.__progressHandle) 487 self.__project.do(changes, self.__progressHandle)
484 except Exception as err: 488 except Exception as err:
485 errorDict = self.__handleRopeError(err) 489 errorDict = self.__handleRopeError(err)
486 self.__progressHandle.reset() 490 self.__progressHandle.reset()
487 self.__progressHandle = None 491 self.__progressHandle = None
488 492
489 result = { 493 result = {
490 "Subcommand": "ChangesApplied", 494 "Subcommand": "ChangesApplied",
491 "ChangeGroup": params["ChangeGroup"], 495 "ChangeGroup": params["ChangeGroup"],
492 "Title": params["Title"], 496 "Title": params["Title"],
493 "ChangedFiles": [ 497 "ChangedFiles": [res.real_path for res in changes.get_changed_resources()],
494 res.real_path for res in changes.get_changed_resources() 498 }
495 ], 499 result.update(errorDict)
496 } 500
497 result.update(errorDict) 501 self.sendJson("Changes", result)
498 502
499 self.sendJson("Changes", result)
500
501 def __previewChanges(self, params): 503 def __previewChanges(self, params):
502 """ 504 """
503 Private method to determine the changes data for a preview. 505 Private method to determine the changes data for a preview.
504 506
505 @param params dictionary containing the method parameters sent by 507 @param params dictionary containing the method parameters sent by
506 the server 508 the server
507 @type dict 509 @type dict
508 """ 510 """
509 try: 511 try:
510 changes = self.__changes[params["ChangeGroup"]] 512 changes = self.__changes[params["ChangeGroup"]]
511 description = changes.description 513 description = changes.description
512 except KeyError: 514 except KeyError:
513 changes = None 515 changes = None
514 description = "" 516 description = ""
515 517
516 changesData = [] 518 changesData = []
517 if changes is not None: 519 if changes is not None:
518 for change in changes.changes: 520 for change in changes.changes:
519 changeTitle = str(change) 521 changeTitle = str(change)
520 try: 522 try:
521 changeText = change.get_description() 523 changeText = change.get_description()
522 except AttributeError: 524 except AttributeError:
523 changeText = None 525 changeText = None
524 changesData.append([changeTitle, changeText]) 526 changesData.append([changeTitle, changeText])
525 527
526 result = { 528 result = {
527 "Subcommand": "PreviewChanges", 529 "Subcommand": "PreviewChanges",
528 "ChangeGroup": params["ChangeGroup"], 530 "ChangeGroup": params["ChangeGroup"],
529 "Description": description, 531 "Description": description,
530 "Changes": changesData, 532 "Changes": changesData,
531 } 533 }
532 534
533 self.sendJson("Changes", result) 535 self.sendJson("Changes", result)
534 536
535 def __calculateRenameChanges(self, params): 537 def __calculateRenameChanges(self, params):
536 """ 538 """
537 Private method to calculate the rename changes based on the parameters 539 Private method to calculate the rename changes based on the parameters
538 sent by the server. 540 sent by the server.
539 541
540 @param params dictionary containing the method parameters sent by 542 @param params dictionary containing the method parameters sent by
541 the server 543 the server
542 @type dict 544 @type dict
543 """ 545 """
544 changeGroup = params["ChangeGroup"] 546 changeGroup = params["ChangeGroup"]
547 offset = params["Offset"] 549 offset = params["Offset"]
548 isLocal = params["LocalRename"] 550 isLocal = params["LocalRename"]
549 newName = params["NewName"] 551 newName = params["NewName"]
550 renameHierarchy = params["RenameHierarchy"] 552 renameHierarchy = params["RenameHierarchy"]
551 renameInStrings = params["RenameInStrings"] 553 renameInStrings = params["RenameInStrings"]
552 554
553 errorDict = {} 555 errorDict = {}
554 changes = None 556 changes = None
555 result = { 557 result = {
556 "ChangeGroup": changeGroup, 558 "ChangeGroup": changeGroup,
557 "Title": title, 559 "Title": title,
558 } 560 }
559 561
560 import rope.refactor.rename 562 import rope.refactor.rename
561 resource = rope.base.libutils.path_to_resource( 563
562 self.__project, filename) 564 resource = rope.base.libutils.path_to_resource(self.__project, filename)
563 565
564 resources = [resource] if isLocal else None 566 resources = [resource] if isLocal else None
565 567
566 self.__progressHandle = ProgressHandle(self, title, True) 568 self.__progressHandle = ProgressHandle(self, title, True)
567 try: 569 try:
568 renamer = rope.refactor.rename.Rename( 570 renamer = rope.refactor.rename.Rename(self.__project, resource, offset)
569 self.__project, resource, offset)
570 changes = renamer.get_changes( 571 changes = renamer.get_changes(
571 newName, 572 newName,
572 resources=resources, 573 resources=resources,
573 in_hierarchy=renameHierarchy, 574 in_hierarchy=renameHierarchy,
574 unsure=lambda o: self.__confirmUnsure(o, changeGroup), 575 unsure=lambda o: self.__confirmUnsure(o, changeGroup),
575 docs=renameInStrings, 576 docs=renameInStrings,
576 task_handle=self.__progressHandle) 577 task_handle=self.__progressHandle,
578 )
577 except Exception as err: 579 except Exception as err:
578 errorDict = self.__handleRopeError(err) 580 errorDict = self.__handleRopeError(err)
579 self.__progressHandle.reset() 581 self.__progressHandle.reset()
580 self.__progressHandle = None 582 self.__progressHandle = None
581 583
582 self.__changes[changeGroup] = changes 584 self.__changes[changeGroup] = changes
583 585
584 result["Subcommand"] = "ChangesCalculated" 586 result["Subcommand"] = "ChangesCalculated"
585 result.update(errorDict) 587 result.update(errorDict)
586 588
587 self.sendJson("Changes", result) 589 self.sendJson("Changes", result)
588 590
589 def __confirmUnsure(self, occurrence, changeGroup): 591 def __confirmUnsure(self, occurrence, changeGroup):
590 """ 592 """
591 Private method to confirm unsure occurrences. 593 Private method to confirm unsure occurrences.
592 594
593 @param occurrence reference to the occurrence object 595 @param occurrence reference to the occurrence object
594 @type rope.refactor.occurrences.Occurrence 596 @type rope.refactor.occurrences.Occurrence
595 @param changeGroup name of the change group 597 @param changeGroup name of the change group
596 @type str 598 @type str
597 @return flag indicating an occurrence 599 @return flag indicating an occurrence
598 @rtype bool 600 @rtype bool
599 """ 601 """
600 filename = occurrence.resource.real_path 602 filename = occurrence.resource.real_path
601 start, end = occurrence.get_primary_range() 603 start, end = occurrence.get_primary_range()
602 604
603 self.sendJson("Changes", { 605 self.sendJson(
604 "Subcommand": "ConfirmUnsure", 606 "Changes",
605 "ChangeGroup": changeGroup, 607 {
606 "FileName": filename, 608 "Subcommand": "ConfirmUnsure",
607 "StartOffset": start, 609 "ChangeGroup": changeGroup,
608 "EndOffset": end, 610 "FileName": filename,
609 }) 611 "StartOffset": start,
610 612 "EndOffset": end,
613 },
614 )
615
611 answer = self.poll(waitMethod="ConfirmUnsure") 616 answer = self.poll(waitMethod="ConfirmUnsure")
612 617
613 return answer["Answer"] 618 return answer["Answer"]
614 619
615 def __calculateChangeOccurrencesChanges(self, params): 620 def __calculateChangeOccurrencesChanges(self, params):
616 """ 621 """
617 Private method to calculate the 'Change Occurrences' changes based on 622 Private method to calculate the 'Change Occurrences' changes based on
618 the parameters sent by the server. 623 the parameters sent by the server.
619 624
620 @param params dictionary containing the method parameters sent by 625 @param params dictionary containing the method parameters sent by
621 the server 626 the server
622 @type dict 627 @type dict
623 """ 628 """
624 changeGroup = params["ChangeGroup"] 629 changeGroup = params["ChangeGroup"]
627 offset = params["Offset"] 632 offset = params["Offset"]
628 newName = params["NewName"] 633 newName = params["NewName"]
629 onlyCalls = params["OnlyCalls"] 634 onlyCalls = params["OnlyCalls"]
630 reads = params["Reads"] 635 reads = params["Reads"]
631 writes = params["Writes"] 636 writes = params["Writes"]
632 637
633 errorDict = {} 638 errorDict = {}
634 changes = None 639 changes = None
635 result = { 640 result = {
636 "ChangeGroup": changeGroup, 641 "ChangeGroup": changeGroup,
637 "Title": title, 642 "Title": title,
638 } 643 }
639 644
640 import rope.refactor.rename 645 import rope.refactor.rename
641 resource = rope.base.libutils.path_to_resource( 646
642 self.__project, filename) 647 resource = rope.base.libutils.path_to_resource(self.__project, filename)
643 try: 648 try:
644 renamer = rope.refactor.rename.ChangeOccurrences( 649 renamer = rope.refactor.rename.ChangeOccurrences(
645 self.__project, resource, offset) 650 self.__project, resource, offset
651 )
646 changes = renamer.get_changes( 652 changes = renamer.get_changes(
647 newName, only_calls=onlyCalls, reads=reads, writes=writes) 653 newName, only_calls=onlyCalls, reads=reads, writes=writes
648 except Exception as err: 654 )
649 errorDict = self.__handleRopeError(err) 655 except Exception as err:
650 656 errorDict = self.__handleRopeError(err)
657
651 self.__changes[changeGroup] = changes 658 self.__changes[changeGroup] = changes
652 659
653 result["Subcommand"] = "ChangesCalculated" 660 result["Subcommand"] = "ChangesCalculated"
654 result.update(errorDict) 661 result.update(errorDict)
655 662
656 self.sendJson("Changes", result) 663 self.sendJson("Changes", result)
657 664
658 def __calculateExtractChanges(self, params): 665 def __calculateExtractChanges(self, params):
659 """ 666 """
660 Private method to calculate the 'Extract' changes based on the 667 Private method to calculate the 'Extract' changes based on the
661 parameters sent by the server. 668 parameters sent by the server.
662 669
663 @param params dictionary containing the method parameters sent by 670 @param params dictionary containing the method parameters sent by
664 the server 671 the server
665 @type dict 672 @type dict
666 @exception Exception raised to indicate an invalid extraction type 673 @exception Exception raised to indicate an invalid extraction type
667 """ 674 """
672 endOffset = params["EndOffset"] 679 endOffset = params["EndOffset"]
673 kind = params["Kind"] 680 kind = params["Kind"]
674 newName = params["NewName"] 681 newName = params["NewName"]
675 similar = params["Similar"] 682 similar = params["Similar"]
676 global_ = params["Global"] 683 global_ = params["Global"]
677 684
678 errorDict = {} 685 errorDict = {}
679 changes = None 686 changes = None
680 result = { 687 result = {
681 "ChangeGroup": changeGroup, 688 "ChangeGroup": changeGroup,
682 "Title": title, 689 "Title": title,
683 } 690 }
684 691
685 import rope.refactor.extract 692 import rope.refactor.extract
686 resource = rope.base.libutils.path_to_resource( 693
687 self.__project, filename) 694 resource = rope.base.libutils.path_to_resource(self.__project, filename)
688 try: 695 try:
689 if kind not in ("variable", "method"): 696 if kind not in ("variable", "method"):
690 raise Exception("Invalid extraction kind <{0}>.".format(kind)) 697 raise Exception("Invalid extraction kind <{0}>.".format(kind))
691 elif kind == "variable": 698 elif kind == "variable":
692 extractor = rope.refactor.extract.ExtractVariable( 699 extractor = rope.refactor.extract.ExtractVariable(
693 self.__project, resource, startOffset, endOffset) 700 self.__project, resource, startOffset, endOffset
701 )
694 elif kind == "method": 702 elif kind == "method":
695 extractor = rope.refactor.extract.ExtractMethod( 703 extractor = rope.refactor.extract.ExtractMethod(
696 self.__project, resource, startOffset, endOffset) 704 self.__project, resource, startOffset, endOffset
697 changes = extractor.get_changes( 705 )
698 newName, similar=similar, global_=global_) 706 changes = extractor.get_changes(newName, similar=similar, global_=global_)
699 except Exception as err: 707 except Exception as err:
700 errorDict = self.__handleRopeError(err) 708 errorDict = self.__handleRopeError(err)
701 709
702 self.__changes[changeGroup] = changes 710 self.__changes[changeGroup] = changes
703 711
704 result["Subcommand"] = "ChangesCalculated" 712 result["Subcommand"] = "ChangesCalculated"
705 result.update(errorDict) 713 result.update(errorDict)
706 714
707 self.sendJson("Changes", result) 715 self.sendJson("Changes", result)
708 716
709 def __requestInlineType(self, params): 717 def __requestInlineType(self, params):
710 """ 718 """
711 Private method to determine the 'Inline' changes type based on the 719 Private method to determine the 'Inline' changes type based on the
712 parameters sent by the server. 720 parameters sent by the server.
713 721
714 @param params dictionary containing the method parameters sent by 722 @param params dictionary containing the method parameters sent by
715 the server 723 the server
716 @type dict 724 @type dict
717 """ 725 """
718 changeGroup = params["ChangeGroup"] 726 changeGroup = params["ChangeGroup"]
719 title = params["Title"] 727 title = params["Title"]
720 filename = params["FileName"] 728 filename = params["FileName"]
721 offset = params["Offset"] 729 offset = params["Offset"]
722 730
723 errorDict = {} 731 errorDict = {}
724 result = { 732 result = {
725 "Subcommand": "InlineType", 733 "Subcommand": "InlineType",
726 "ChangeGroup": changeGroup, 734 "ChangeGroup": changeGroup,
727 "Title": title, 735 "Title": title,
728 } 736 }
729 737
730 import rope.refactor.inline 738 import rope.refactor.inline
731 resource = rope.base.libutils.path_to_resource( 739
732 self.__project, filename) 740 resource = rope.base.libutils.path_to_resource(self.__project, filename)
733 try: 741 try:
734 inliner = rope.refactor.inline.create_inline( 742 inliner = rope.refactor.inline.create_inline(
735 self.__project, resource, offset) 743 self.__project, resource, offset
736 result.update({ 744 )
737 "Name": inliner.name, 745 result.update(
738 "Kind": inliner.get_kind(), 746 {
739 }) 747 "Name": inliner.name,
740 except Exception as err: 748 "Kind": inliner.get_kind(),
741 errorDict = self.__handleRopeError(err) 749 }
742 750 )
743 result.update(errorDict) 751 except Exception as err:
744 752 errorDict = self.__handleRopeError(err)
745 self.sendJson("Changes", result) 753
746 754 result.update(errorDict)
755
756 self.sendJson("Changes", result)
757
747 def __calculateInlineChanges(self, params): 758 def __calculateInlineChanges(self, params):
748 """ 759 """
749 Private method to calculate the 'Inline' changes based on the 760 Private method to calculate the 'Inline' changes based on the
750 parameters sent by the server. 761 parameters sent by the server.
751 762
752 @param params dictionary containing the method parameters sent by 763 @param params dictionary containing the method parameters sent by
753 the server 764 the server
754 @type dict 765 @type dict
755 """ 766 """
756 changeGroup = params["ChangeGroup"] 767 changeGroup = params["ChangeGroup"]
757 title = params["Title"] 768 title = params["Title"]
758 filename = params["FileName"] 769 filename = params["FileName"]
759 offset = params["Offset"] 770 offset = params["Offset"]
760 kind = params["Kind"] 771 kind = params["Kind"]
761 772
762 errorDict = {} 773 errorDict = {}
763 changes = None 774 changes = None
764 result = { 775 result = {
765 "ChangeGroup": changeGroup, 776 "ChangeGroup": changeGroup,
766 "Title": title, 777 "Title": title,
767 } 778 }
768 779
769 import rope.refactor.extract 780 import rope.refactor.extract
770 resource = rope.base.libutils.path_to_resource( 781
771 self.__project, filename) 782 resource = rope.base.libutils.path_to_resource(self.__project, filename)
772 self.__progressHandle = ProgressHandle(self, title, True) 783 self.__progressHandle = ProgressHandle(self, title, True)
773 try: 784 try:
774 inliner = rope.refactor.inline.create_inline( 785 inliner = rope.refactor.inline.create_inline(
775 self.__project, resource, offset) 786 self.__project, resource, offset
787 )
776 opts = ( 788 opts = (
777 {"in_hierarchy": params["Hierarchy"], } 789 {
778 if kind == "parameter" else 790 "in_hierarchy": params["Hierarchy"],
779 {"remove": params["Remove"], 791 }
780 "only_current": params["OnlyCurrent"], } 792 if kind == "parameter"
781 ) 793 else {
782 changes = inliner.get_changes( 794 "remove": params["Remove"],
783 task_handle=self.__progressHandle, **opts) 795 "only_current": params["OnlyCurrent"],
796 }
797 )
798 changes = inliner.get_changes(task_handle=self.__progressHandle, **opts)
784 except Exception as err: 799 except Exception as err:
785 errorDict = self.__handleRopeError(err) 800 errorDict = self.__handleRopeError(err)
786 self.__progressHandle.reset() 801 self.__progressHandle.reset()
787 self.__progressHandle = None 802 self.__progressHandle = None
788 803
789 self.__changes[changeGroup] = changes 804 self.__changes[changeGroup] = changes
790 805
791 result["Subcommand"] = "ChangesCalculated" 806 result["Subcommand"] = "ChangesCalculated"
792 result.update(errorDict) 807 result.update(errorDict)
793 808
794 self.sendJson("Changes", result) 809 self.sendJson("Changes", result)
795 810
796 def __requestMoveType(self, params): 811 def __requestMoveType(self, params):
797 """ 812 """
798 Private method to determine the 'Move Method' changes type based on the 813 Private method to determine the 'Move Method' changes type based on the
799 parameters sent by the server. 814 parameters sent by the server.
800 815
801 @param params dictionary containing the method parameters sent by 816 @param params dictionary containing the method parameters sent by
802 the server 817 the server
803 @type dict 818 @type dict
804 """ 819 """
805 changeGroup = params["ChangeGroup"] 820 changeGroup = params["ChangeGroup"]
806 title = params["Title"] 821 title = params["Title"]
807 filename = params["FileName"] 822 filename = params["FileName"]
808 offset = params["Offset"] 823 offset = params["Offset"]
809 824
810 errorDict = {} 825 errorDict = {}
811 result = { 826 result = {
812 "Subcommand": "MoveType", 827 "Subcommand": "MoveType",
813 "ChangeGroup": changeGroup, 828 "ChangeGroup": changeGroup,
814 "Title": title, 829 "Title": title,
815 } 830 }
816 831
817 import rope.refactor.move 832 import rope.refactor.move
818 resource = rope.base.libutils.path_to_resource( 833
819 self.__project, filename) 834 resource = rope.base.libutils.path_to_resource(self.__project, filename)
820 try: 835 try:
821 mover = rope.refactor.move.create_move( 836 mover = rope.refactor.move.create_move(self.__project, resource, offset)
822 self.__project, resource, offset)
823 if isinstance(mover, rope.refactor.move.MoveGlobal): 837 if isinstance(mover, rope.refactor.move.MoveGlobal):
824 result.update({ 838 result.update(
825 "Kind": "move_global_method", 839 {
826 "Method": "", 840 "Kind": "move_global_method",
827 }) 841 "Method": "",
842 }
843 )
828 else: 844 else:
829 result.update({ 845 result.update(
830 "Kind": "move_method", 846 {
831 "Method": mover.get_method_name(), 847 "Kind": "move_method",
832 }) 848 "Method": mover.get_method_name(),
833 except Exception as err: 849 }
834 errorDict = self.__handleRopeError(err) 850 )
835 851 except Exception as err:
836 result.update(errorDict) 852 errorDict = self.__handleRopeError(err)
837 853
838 self.sendJson("Changes", result) 854 result.update(errorDict)
839 855
856 self.sendJson("Changes", result)
857
840 def __calculateMoveChanges(self, params): 858 def __calculateMoveChanges(self, params):
841 """ 859 """
842 Private method to calculate the 'Move ...' changes based on the 860 Private method to calculate the 'Move ...' changes based on the
843 parameters sent by the server. 861 parameters sent by the server.
844 862
845 @param params dictionary containing the method parameters sent by 863 @param params dictionary containing the method parameters sent by
846 the server 864 the server
847 @type dict 865 @type dict
848 """ 866 """
849 changeGroup = params["ChangeGroup"] 867 changeGroup = params["ChangeGroup"]
852 offset = params["Offset"] 870 offset = params["Offset"]
853 kind = params["Kind"] 871 kind = params["Kind"]
854 newName = params["NewName"] 872 newName = params["NewName"]
855 attribute = params["Attribute"] 873 attribute = params["Attribute"]
856 destination = params["DestinationModule"] 874 destination = params["DestinationModule"]
857 875
858 errorDict = {} 876 errorDict = {}
859 changes = None 877 changes = None
860 result = { 878 result = {
861 "ChangeGroup": changeGroup, 879 "ChangeGroup": changeGroup,
862 "Title": title, 880 "Title": title,
863 } 881 }
864 882
865 import rope.refactor.move 883 import rope.refactor.move
866 resource = rope.base.libutils.path_to_resource( 884
867 self.__project, filename) 885 resource = rope.base.libutils.path_to_resource(self.__project, filename)
868 self.__progressHandle = ProgressHandle(self, title, True) 886 self.__progressHandle = ProgressHandle(self, title, True)
869 try: 887 try:
870 mover = rope.refactor.move.create_move( 888 mover = rope.refactor.move.create_move(self.__project, resource, offset)
871 self.__project, resource, offset)
872 if kind == "move_method": 889 if kind == "move_method":
873 changes = mover.get_changes( 890 changes = mover.get_changes(
874 attribute, newName, task_handle=self.__progressHandle) 891 attribute, newName, task_handle=self.__progressHandle
892 )
875 else: 893 else:
876 if kind == "move_global_method": 894 if kind == "move_global_method":
877 dest = self.__project.get_pycore().find_module( 895 dest = self.__project.get_pycore().find_module(
878 os.path.splitext(destination)[0]) 896 os.path.splitext(destination)[0]
897 )
879 else: 898 else:
880 # move_module 899 # move_module
881 if destination.endswith(os.sep): 900 if destination.endswith(os.sep):
882 destination = destination[:-1] 901 destination = destination[:-1]
883 dest = self.__project.get_pycore().find_module( 902 dest = self.__project.get_pycore().find_module(destination)
884 destination) 903 changes = mover.get_changes(dest, task_handle=self.__progressHandle)
885 changes = mover.get_changes(
886 dest, task_handle=self.__progressHandle)
887 except Exception as err: 904 except Exception as err:
888 errorDict = self.__handleRopeError(err) 905 errorDict = self.__handleRopeError(err)
889 self.__progressHandle.reset() 906 self.__progressHandle.reset()
890 self.__progressHandle = None 907 self.__progressHandle = None
891 908
892 self.__changes[changeGroup] = changes 909 self.__changes[changeGroup] = changes
893 910
894 result["Subcommand"] = "ChangesCalculated" 911 result["Subcommand"] = "ChangesCalculated"
895 result.update(errorDict) 912 result.update(errorDict)
896 913
897 self.sendJson("Changes", result) 914 self.sendJson("Changes", result)
898 915
899 def __requestUseFunction(self, params): 916 def __requestUseFunction(self, params):
900 """ 917 """
901 Private method to determine the 'Use Function' function name based on 918 Private method to determine the 'Use Function' function name based on
902 the parameters sent by the server. 919 the parameters sent by the server.
903 920
904 @param params dictionary containing the method parameters sent by 921 @param params dictionary containing the method parameters sent by
905 the server 922 the server
906 @type dict 923 @type dict
907 """ 924 """
908 changeGroup = params["ChangeGroup"] 925 changeGroup = params["ChangeGroup"]
909 title = params["Title"] 926 title = params["Title"]
910 filename = params["FileName"] 927 filename = params["FileName"]
911 offset = params["Offset"] 928 offset = params["Offset"]
912 929
913 errorDict = {} 930 errorDict = {}
914 result = { 931 result = {
915 "Subcommand": "UseFunctionName", 932 "Subcommand": "UseFunctionName",
916 "ChangeGroup": changeGroup, 933 "ChangeGroup": changeGroup,
917 "Title": title, 934 "Title": title,
918 } 935 }
919 936
920 import rope.refactor.usefunction 937 import rope.refactor.usefunction
921 resource = rope.base.libutils.path_to_resource( 938
922 self.__project, filename) 939 resource = rope.base.libutils.path_to_resource(self.__project, filename)
923 try: 940 try:
924 user = rope.refactor.usefunction.UseFunction( 941 user = rope.refactor.usefunction.UseFunction(
925 self.__project, resource, offset) 942 self.__project, resource, offset
943 )
926 result["FunctionName"] = user.get_function_name() 944 result["FunctionName"] = user.get_function_name()
927 except Exception as err: 945 except Exception as err:
928 errorDict = self.__handleRopeError(err) 946 errorDict = self.__handleRopeError(err)
929 947
930 result.update(errorDict) 948 result.update(errorDict)
931 949
932 self.sendJson("Changes", result) 950 self.sendJson("Changes", result)
933 951
934 def __calculateUseFunctionChanges(self, params): 952 def __calculateUseFunctionChanges(self, params):
935 """ 953 """
936 Private method to calculate the 'Use Function' changes based on the 954 Private method to calculate the 'Use Function' changes based on the
937 parameters sent by the server. 955 parameters sent by the server.
938 956
939 @param params dictionary containing the method parameters sent by 957 @param params dictionary containing the method parameters sent by
940 the server 958 the server
941 @type dict 959 @type dict
942 """ 960 """
943 changeGroup = params["ChangeGroup"] 961 changeGroup = params["ChangeGroup"]
944 title = params["Title"] 962 title = params["Title"]
945 filename = params["FileName"] 963 filename = params["FileName"]
946 offset = params["Offset"] 964 offset = params["Offset"]
947 965
948 errorDict = {} 966 errorDict = {}
949 changes = None 967 changes = None
950 result = { 968 result = {
951 "ChangeGroup": changeGroup, 969 "ChangeGroup": changeGroup,
952 "Title": title, 970 "Title": title,
953 } 971 }
954 972
955 import rope.refactor.usefunction 973 import rope.refactor.usefunction
956 resource = rope.base.libutils.path_to_resource( 974
957 self.__project, filename) 975 resource = rope.base.libutils.path_to_resource(self.__project, filename)
958 self.__progressHandle = ProgressHandle(self, title, True) 976 self.__progressHandle = ProgressHandle(self, title, True)
959 try: 977 try:
960 user = rope.refactor.usefunction.UseFunction( 978 user = rope.refactor.usefunction.UseFunction(
961 self.__project, resource, offset) 979 self.__project, resource, offset
980 )
962 changes = user.get_changes(task_handle=self.__progressHandle) 981 changes = user.get_changes(task_handle=self.__progressHandle)
963 except Exception as err: 982 except Exception as err:
964 errorDict = self.__handleRopeError(err) 983 errorDict = self.__handleRopeError(err)
965 self.__progressHandle.reset() 984 self.__progressHandle.reset()
966 self.__progressHandle = None 985 self.__progressHandle = None
967 986
968 self.__changes[changeGroup] = changes 987 self.__changes[changeGroup] = changes
969 988
970 result["Subcommand"] = "ChangesCalculated" 989 result["Subcommand"] = "ChangesCalculated"
971 result.update(errorDict) 990 result.update(errorDict)
972 991
973 self.sendJson("Changes", result) 992 self.sendJson("Changes", result)
974 993
975 def __calculateIntroduceFactoryChanges(self, params): 994 def __calculateIntroduceFactoryChanges(self, params):
976 """ 995 """
977 Private method to calculate the 'Introduce Factory' changes based on 996 Private method to calculate the 'Introduce Factory' changes based on
978 the parameters sent by the server. 997 the parameters sent by the server.
979 998
980 @param params dictionary containing the method parameters sent by 999 @param params dictionary containing the method parameters sent by
981 the server 1000 the server
982 @type dict 1001 @type dict
983 """ 1002 """
984 changeGroup = params["ChangeGroup"] 1003 changeGroup = params["ChangeGroup"]
985 title = params["Title"] 1004 title = params["Title"]
986 filename = params["FileName"] 1005 filename = params["FileName"]
987 offset = params["Offset"] 1006 offset = params["Offset"]
988 factoryName = params["Name"] 1007 factoryName = params["Name"]
989 globalFactory = params["GlobalFactory"] 1008 globalFactory = params["GlobalFactory"]
990 1009
991 errorDict = {} 1010 errorDict = {}
992 changes = None 1011 changes = None
993 result = { 1012 result = {
994 "ChangeGroup": changeGroup, 1013 "ChangeGroup": changeGroup,
995 "Title": title, 1014 "Title": title,
996 } 1015 }
997 1016
998 import rope.refactor.introduce_factory 1017 import rope.refactor.introduce_factory
999 resource = rope.base.libutils.path_to_resource( 1018
1000 self.__project, filename) 1019 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1001 self.__progressHandle = ProgressHandle(self, title, True) 1020 self.__progressHandle = ProgressHandle(self, title, True)
1002 try: 1021 try:
1003 introducer = ( 1022 introducer = rope.refactor.introduce_factory.IntroduceFactoryRefactoring(
1004 rope.refactor.introduce_factory.IntroduceFactoryRefactoring( 1023 self.__project, resource, offset
1005 self.__project, resource, offset)
1006 ) 1024 )
1007 changes = introducer.get_changes( 1025 changes = introducer.get_changes(
1008 factoryName, global_factory=globalFactory, 1026 factoryName,
1009 task_handle=self.__progressHandle) 1027 global_factory=globalFactory,
1028 task_handle=self.__progressHandle,
1029 )
1010 except Exception as err: 1030 except Exception as err:
1011 errorDict = self.__handleRopeError(err) 1031 errorDict = self.__handleRopeError(err)
1012 self.__progressHandle.reset() 1032 self.__progressHandle.reset()
1013 self.__progressHandle = None 1033 self.__progressHandle = None
1014 1034
1015 self.__changes[changeGroup] = changes 1035 self.__changes[changeGroup] = changes
1016 1036
1017 result["Subcommand"] = "ChangesCalculated" 1037 result["Subcommand"] = "ChangesCalculated"
1018 result.update(errorDict) 1038 result.update(errorDict)
1019 1039
1020 self.sendJson("Changes", result) 1040 self.sendJson("Changes", result)
1021 1041
1022 def __calculateIntroduceParameterChanges(self, params): 1042 def __calculateIntroduceParameterChanges(self, params):
1023 """ 1043 """
1024 Private method to calculate the 'Introduce Parameter' changes based on 1044 Private method to calculate the 'Introduce Parameter' changes based on
1025 the parameters sent by the server. 1045 the parameters sent by the server.
1026 1046
1027 @param params dictionary containing the method parameters sent by 1047 @param params dictionary containing the method parameters sent by
1028 the server 1048 the server
1029 @type dict 1049 @type dict
1030 """ 1050 """
1031 changeGroup = params["ChangeGroup"] 1051 changeGroup = params["ChangeGroup"]
1032 title = params["Title"] 1052 title = params["Title"]
1033 filename = params["FileName"] 1053 filename = params["FileName"]
1034 offset = params["Offset"] 1054 offset = params["Offset"]
1035 parameterName = params["Name"] 1055 parameterName = params["Name"]
1036 1056
1037 errorDict = {} 1057 errorDict = {}
1038 changes = None 1058 changes = None
1039 result = { 1059 result = {
1040 "ChangeGroup": changeGroup, 1060 "ChangeGroup": changeGroup,
1041 "Title": title, 1061 "Title": title,
1042 } 1062 }
1043 1063
1044 import rope.refactor.introduce_parameter 1064 import rope.refactor.introduce_parameter
1045 resource = rope.base.libutils.path_to_resource( 1065
1046 self.__project, filename) 1066 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1047 try: 1067 try:
1048 introducer = rope.refactor.introduce_parameter.IntroduceParameter( 1068 introducer = rope.refactor.introduce_parameter.IntroduceParameter(
1049 self.__project, resource, offset) 1069 self.__project, resource, offset
1070 )
1050 changes = introducer.get_changes(parameterName) 1071 changes = introducer.get_changes(parameterName)
1051 except Exception as err: 1072 except Exception as err:
1052 errorDict = self.__handleRopeError(err) 1073 errorDict = self.__handleRopeError(err)
1053 1074
1054 self.__changes[changeGroup] = changes 1075 self.__changes[changeGroup] = changes
1055 1076
1056 result["Subcommand"] = "ChangesCalculated" 1077 result["Subcommand"] = "ChangesCalculated"
1057 result.update(errorDict) 1078 result.update(errorDict)
1058 1079
1059 self.sendJson("Changes", result) 1080 self.sendJson("Changes", result)
1060 1081
1061 def __calculateImportsChanges(self, params): 1082 def __calculateImportsChanges(self, params):
1062 """ 1083 """
1063 Private method to calculate the 'Import' changes based on the 1084 Private method to calculate the 'Import' changes based on the
1064 parameters sent by the server. 1085 parameters sent by the server.
1065 1086
1066 @param params dictionary containing the method parameters sent by 1087 @param params dictionary containing the method parameters sent by
1067 the server 1088 the server
1068 @type dict 1089 @type dict
1069 """ 1090 """
1070 changeGroup = params["ChangeGroup"] 1091 changeGroup = params["ChangeGroup"]
1071 title = params["Title"] 1092 title = params["Title"]
1072 filename = params["FileName"] 1093 filename = params["FileName"]
1073 offset = params["Offset"] 1094 offset = params["Offset"]
1074 methodName = params["MethodName"] 1095 methodName = params["MethodName"]
1075 1096
1076 errorDict = {} 1097 errorDict = {}
1077 changes = None 1098 changes = None
1078 result = { 1099 result = {
1079 "ChangeGroup": changeGroup, 1100 "ChangeGroup": changeGroup,
1080 "Title": title, 1101 "Title": title,
1081 } 1102 }
1082 1103
1083 from rope.refactor.importutils import ImportOrganizer 1104 from rope.refactor.importutils import ImportOrganizer
1105
1084 method = { 1106 method = {
1085 "organize_imports": ImportOrganizer.organize_imports, 1107 "organize_imports": ImportOrganizer.organize_imports,
1086 "expand_star_imports": ImportOrganizer.expand_star_imports, 1108 "expand_star_imports": ImportOrganizer.expand_star_imports,
1087 "relatives_to_absolutes": ImportOrganizer.relatives_to_absolutes, 1109 "relatives_to_absolutes": ImportOrganizer.relatives_to_absolutes,
1088 "froms_to_imports": ImportOrganizer.froms_to_imports, 1110 "froms_to_imports": ImportOrganizer.froms_to_imports,
1089 "handle_long_imports": ImportOrganizer.handle_long_imports, 1111 "handle_long_imports": ImportOrganizer.handle_long_imports,
1090 }[methodName] 1112 }[methodName]
1091 importOrganizer = ImportOrganizer(self.__project) 1113 importOrganizer = ImportOrganizer(self.__project)
1092 resource = rope.base.libutils.path_to_resource( 1114 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1093 self.__project, filename)
1094 try: 1115 try:
1095 changes = method(importOrganizer, resource, offset=offset) 1116 changes = method(importOrganizer, resource, offset=offset)
1096 except Exception as err: 1117 except Exception as err:
1097 errorDict = self.__handleRopeError(err) 1118 errorDict = self.__handleRopeError(err)
1098 1119
1099 self.__changes[changeGroup] = changes 1120 self.__changes[changeGroup] = changes
1100 1121
1101 # send the change description first 1122 # send the change description first
1102 description = changes.description if changes else "" 1123 description = changes.description if changes else ""
1103 self.sendJson("Changes", { 1124 self.sendJson(
1104 "ChangeGroup": changeGroup, 1125 "Changes",
1105 "Title": title, 1126 {
1106 "Subcommand": "ChangeDescription", 1127 "ChangeGroup": changeGroup,
1107 "Description": description, 1128 "Title": title,
1108 }) 1129 "Subcommand": "ChangeDescription",
1109 1130 "Description": description,
1131 },
1132 )
1133
1110 result["Subcommand"] = "ChangesCalculated" 1134 result["Subcommand"] = "ChangesCalculated"
1111 result.update(errorDict) 1135 result.update(errorDict)
1112 1136
1113 self.sendJson("Changes", result) 1137 self.sendJson("Changes", result)
1114 1138
1115 def __calculateRestructureChanges(self, params): 1139 def __calculateRestructureChanges(self, params):
1116 """ 1140 """
1117 Private method to calculate the 'Restructure' changes based on the 1141 Private method to calculate the 'Restructure' changes based on the
1118 parameters sent by the server. 1142 parameters sent by the server.
1119 1143
1120 @param params dictionary containing the method parameters sent by 1144 @param params dictionary containing the method parameters sent by
1121 the server 1145 the server
1122 @type dict 1146 @type dict
1123 """ 1147 """
1124 changeGroup = params["ChangeGroup"] 1148 changeGroup = params["ChangeGroup"]
1125 title = params["Title"] 1149 title = params["Title"]
1126 pattern = params["Pattern"] 1150 pattern = params["Pattern"]
1127 goal = params["Goal"] 1151 goal = params["Goal"]
1128 args = params["Args"] 1152 args = params["Args"]
1129 imports = params["Imports"] 1153 imports = params["Imports"]
1130 1154
1131 errorDict = {} 1155 errorDict = {}
1132 changes = None 1156 changes = None
1133 result = { 1157 result = {
1134 "ChangeGroup": changeGroup, 1158 "ChangeGroup": changeGroup,
1135 "Title": title, 1159 "Title": title,
1136 } 1160 }
1137 1161
1138 import rope.refactor.restructure 1162 import rope.refactor.restructure
1163
1139 self.__project.validate(self.__project.root) 1164 self.__project.validate(self.__project.root)
1140 self.__progressHandle = ProgressHandle(self, title, True) 1165 self.__progressHandle = ProgressHandle(self, title, True)
1141 try: 1166 try:
1142 restructuring = rope.refactor.restructure.Restructure( 1167 restructuring = rope.refactor.restructure.Restructure(
1143 self.__project, pattern, goal, args=args, imports=imports) 1168 self.__project, pattern, goal, args=args, imports=imports
1144 changes = restructuring.get_changes( 1169 )
1145 task_handle=self.__progressHandle) 1170 changes = restructuring.get_changes(task_handle=self.__progressHandle)
1146 except Exception as err: 1171 except Exception as err:
1147 errorDict = self.__handleRopeError(err) 1172 errorDict = self.__handleRopeError(err)
1148 self.__progressHandle.reset() 1173 self.__progressHandle.reset()
1149 self.__progressHandle = None 1174 self.__progressHandle = None
1150 1175
1151 self.__changes[changeGroup] = changes 1176 self.__changes[changeGroup] = changes
1152 1177
1153 result["Subcommand"] = "ChangesCalculated" 1178 result["Subcommand"] = "ChangesCalculated"
1154 result.update(errorDict) 1179 result.update(errorDict)
1155 1180
1156 self.sendJson("Changes", result) 1181 self.sendJson("Changes", result)
1157 1182
1158 def __requestSignature(self, params): 1183 def __requestSignature(self, params):
1159 """ 1184 """
1160 Private method to calculate the 'Signature' based on the parameters 1185 Private method to calculate the 'Signature' based on the parameters
1161 sent by the server. 1186 sent by the server.
1162 1187
1163 @param params dictionary containing the method parameters sent by 1188 @param params dictionary containing the method parameters sent by
1164 the server 1189 the server
1165 @type dict 1190 @type dict
1166 """ 1191 """
1167 changeGroup = params["ChangeGroup"] 1192 changeGroup = params["ChangeGroup"]
1168 title = params["Title"] 1193 title = params["Title"]
1169 filename = params["FileName"] 1194 filename = params["FileName"]
1170 offset = params["Offset"] 1195 offset = params["Offset"]
1171 1196
1172 errorDict = {} 1197 errorDict = {}
1173 result = { 1198 result = {
1174 "Subcommand": "Signature", 1199 "Subcommand": "Signature",
1175 "ChangeGroup": changeGroup, 1200 "ChangeGroup": changeGroup,
1176 "Title": title, 1201 "Title": title,
1177 } 1202 }
1178 1203
1179 import rope.refactor.change_signature 1204 import rope.refactor.change_signature
1180 resource = rope.base.libutils.path_to_resource( 1205
1181 self.__project, filename) 1206 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1182 try: 1207 try:
1183 changer = rope.refactor.change_signature.ChangeSignature( 1208 changer = rope.refactor.change_signature.ChangeSignature(
1184 self.__project, resource, offset) 1209 self.__project, resource, offset
1210 )
1185 result["DefinitionInfo"] = changer.get_args() 1211 result["DefinitionInfo"] = changer.get_args()
1186 except Exception as err: 1212 except Exception as err:
1187 errorDict = self.__handleRopeError(err) 1213 errorDict = self.__handleRopeError(err)
1188 1214
1189 result.update(errorDict) 1215 result.update(errorDict)
1190 1216
1191 self.sendJson("Changes", result) 1217 self.sendJson("Changes", result)
1192 1218
1193 def __calculateSignatureChanges(self, params): 1219 def __calculateSignatureChanges(self, params):
1194 """ 1220 """
1195 Private method to calculate the 'Signature' changes based on the 1221 Private method to calculate the 'Signature' changes based on the
1196 parameters sent by the server. 1222 parameters sent by the server.
1197 1223
1198 @param params dictionary containing the method parameters sent by 1224 @param params dictionary containing the method parameters sent by
1199 the server 1225 the server
1200 @type dict 1226 @type dict
1201 """ 1227 """
1202 changeGroup = params["ChangeGroup"] 1228 changeGroup = params["ChangeGroup"]
1206 removals = params["Removals"] 1232 removals = params["Removals"]
1207 additions = params["Additions"] 1233 additions = params["Additions"]
1208 newOrdering = params["Ordering"] 1234 newOrdering = params["Ordering"]
1209 autodef = params["AutoDef"] 1235 autodef = params["AutoDef"]
1210 doHierarchy = params["Hierarchy"] 1236 doHierarchy = params["Hierarchy"]
1211 1237
1212 errorDict = {} 1238 errorDict = {}
1213 changes = None 1239 changes = None
1214 result = { 1240 result = {
1215 "ChangeGroup": changeGroup, 1241 "ChangeGroup": changeGroup,
1216 "Title": title, 1242 "Title": title,
1217 } 1243 }
1218 1244
1219 changers = [] 1245 changers = []
1220 import rope.refactor.change_signature 1246 import rope.refactor.change_signature
1247
1221 # removals 1248 # removals
1222 for index in removals: 1249 for index in removals:
1223 remover = rope.refactor.change_signature.ArgumentRemover(index) 1250 remover = rope.refactor.change_signature.ArgumentRemover(index)
1224 changers.append(remover) 1251 changers.append(remover)
1225 # additions 1252 # additions
1226 for index, name, default, value in additions: 1253 for index, name, default, value in additions:
1227 adder = rope.refactor.change_signature.ArgumentAdder( 1254 adder = rope.refactor.change_signature.ArgumentAdder(
1228 index, name, default, value) 1255 index, name, default, value
1256 )
1229 changers.append(adder) 1257 changers.append(adder)
1230 # new ordering 1258 # new ordering
1231 changers.append(rope.refactor.change_signature.ArgumentReorderer( 1259 changers.append(
1232 newOrdering, autodef=autodef)) 1260 rope.refactor.change_signature.ArgumentReorderer(
1233 1261 newOrdering, autodef=autodef
1234 resource = rope.base.libutils.path_to_resource( 1262 )
1235 self.__project, filename) 1263 )
1264
1265 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1236 self.__progressHandle = ProgressHandle(self, title, True) 1266 self.__progressHandle = ProgressHandle(self, title, True)
1237 try: 1267 try:
1238 changer = rope.refactor.change_signature.ChangeSignature( 1268 changer = rope.refactor.change_signature.ChangeSignature(
1239 self.__project, resource, offset) 1269 self.__project, resource, offset
1270 )
1240 changes = changer.get_changes( 1271 changes = changer.get_changes(
1241 changers, in_hierarchy=doHierarchy, 1272 changers, in_hierarchy=doHierarchy, task_handle=self.__progressHandle
1242 task_handle=self.__progressHandle) 1273 )
1243 except Exception as err: 1274 except Exception as err:
1244 errorDict = self.__handleRopeError(err) 1275 errorDict = self.__handleRopeError(err)
1245 self.__progressHandle.reset() 1276 self.__progressHandle.reset()
1246 self.__progressHandle = None 1277 self.__progressHandle = None
1247 1278
1248 self.__changes[changeGroup] = changes 1279 self.__changes[changeGroup] = changes
1249 1280
1250 result["Subcommand"] = "ChangesCalculated" 1281 result["Subcommand"] = "ChangesCalculated"
1251 result.update(errorDict) 1282 result.update(errorDict)
1252 1283
1253 self.sendJson("Changes", result) 1284 self.sendJson("Changes", result)
1254 1285
1255 def __calculateInlineArgumentDefaultChanges(self, params): 1286 def __calculateInlineArgumentDefaultChanges(self, params):
1256 """ 1287 """
1257 Private method to calculate the 'Inline Argument Default' changes 1288 Private method to calculate the 'Inline Argument Default' changes
1258 based on the parameters sent by the server. 1289 based on the parameters sent by the server.
1259 1290
1260 @param params dictionary containing the method parameters sent by 1291 @param params dictionary containing the method parameters sent by
1261 the server 1292 the server
1262 @type dict 1293 @type dict
1263 """ 1294 """
1264 changeGroup = params["ChangeGroup"] 1295 changeGroup = params["ChangeGroup"]
1265 title = params["Title"] 1296 title = params["Title"]
1266 filename = params["FileName"] 1297 filename = params["FileName"]
1267 offset = params["Offset"] 1298 offset = params["Offset"]
1268 argumentIndex = params["Index"] 1299 argumentIndex = params["Index"]
1269 1300
1270 errorDict = {} 1301 errorDict = {}
1271 changes = None 1302 changes = None
1272 result = { 1303 result = {
1273 "ChangeGroup": changeGroup, 1304 "ChangeGroup": changeGroup,
1274 "Title": title, 1305 "Title": title,
1275 } 1306 }
1276 1307
1277 resource = rope.base.libutils.path_to_resource( 1308 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1278 self.__project, filename)
1279 self.__progressHandle = ProgressHandle(self, title, True) 1309 self.__progressHandle = ProgressHandle(self, title, True)
1280 try: 1310 try:
1281 changer = rope.refactor.change_signature.ChangeSignature( 1311 changer = rope.refactor.change_signature.ChangeSignature(
1282 self.__project, resource, offset) 1312 self.__project, resource, offset
1313 )
1283 inliner = rope.refactor.change_signature.ArgumentDefaultInliner( 1314 inliner = rope.refactor.change_signature.ArgumentDefaultInliner(
1284 argumentIndex) 1315 argumentIndex
1285 changes = changer.get_changes( 1316 )
1286 [inliner], task_handle=self.__progressHandle) 1317 changes = changer.get_changes([inliner], task_handle=self.__progressHandle)
1287 except Exception as err: 1318 except Exception as err:
1288 errorDict = self.__handleRopeError(err) 1319 errorDict = self.__handleRopeError(err)
1289 self.__progressHandle.reset() 1320 self.__progressHandle.reset()
1290 self.__progressHandle = None 1321 self.__progressHandle = None
1291 1322
1292 self.__changes[changeGroup] = changes 1323 self.__changes[changeGroup] = changes
1293 1324
1294 result["Subcommand"] = "ChangesCalculated" 1325 result["Subcommand"] = "ChangesCalculated"
1295 result.update(errorDict) 1326 result.update(errorDict)
1296 1327
1297 self.sendJson("Changes", result) 1328 self.sendJson("Changes", result)
1298 1329
1299 def __calculateModuleToPackageChanges(self, params): 1330 def __calculateModuleToPackageChanges(self, params):
1300 """ 1331 """
1301 Private method to calculate the 'Module to Package' changes 1332 Private method to calculate the 'Module to Package' changes
1302 based on the parameters sent by the server. 1333 based on the parameters sent by the server.
1303 1334
1304 @param params dictionary containing the method parameters sent by 1335 @param params dictionary containing the method parameters sent by
1305 the server 1336 the server
1306 @type dict 1337 @type dict
1307 """ 1338 """
1308 changeGroup = params["ChangeGroup"] 1339 changeGroup = params["ChangeGroup"]
1309 title = params["Title"] 1340 title = params["Title"]
1310 filename = params["FileName"] 1341 filename = params["FileName"]
1311 1342
1312 errorDict = {} 1343 errorDict = {}
1313 changes = None 1344 changes = None
1314 result = { 1345 result = {
1315 "ChangeGroup": changeGroup, 1346 "ChangeGroup": changeGroup,
1316 "Title": title, 1347 "Title": title,
1317 } 1348 }
1318 1349
1319 import rope.refactor.topackage 1350 import rope.refactor.topackage
1320 resource = rope.base.libutils.path_to_resource( 1351
1321 self.__project, filename) 1352 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1322 try: 1353 try:
1323 changes = rope.refactor.topackage.ModuleToPackage( 1354 changes = rope.refactor.topackage.ModuleToPackage(
1324 self.__project, resource).get_changes() 1355 self.__project, resource
1325 except Exception as err: 1356 ).get_changes()
1326 errorDict = self.__handleRopeError(err) 1357 except Exception as err:
1327 1358 errorDict = self.__handleRopeError(err)
1359
1328 self.__changes[changeGroup] = changes 1360 self.__changes[changeGroup] = changes
1329 1361
1330 # send the change description first 1362 # send the change description first
1331 description = changes.description if changes else "" 1363 description = changes.description if changes else ""
1332 self.sendJson("Changes", { 1364 self.sendJson(
1333 "ChangeGroup": changeGroup, 1365 "Changes",
1334 "Title": title, 1366 {
1335 "Subcommand": "ChangeDescription", 1367 "ChangeGroup": changeGroup,
1336 "Description": description, 1368 "Title": title,
1337 }) 1369 "Subcommand": "ChangeDescription",
1338 1370 "Description": description,
1371 },
1372 )
1373
1339 result["Subcommand"] = "ChangesCalculated" 1374 result["Subcommand"] = "ChangesCalculated"
1340 result.update(errorDict) 1375 result.update(errorDict)
1341 1376
1342 self.sendJson("Changes", result) 1377 self.sendJson("Changes", result)
1343 1378
1344 def __requestFieldName(self, params): 1379 def __requestFieldName(self, params):
1345 """ 1380 """
1346 Private method to calculate the 'Field Name' based on the parameters 1381 Private method to calculate the 'Field Name' based on the parameters
1347 sent by the server. 1382 sent by the server.
1348 1383
1349 @param params dictionary containing the method parameters sent by 1384 @param params dictionary containing the method parameters sent by
1350 the server 1385 the server
1351 @type dict 1386 @type dict
1352 """ 1387 """
1353 changeGroup = params["ChangeGroup"] 1388 changeGroup = params["ChangeGroup"]
1354 title = params["Title"] 1389 title = params["Title"]
1355 filename = params["FileName"] 1390 filename = params["FileName"]
1356 offset = params["Offset"] 1391 offset = params["Offset"]
1357 1392
1358 errorDict = {} 1393 errorDict = {}
1359 result = { 1394 result = {
1360 "Subcommand": "FieldName", 1395 "Subcommand": "FieldName",
1361 "ChangeGroup": changeGroup, 1396 "ChangeGroup": changeGroup,
1362 "Title": title, 1397 "Title": title,
1363 } 1398 }
1364 1399
1365 import rope.refactor.encapsulate_field 1400 import rope.refactor.encapsulate_field
1366 resource = rope.base.libutils.path_to_resource( 1401
1367 self.__project, filename) 1402 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1368 try: 1403 try:
1369 encapsulateField = ( 1404 encapsulateField = rope.refactor.encapsulate_field.EncapsulateField(
1370 rope.refactor.encapsulate_field.EncapsulateField( 1405 self.__project, resource, offset
1371 self.__project, resource, offset)
1372 ) 1406 )
1373 result["Name"] = encapsulateField.get_field_name() 1407 result["Name"] = encapsulateField.get_field_name()
1374 except Exception as err: 1408 except Exception as err:
1375 errorDict = self.__handleRopeError(err) 1409 errorDict = self.__handleRopeError(err)
1376 1410
1377 result.update(errorDict) 1411 result.update(errorDict)
1378 1412
1379 self.sendJson("Changes", result) 1413 self.sendJson("Changes", result)
1380 1414
1381 def __calculateEncapsulateFieldChanges(self, params): 1415 def __calculateEncapsulateFieldChanges(self, params):
1382 """ 1416 """
1383 Private method to calculate the 'Encapsulate Field' changes based on 1417 Private method to calculate the 'Encapsulate Field' changes based on
1384 the parameters sent by the server. 1418 the parameters sent by the server.
1385 1419
1386 @param params dictionary containing the method parameters sent by 1420 @param params dictionary containing the method parameters sent by
1387 the server 1421 the server
1388 @type dict 1422 @type dict
1389 """ 1423 """
1390 changeGroup = params["ChangeGroup"] 1424 changeGroup = params["ChangeGroup"]
1391 title = params["Title"] 1425 title = params["Title"]
1392 filename = params["FileName"] 1426 filename = params["FileName"]
1393 offset = params["Offset"] 1427 offset = params["Offset"]
1394 getter = params["Getter"] 1428 getter = params["Getter"]
1395 setter = params["Setter"] 1429 setter = params["Setter"]
1396 1430
1397 errorDict = {} 1431 errorDict = {}
1398 changes = None 1432 changes = None
1399 result = { 1433 result = {
1400 "ChangeGroup": changeGroup, 1434 "ChangeGroup": changeGroup,
1401 "Title": title, 1435 "Title": title,
1402 } 1436 }
1403 1437
1404 import rope.refactor.encapsulate_field 1438 import rope.refactor.encapsulate_field
1405 resource = rope.base.libutils.path_to_resource( 1439
1406 self.__project, filename) 1440 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1407 self.__progressHandle = ProgressHandle(self, title, True) 1441 self.__progressHandle = ProgressHandle(self, title, True)
1408 try: 1442 try:
1409 encapsulateField = ( 1443 encapsulateField = rope.refactor.encapsulate_field.EncapsulateField(
1410 rope.refactor.encapsulate_field.EncapsulateField( 1444 self.__project, resource, offset
1411 self.__project, resource, offset)
1412 ) 1445 )
1413 changes = encapsulateField.get_changes( 1446 changes = encapsulateField.get_changes(
1414 getter=getter, setter=setter, 1447 getter=getter, setter=setter, task_handle=self.__progressHandle
1415 task_handle=self.__progressHandle) 1448 )
1416 except Exception as err: 1449 except Exception as err:
1417 errorDict = self.__handleRopeError(err) 1450 errorDict = self.__handleRopeError(err)
1418 self.__progressHandle.reset() 1451 self.__progressHandle.reset()
1419 self.__progressHandle = None 1452 self.__progressHandle = None
1420 1453
1421 self.__changes[changeGroup] = changes 1454 self.__changes[changeGroup] = changes
1422 1455
1423 result["Subcommand"] = "ChangesCalculated" 1456 result["Subcommand"] = "ChangesCalculated"
1424 result.update(errorDict) 1457 result.update(errorDict)
1425 1458
1426 self.sendJson("Changes", result) 1459 self.sendJson("Changes", result)
1427 1460
1428 def __calculateLocalToAttributeChanges(self, params): 1461 def __calculateLocalToAttributeChanges(self, params):
1429 """ 1462 """
1430 Private method to calculate the 'Local Variabe to Attribute' changes 1463 Private method to calculate the 'Local Variabe to Attribute' changes
1431 based on the parameters sent by the server. 1464 based on the parameters sent by the server.
1432 1465
1433 @param params dictionary containing the method parameters sent by 1466 @param params dictionary containing the method parameters sent by
1434 the server 1467 the server
1435 @type dict 1468 @type dict
1436 """ 1469 """
1437 changeGroup = params["ChangeGroup"] 1470 changeGroup = params["ChangeGroup"]
1438 title = params["Title"] 1471 title = params["Title"]
1439 filename = params["FileName"] 1472 filename = params["FileName"]
1440 offset = params["Offset"] 1473 offset = params["Offset"]
1441 1474
1442 errorDict = {} 1475 errorDict = {}
1443 changes = None 1476 changes = None
1444 result = { 1477 result = {
1445 "ChangeGroup": changeGroup, 1478 "ChangeGroup": changeGroup,
1446 "Title": title, 1479 "Title": title,
1447 } 1480 }
1448 1481
1449 import rope.refactor.localtofield 1482 import rope.refactor.localtofield
1450 resource = rope.base.libutils.path_to_resource( 1483
1451 self.__project, filename) 1484 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1452 try: 1485 try:
1453 changes = rope.refactor.localtofield.LocalToField( 1486 changes = rope.refactor.localtofield.LocalToField(
1454 self.__project, resource, offset).get_changes() 1487 self.__project, resource, offset
1455 except Exception as err: 1488 ).get_changes()
1456 errorDict = self.__handleRopeError(err) 1489 except Exception as err:
1457 1490 errorDict = self.__handleRopeError(err)
1491
1458 self.__changes[changeGroup] = changes 1492 self.__changes[changeGroup] = changes
1459 1493
1460 # send the change description first 1494 # send the change description first
1461 description = changes.description if changes else "" 1495 description = changes.description if changes else ""
1462 self.sendJson("Changes", { 1496 self.sendJson(
1463 "ChangeGroup": changeGroup, 1497 "Changes",
1464 "Title": title, 1498 {
1465 "Subcommand": "ChangeDescription", 1499 "ChangeGroup": changeGroup,
1466 "Description": description, 1500 "Title": title,
1467 }) 1501 "Subcommand": "ChangeDescription",
1468 1502 "Description": description,
1503 },
1504 )
1505
1469 result["Subcommand"] = "ChangesCalculated" 1506 result["Subcommand"] = "ChangesCalculated"
1470 result.update(errorDict) 1507 result.update(errorDict)
1471 1508
1472 self.sendJson("Changes", result) 1509 self.sendJson("Changes", result)
1473 1510
1474 def __calculateMethodObjectChanges(self, params): 1511 def __calculateMethodObjectChanges(self, params):
1475 """ 1512 """
1476 Private method to calculate the 'Method to Method Object' changes 1513 Private method to calculate the 'Method to Method Object' changes
1477 based on the parameters sent by the server. 1514 based on the parameters sent by the server.
1478 1515
1479 @param params dictionary containing the method parameters sent by 1516 @param params dictionary containing the method parameters sent by
1480 the server 1517 the server
1481 @type dict 1518 @type dict
1482 """ 1519 """
1483 changeGroup = params["ChangeGroup"] 1520 changeGroup = params["ChangeGroup"]
1484 title = params["Title"] 1521 title = params["Title"]
1485 filename = params["FileName"] 1522 filename = params["FileName"]
1486 offset = params["Offset"] 1523 offset = params["Offset"]
1487 name = params["Name"] 1524 name = params["Name"]
1488 1525
1489 errorDict = {} 1526 errorDict = {}
1490 changes = None 1527 changes = None
1491 result = { 1528 result = {
1492 "ChangeGroup": changeGroup, 1529 "ChangeGroup": changeGroup,
1493 "Title": title, 1530 "Title": title,
1494 } 1531 }
1495 1532
1496 import rope.refactor.method_object 1533 import rope.refactor.method_object
1497 resource = rope.base.libutils.path_to_resource( 1534
1498 self.__project, filename) 1535 resource = rope.base.libutils.path_to_resource(self.__project, filename)
1499 try: 1536 try:
1500 converter = rope.refactor.method_object.MethodObject( 1537 converter = rope.refactor.method_object.MethodObject(
1501 self.__project, resource, offset) 1538 self.__project, resource, offset
1539 )
1502 changes = converter.get_changes(name) 1540 changes = converter.get_changes(name)
1503 except Exception as err: 1541 except Exception as err:
1504 errorDict = self.__handleRopeError(err) 1542 errorDict = self.__handleRopeError(err)
1505 1543
1506 self.__changes[changeGroup] = changes 1544 self.__changes[changeGroup] = changes
1507 1545
1508 result["Subcommand"] = "ChangesCalculated" 1546 result["Subcommand"] = "ChangesCalculated"
1509 result.update(errorDict) 1547 result.update(errorDict)
1510 1548
1511 self.sendJson("Changes", result) 1549 self.sendJson("Changes", result)
1512 1550
1513 1551
1514 if __name__ == '__main__': 1552 if __name__ == "__main__":
1515 if len(sys.argv) != 5: 1553 if len(sys.argv) != 5:
1516 print('Host, port, project path and module path parameters are' 1554 print(
1517 ' missing. Abort.') 1555 "Host, port, project path and module path parameters are" " missing. Abort."
1556 )
1518 sys.exit(1) 1557 sys.exit(1)
1519 1558
1520 host, port, projectPath = sys.argv[1:-1] 1559 host, port, projectPath = sys.argv[1:-1]
1521 1560
1522 # Create a Qt6 application object in order to allow the processing of 1561 # Create a Qt6 application object in order to allow the processing of
1523 # modules containing Qt stuff. 1562 # modules containing Qt stuff.
1524 try: 1563 try:
1525 from PyQt6.QtCore import QCoreApplication 1564 from PyQt6.QtCore import QCoreApplication
1526 except (ImportError, RuntimeError): 1565 except (ImportError, RuntimeError):
1527 QCoreApplication = None 1566 QCoreApplication = None
1528 if QCoreApplication is not None: 1567 if QCoreApplication is not None:
1529 app = QCoreApplication([]) 1568 app = QCoreApplication([])
1530 1569
1531 client = RefactoringClient(host, int(port), projectPath) 1570 client = RefactoringClient(host, int(port), projectPath)
1532 # Start the main loop 1571 # Start the main loop
1533 client.run() 1572 client.run()
1534 1573
1535 sys.exit(0) 1574 sys.exit(0)
1536 1575
1537 # 1576 #
1538 # eflag: noqa = M801 1577 # eflag: noqa = M801

eric ide

mercurial