-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathrename.py
83 lines (71 loc) · 3.42 KB
/
rename.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from ..libs import *
from ..libs.view_helpers import *
from ..libs.reference import *
from .base_command import TypeScriptBaseTextCommand
class TypescriptRenameCommand(TypeScriptBaseTextCommand):
"""
Command to rename identifier
"""
def run(self, text):
check_update_view(self.view)
rename_response = cli.service.rename(self.view.file_name(), get_location_from_view(self.view))
if not rename_response['success']:
return
body = rename_response['body']
info = body['info']
if not info['canRename']:
self.view.set_status('typescript_error', info['localizedErrorMessage'])
return
outer_locations = body['locs']
def on_done(new_name):
args = {'newName': new_name, 'outerLocs': outer_locations}
args_json_str = json_helpers.encode(args)
self.view.run_command('typescript_finish_rename', {'args_json': args_json_str})
if len(outer_locations) > 0:
sublime.active_window().show_input_panel(
'New name: ',
info['displayName'], # initial text
on_done,
None, # on_change
None # on_cancel
)
class TypescriptFinishRenameCommand(TypeScriptBaseTextCommand):
"""
Called from on_done handler in finish_rename command
on_done is called by input panel for new name
"""
def run(self, text, args_json=""):
args = json_helpers.decode(args_json)
new_name = args["newName"]
outer_locations = args["outerLocs"]
if len(outer_locations) > 0:
for outerLoc in outer_locations:
file = outerLoc["file"]
inner_locations = outerLoc["locs"]
rename_view = active_window().find_open_file(file)
if not rename_view:
# File not loaded but on disk
client_info = cli.get_or_add_file(file)
client_info.rename_on_load = {"locs": inner_locations, "name": new_name}
active_window().open_file(file)
elif rename_view != self.view:
# File opened but not current one
rename_view.run_command('typescript_delayed_rename_file',
{"locs_name": {"locs": inner_locations, "name": new_name}})
else:
for inner_location in inner_locations[::-1]:
start_line, start_offset = extract_line_offset(inner_location["start"])
end_line, end_offset = extract_line_offset(inner_location["end"])
apply_edit(text, self.view, start_line, start_offset, end_line,
end_offset, new_text=new_name)
class TypescriptDelayedRenameFile(TypeScriptBaseTextCommand):
"""Rename in 'on_load' method"""
def run(self, text, locs_name=None):
if locs_name['locs'] and (len(locs_name['name']) > 0):
locs = locs_name['locs']
name = locs_name['name']
for inner_location in locs:
start_line, start_offset = extract_line_offset(inner_location['start'])
end_line, end_offset = extract_line_offset(inner_location['end'])
apply_edit(text, self.view, start_line, start_offset, end_line,
end_offset, new_text=name)