-
Notifications
You must be signed in to change notification settings - Fork 3
/
sublime_import_magic.py
215 lines (166 loc) · 6.68 KB
/
sublime_import_magic.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
import sys
import_path = os.path.dirname(__file__)
if import_path not in sys.path:
sys.path.insert(0, import_path)
# importmagic: manage
import sublime
import sublime_plugin
from distutils import sysconfig
from threading import RLock, Thread
from importmagic.importer import get_update
from importmagic.index import LIB_LOCATIONS, SymbolIndex
from importmagic.symbols import Scope
def index_filename():
settings = sublime.load_settings('Python Import Magic.sublime-settings')
return settings.get('index_filename', '.importmagic.idx')
def log(fmt, *args, **kwargs):
text = fmt.format(*args, **kwargs)
text = 'ImportMagic: {0}'.format(text)
if kwargs.get('status', False):
sublime.status_message(text)
print(text)
class Indexer(object):
def __init__(self):
self._lock = RLock()
self._indexes = {}
self._threads = {}
def rebuild(self, root):
log('Rebuilding index for {0}', root)
with self._lock:
index_file = os.path.join(root, index_filename())
try:
os.unlink(index_file)
except OSError:
pass
try:
del self._indexes[root]
except KeyError:
pass
log('Rebuilt index for {0}', root, status=True)
return self.index(root)
def index(self, root):
with self._lock:
if root in self._indexes:
return self._indexes[root]
if root in self._threads:
log('WARNING: Still loading index from {0}', root, status=True)
return None
index_file = os.path.join(root, index_filename())
thread = self._threads[root] = Thread(
target=self._indexer, args=(root, index_file))
self._threads[root].start()
self._lock.release()
thread.join(2.0)
self._lock.acquire()
if not thread.is_alive():
return self._indexes[root]
log('WARNING: Still loading index from {0}', root, status=True)
def _indexer(self, root, index_file):
# Delay indexing briefly so we don't hammer the system.
print(os.environ)
settings = sublime.load_settings('Python Import Magic.sublime-settings')
locations = settings.get('python_path', LIB_LOCATIONS)
print(locations)
sublime.status_message('Loading index {0}'.format(root))
if os.path.exists(index_file):
log('Loading index for {0}', root)
with open(index_file) as fd:
index = SymbolIndex.deserialize(fd)
else:
index = SymbolIndex(locations=locations)
paths = self._make_python_path(root, locations=locations)
log('Indexing {0} with paths {1}',
root, os.path.pathsep.join(paths))
index.build_index(paths)
with open(index_file, 'w') as fd:
fd.write(index.serialize())
with self._lock:
self._indexes[root] = index
del self._threads[root]
log('Ready for {0}', root, status=True)
def _make_python_path(self, root, locations):
paths = [p[0] for p in locations]
if root not in paths:
paths.insert(0, root)
virtualenv = os.environ.get('VIRTUAL_ENV')
if virtualenv:
for path in (sysconfig.get_python_lib(standard_lib=True, prefix=virtualenv),
sysconfig.get_python_lib(prefix=virtualenv)):
paths.insert(0, path)
return paths
class PythonImportMagic(sublime_plugin.EventListener):
def on_pre_save(self, view):
settings = sublime.load_settings(
'Python Import Magic.sublime-settings')
if not settings.get('update_imports_on_save', False):
return
view.run_command("update_python_imports")
class UpdatePythonImports(sublime_plugin.TextCommand):
def run(self, edit):
index = index_for_view(self.view)
if not index:
return
update_imports_for_view(edit, self.view, index)
class RebuildPythonImportIndex(sublime_plugin.TextCommand):
def run(self, edit):
indexer.rebuild(get_project_root(self.view))
class ImportPythonSymbol(sublime_plugin.TextCommand):
def run(self, edit):
symbols = indexer.symbol_scores("")
# self.window.show_quick_panel(items, on_done, <flags>, <selected_index>, <on_highlighted>)
# self.window.show_input_panel("Select import", "", on_done, self._match_symbol, on_cancel)
def _match_symbol(self, symbol):
scores = self.symbol_index.symbol_scores(symbol)
def sort_key(item):
score, mod, var = item
if mod in self.favorites:
return 2 + score, mod, var
return score, mod, var
scores.sort(key=sort_key, reverse=True)
return ["from %s import %s" % (mod, var) if var else "import %s" % mod
for (_, mod, var) in scores]
indexer = Indexer()
def index_for_view(view):
if not view.match_selector(0, 'source.python'):
return
return indexer.index(get_project_root(view))
def get_project_root(view):
# NOTE: It would be nice if this wasn't so difficult :\
try: # handle case with no open folder
return view.window().folders()[0]
except IndexError:
dir = get_working_dir(view)
last_package = None
while not os.path.exists(os.path.join(dir, index_filename())):
if os.path.exists(os.path.join(dir, '__init__.py')):
last_package = dir
dir = os.path.dirname(dir)
if os.path.dirname(dir) == dir:
return last_package
return dir
def get_working_dir(view):
file_name = active_file_name(view)
if file_name:
return os.path.realpath(os.path.dirname(file_name))
else:
try: # handle case with no open folder
return view.window().folders()[0]
except IndexError:
return ''
def active_file_name(view):
if view and view.file_name() and len(view.file_name()) > 0:
return view.file_name()
def update_imports_for_view(edit, view, index):
# Extract symbols from source
src = view.substr(sublime.Region(0, view.size()))
scope = Scope.from_source(src)
unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
# Get update region and replacement text.
start_line, end_line, text = get_update(src, index, unresolved, unreferenced)
# Get region that needs updating
start = view.text_point(start_line, 0)
end = view.text_point(end_line, 0)
region = sublime.Region(start, end)
# Replace existing imports!
view.replace(edit, region, text)