-
Notifications
You must be signed in to change notification settings - Fork 7
/
import_helper.py
189 lines (160 loc) · 6.49 KB
/
import_helper.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import sublime
import sublime_plugin
import os
PROJECT_NAME = "Import Helper"
PACKAGE_PATH = os.path.dirname(os.path.realpath(__file__))
RUN_PATH = os.path.join(PACKAGE_PATH, "backend_run.js")
NODE_BIN = "node"
# Collection of entries
NODE_MODULES = []
SOURCE_MODULES = []
TYPESCRIPT_PATHS = []
from .library.utils import get_time, status_message
from .library.get_setting import get_setting
from .library.debug import debug
from .library.update_source_modules import update_source_modules
from .library.update_node_modules import update_node_modules
from .library.list_imports_command import list_imports_command
from .library.get_import_root import get_import_root
from .library.insert_import_command import insert_import_command
from .library.paste_import_command import paste_import_command
from .library.update_typescript_paths import update_typescript_paths
from .library.query_completions_modules import query_completions_modules
from .library.get_node_executable import get_node_executable
from .library.exec_command import exec_sync
def plugin_loaded():
print()
debug("Plugin loaded", PROJECT_NAME)
sublime.set_timeout(setup, 0)
def setup():
check_node()
update_source_modules(SOURCE_MODULES)
update_node_modules(NODE_MODULES)
update_typescript_paths(TYPESCRIPT_PATHS)
def check_node():
node_executable = get_node_executable()
(err, out) = exec_sync([node_executable, "--version"], None)
version = float(".".join(out[1:].split(".")[0:2]))
if version < 12:
status_message("Node.js version is {0}, but 12+ is required".format(version))
# window.run_command('update_source_modules')
class UpdateSourceModulesCommand(sublime_plugin.WindowCommand):
def run(self):
update_source_modules(SOURCE_MODULES)
# Command list_imports - Show all available imports
# view.run_command('list_imports')
class ListImportsCommand(sublime_plugin.TextCommand):
def run(self, edit):
list_imports_command(
view=self.view,
import_root=get_import_root(),
entry_modules=SOURCE_MODULES + NODE_MODULES,
typescript_paths=TYPESCRIPT_PATHS,
)
# Adds import of identifier near cursor (insert_import)
# view.run_command('insert_import', args=({'name': 'createName'}))
class InsertImportCommand(sublime_plugin.TextCommand):
def run(self, edit, name=None, point=None, notify=True):
insert_import_command(
view=self.view,
name=name,
point=point,
notify=notify,
entry_modules=SOURCE_MODULES + NODE_MODULES,
import_root=get_import_root(),
typescript_paths=TYPESCRIPT_PATHS,
)
class PasteImportCommand(sublime_plugin.TextCommand):
def run(
self,
edit,
item,
typescript_paths=TYPESCRIPT_PATHS,
test_selected_index=-1,
settings={},
):
replace_content = paste_import_command(
view=self.view,
item=item,
typescript_paths=typescript_paths,
test_selected_index=test_selected_index,
settings=settings,
)
if type(replace_content) == str:
self.view.replace(
edit, sublime.Region(0, self.view.size()), replace_content
)
# window.run_command('initialize_setup')
# sublime.active_window().run_command('initialize_setup', args={'a':'bar'})
class InitializeSetupCommand(sublime_plugin.WindowCommand):
def run(self):
setup()
# view.run_command('import_from_clipboard')
class ImportFromClipboardCommand(sublime_plugin.TextCommand):
def run(self, edit):
insert_import_command(
view=self.view,
name=sublime.get_clipboard(),
notify=True,
entry_modules=SOURCE_MODULES + NODE_MODULES,
typescript_paths=TYPESCRIPT_PATHS,
)
class ImportHelperEventListener(sublime_plugin.EventListener):
def __init__(self):
self.viewIds = []
def on_new(self, view):
self.viewIds.append(view.id())
def on_post_save(self, view):
if view.id() in self.viewIds:
self.viewIds.remove(view.id())
update_source_modules(SOURCE_MODULES)
class ImportHelperViewEventListener(sublime_plugin.ViewEventListener):
def __init__(self, view):
super().__init__(view)
self.completions_info = {"time": -1, "result": [], "prefix": ""}
self.in_auto_complete = False
self.autocomplete_point = 0
self.autocomplete_export_names = get_setting("autocomplete_export_names", True)
self.autocomplete_auto_import = get_setting("autocomplete_auto_import", False)
def on_query_completions(self, prefix, locations):
if not self.autocomplete_export_names or not (
len(prefix) > 0
and self.view.match_selector(
self.autocomplete_point,
"source.ts, source.ts.unittest, source.tsx, source.js, source.jsx",
)
):
return []
self.autocomplete_point = locations[0]
if (
get_time() > self.completions_info["time"] + 1
or prefix != self.completions_info["prefix"]
):
self.completions_info["time"] = get_time()
self.completions_info["prefix"] = prefix
self.completions_info["result"] = query_completions_modules(
prefix=prefix, source_modules=SOURCE_MODULES, node_modules=NODE_MODULES
)
return self.completions_info["result"]
def on_post_text_command(self, command_name, args):
if not (self.autocomplete_auto_import and self.autocomplete_export_names):
return
if self.in_auto_complete and command_name in [
"insert_best_completion",
"insert_dimensions",
]:
self.in_auto_complete = False
self.view.run_command(
"insert_import",
args=({"point": self.autocomplete_point - 1, "notify": False}),
)
elif command_name in [
"auto_complete",
"replace_completion_with_next_completion",
"replace_completion_with_auto_complete",
]:
self.in_auto_complete = True
elif command_name == "hide_auto_complete":
self.in_auto_complete = False
def on_activated(self):
self.in_auto_complete = False