-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathrefactor.py
80 lines (65 loc) · 3.27 KB
/
refactor.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
import sublime_plugin
from ..libs import *
from ..libs.view_helpers import *
from ..libs.reference import *
from .base_command import TypeScriptBaseTextCommand
class RefactorAction:
def __init__(self, refactor_name, action_name, description):
self.refactor_name = refactor_name
self.action_name = action_name
self.description = description
class TypescriptGetApplicableRefactorsCommand(TypeScriptBaseTextCommand):
"""Get refactors applicable to a specific region"""
def run(self, text):
check_update_view(self.view)
file_name = self.view.file_name()
(start_loc, end_loc) = get_start_and_end_from_view(self.view)
def choose(actions, index):
self.choose_refactor(file_name, start_loc, end_loc, actions, index)
def receive(response):
self.receive_refactors(response, choose)
cli.service.get_applicable_refactors_async(file_name, start_loc, end_loc, receive)
def receive_refactors(self, refactors_resp, choose):
if not refactors_resp["success"]:
return
actions = []
for refactor in refactors_resp["body"]:
refactor_name = refactor["name"]
if "inlineable" in refactor and not refactor["inlineable"]:
actions.append(RefactorAction(refactor_name, refactor_name, refactor["description"]))
else:
actions.extend(map(lambda action: RefactorAction(refactor_name, action["name"], action["description"]), refactor["actions"]))
refactoring_names = list(map(lambda action: action.description, actions))
# Somehow there's a blocking bug with `show_popup_menu`
# that leads to issues on certain platforms.
# https://github.com/SublimeTextIssues/Core/issues/1282
# self.view.show_popup_menu(refactoring_names, delayed_choose)
# So instead we can use `show_quick_panel` which looks great anyway.
active_window().show_quick_panel(refactoring_names, lambda i: choose(actions, i))
def choose_refactor(self, file_name, start_loc, end_loc, actions, index):
if index < 0:
return
action = actions[index]
def on_completed(edits_response):
self.view.run_command("typescript_apply_refactor", { "edits_response": edits_response })
def request_edits():
cli.service.get_edits_for_refactor_async(
path = file_name,
refactor_name = action.refactor_name,
action_name = action.action_name,
start_loc = start_loc, end_loc = end_loc,
on_completed = on_completed)
request_edits()
class TypescriptApplyRefactorCommand(TypeScriptBaseTextCommand):
def run(self, text, edits_response):
if not edits_response["success"]:
return
for edit in edits_response["body"]["edits"]:
file_name = edit["fileName"]
file_view = active_window().find_open_file(file_name)
if not file_view:
client_info = cli.get_or_add_file(file_name)
client_info.refactors_on_load = { "textChanges": edit["textChanges"] }
active_window().open_file(file_name)
else:
apply_formatting_changes(text, self.view, edit["textChanges"])