-
Notifications
You must be signed in to change notification settings - Fork 2
/
import_js.py
103 lines (79 loc) · 3.57 KB
/
import_js.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import json
import sublime
import sublime_plugin
from .import_js_daemon import ImportJsDaemon
DEBUG = False
STATUS_KEY = 'import-js'
STATUS_MESSAGE_WAITING_FOR_RESPONSE = 'Running ImportJS command...'
def plugin_unloaded():
ImportJsDaemon.shutdown()
class ImportJsTerminateCommand(sublime_plugin.TextCommand):
def run(self): # pylint: disable=arguments-differ
self.view.erase_status(STATUS_KEY)
ImportJsDaemon.shutdown()
class ImportJsReplaceCommand(sublime_plugin.TextCommand):
def run(self, edit, characters): # pylint: disable=arguments-differ
self.view.replace(edit, sublime.Region(0, self.view.size()), characters)
class ImportJsCommand(sublime_plugin.TextCommand):
def project_root(self):
return self.view.window().extract_variables()['folder']
def run(self, edit, **args): # pylint: disable=arguments-differ
current_file_contents = self.view.substr(sublime.Region(0, self.view.size()))
command = args.get('command')
payload = {
'command': command,
'pathToFile': self.view.file_name(),
'fileContent': current_file_contents,
}
if (command == 'word' or command == 'goto'):
payload['commandArg'] = self.view.substr(self.view.word(self.view.sel()[0]))
if command == 'add':
payload['commandArg'] = args.get('imports')
if DEBUG:
print('Command payload:')
print(payload)
if not self.view.get_status(STATUS_KEY):
self.view.set_status(STATUS_KEY, STATUS_MESSAGE_WAITING_FOR_RESPONSE)
ImportJsDaemon.execute_command(
self.project_root(),
(json.dumps(payload) + '\n').encode('utf-8'),
lambda response: self.handle_daemon_response(response, edit, command, args))
def handle_daemon_response(self, result_json, edit, command, command_args):
if DEBUG:
print('Command output:')
print(result_json)
self.view.erase_status(STATUS_KEY)
result = json.loads(result_json)
if result.get('error'):
sublime.error_message('Error when executing importjs:\n\n' + result.get('error'))
return
if result.get('messages'):
sublime.status_message('\n'.join(result.get('messages')))
if result.get('unresolvedImports'):
def handle_resolved_imports(resolved_imports):
command_args['command'] = 'add'
command_args['imports'] = resolved_imports
self.run(edit, **command_args)
self.view.run_command('import_js_replace', {'characters': result.get('fileContent')})
self.ask_to_resolve(result.get('unresolvedImports'), handle_resolved_imports)
return
if command == 'goto':
self.view.window().open_file(result.get('goto'))
else:
self.view.run_command('import_js_replace', {'characters': result.get('fileContent')})
def ask_to_resolve(self, unresolved_imports, on_resolve):
resolved = {}
unresolved_iter = iter(unresolved_imports)
def ask_recurse(word):
if not word:
on_resolve(resolved)
return
def on_done(i):
if i > -1:
resolved[word] = unresolved_imports[word][i]['data']
ask_recurse(next(unresolved_iter, None))
self.view.show_popup_menu(
list(map(lambda imp: imp.get('displayName'), unresolved_imports[word])),
on_done
)
ask_recurse(next(unresolved_iter, None))