53 "getCallTips": self.__getCallTips, |
54 "getCallTips": self.__getCallTips, |
54 "getDocumentation": self.__getDocumentation, |
55 "getDocumentation": self.__getDocumentation, |
55 "hoverHelp": self.__getHoverHelp, |
56 "hoverHelp": self.__getHoverHelp, |
56 "gotoDefinition": self.__getAssignment, |
57 "gotoDefinition": self.__getAssignment, |
57 "gotoReferences": self.__getReferences, |
58 "gotoReferences": self.__getReferences, |
|
59 |
|
60 "renameVariable": self.__renameVariable, |
|
61 "applyRefactoring": self.__applyRefactoring, |
|
62 "cancelRefactoring": self.__cancelRefactoring, |
58 } |
63 } |
59 |
64 |
60 self.__id = idString |
65 self.__id = idString |
61 |
66 |
62 self.__project = None |
67 self.__project = None |
|
68 |
|
69 self.__refactorings = {} |
63 |
70 |
64 def handleCall(self, method, params): |
71 def handleCall(self, method, params): |
65 """ |
72 """ |
66 Public method to handle a method call from the server. |
73 Public method to handle a method call from the server. |
67 |
74 |
402 "Uuid": uid, |
409 "Uuid": uid, |
403 } |
410 } |
404 result.update(errorDict) |
411 result.update(errorDict) |
405 |
412 |
406 self.sendJson("GotoReferencesResult", result) |
413 self.sendJson("GotoReferencesResult", result) |
|
414 |
|
415 def __renameVariable(self, params): |
|
416 """ |
|
417 Private method to rename the variable under the cursor. |
|
418 |
|
419 @param params dictionary containing the method parameters |
|
420 @type dict |
|
421 """ |
|
422 filename = params["FileName"] |
|
423 source = params["Source"] |
|
424 line = params["Line"] |
|
425 index = params["Index"] |
|
426 uid = params["Uuid"] |
|
427 newName = params["NewName"] |
|
428 |
|
429 errorDict = {} |
|
430 diff = "" |
|
431 |
|
432 script = jedi.Script(source, path=filename, project=self.__project) |
|
433 |
|
434 try: |
|
435 refactoring = script.rename(line, index, new_name=newName) |
|
436 self.__refactorings[uid] = refactoring |
|
437 diff = refactoring.get_diff() |
|
438 except SuppressedException as err: |
|
439 errorDict = self.__handleError(err) |
|
440 |
|
441 result = { |
|
442 "Diff": diff, |
|
443 "Uuid": uid, |
|
444 } |
|
445 result.update(errorDict) |
|
446 |
|
447 self.sendJson("RenameVariableDiff", result) |
|
448 |
|
449 def __applyRefactoring(self, params): |
|
450 """ |
|
451 Private method to apply a refactoring. |
|
452 |
|
453 @param params dictionary containing the method parameters |
|
454 @type dict |
|
455 """ |
|
456 uid = params["Uuid"] |
|
457 |
|
458 errorDict = {} |
|
459 |
|
460 try: |
|
461 refactoring = self.__refactorings[uid] |
|
462 refactoring.apply() |
|
463 ok = True |
|
464 except KeyError: |
|
465 ok = False |
|
466 except SuppressedException as err: |
|
467 errorDict = self.__handleError(err) |
|
468 |
|
469 result = { |
|
470 "result": ok, |
|
471 } |
|
472 result.update(errorDict) |
|
473 |
|
474 self.sendJson("RefactoringApplyResult", result) |
|
475 |
|
476 with contextlib.suppress(KeyError): |
|
477 del self.__refactorings[uid] |
|
478 |
|
479 def __cancelRefactoring(self, params): |
|
480 """ |
|
481 Private method to cancel a refactoring. |
|
482 |
|
483 @param params dictionary containing the method parameters |
|
484 @type dict |
|
485 """ |
|
486 uid = params["Uuid"] |
|
487 with contextlib.suppress(KeyError): |
|
488 del self.__refactorings[uid] |
407 |
489 |
408 |
490 |
409 if __name__ == '__main__': |
491 if __name__ == '__main__': |
410 if len(sys.argv) != 5: |
492 if len(sys.argv) != 5: |
411 print('Host, port, id and module path parameters are missing.' |
493 print('Host, port, id and module path parameters are missing.' |