Skip to content

Commit 1787173

Browse files
committed
Merge pull request #334 from hoanhtien/fixRenameCommand
Handle error of rename command.
2 parents 43f32de + 1affc67 commit 1787173

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

typescript/commands/rename.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,39 @@
55

66

77
class TypescriptRenameCommand(TypeScriptBaseTextCommand):
8-
"""Rename command"""
8+
"""
9+
Command to rename identifier
10+
"""
911
def run(self, text):
1012
check_update_view(self.view)
13+
1114
rename_response = cli.service.rename(self.view.file_name(), get_location_from_view(self.view))
12-
if rename_response["success"]:
13-
info_locations = rename_response["body"]
14-
# Todo: handle error when the symbol cannot be renamed (with 'localizedErrorMessage' property)
15-
log.debug(info_locations["info"])
16-
display_name = info_locations["info"]["fullDisplayName"]
17-
outer_locations = info_locations["locs"]
15+
if not rename_response['success']:
16+
return
1817

19-
def on_cancel():
20-
return
18+
body = rename_response['body']
2119

22-
def on_done(new_name):
23-
args = {"newName": new_name, "outerLocs": outer_locations}
24-
args_json_str = json_helpers.encode(args)
25-
self.view.run_command('typescript_finish_rename', {"args_json": args_json_str})
20+
info = body['info']
21+
if not info['canRename']:
22+
self.view.set_status('typescript_error', info['localizedErrorMessage'])
23+
return
2624

27-
if len(outer_locations) > 0:
28-
sublime.active_window().show_input_panel(
29-
"New name for {0}: ".format(display_name),
30-
info_locations["info"]["displayName"],
31-
on_done, None, on_cancel
32-
)
25+
display_name = info['fullDisplayName']
26+
outer_locations = body['locs']
27+
28+
def on_done(new_name):
29+
args = {'newName': new_name, 'outerLocs': outer_locations}
30+
args_json_str = json_helpers.encode(args)
31+
self.view.run_command('typescript_finish_rename', {'args_json': args_json_str})
32+
33+
if len(outer_locations) > 0:
34+
sublime.active_window().show_input_panel(
35+
'New name for {0}: '.format(display_name),
36+
info['displayName'], # initial text
37+
on_done,
38+
None, # on_change
39+
None # on_cancel
40+
)
3341

3442

3543
class TypescriptFinishRenameCommand(TypeScriptBaseTextCommand):

0 commit comments

Comments
 (0)