-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathphpintel.py
More file actions
339 lines (281 loc) · 12.1 KB
/
phpintel.py
File metadata and controls
339 lines (281 loc) · 12.1 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
'''
SublimePHPIntel for Sublime Text 2
Copyright 2012 John Watson <https://github.com/jotson>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
import os
import threading
import time
import re
import sublime
import sublime_plugin
import phpparser
import intel
'''
TODO Detect variable assignment. e.g. $var = <code> where code returns an object
TODO Custom regex patterns for matching special cases, like factory methods.
Mage::getModel('catalog/product'): e.g. {'Mage::getModel\('(.*?)/(.*?)'\)':
'class': 'Mage_{1}_Model_{2}', 'cap_first': true}
TODO Detect when new files are added/removed and rescan
'''
class ScanProjectCommand(sublime_plugin.WindowCommand):
def run(self):
start_scan()
class ScanAbortCommand(sublime_plugin.WindowCommand):
def run(self):
abort_scan()
class GotoDeclarationCommand(sublime_plugin.TextCommand):
def run(self, edit):
if _scan_thread:
return
intel.reset()
view = self.view
symbol = view.substr(expand_word(view, view.sel()[0]))
path = None
if symbol:
for f in sublime.active_window().folders():
intel.load_index(f)
if symbol in intel._index:
# Find class
# TODO Show a quicklist when there is more than one choice
path = intel._index[symbol].pop()
self.view.window().open_file(path, sublime.TRANSIENT)
# elif symbol in intel._symbol_index:
# # Find other symbol
# # TODO Build an index (with line numbers) of all declared symbols to make this faster
# # TODO Show a quicklist when there is more than one choice
# path = intel._symbol_index[symbol].pop()
# self.view.window().open_file(path, sublime.TRANSIENT)
# # Proof of concept implementation:
# # for class_name in intel._index.keys():
# # i = intel.get_intel(class_name)
# # for s in i:
# # if s['name'] == symbol:
# # path = intel._index[class_name].pop()
# # view = self.view.window().open_file(path, sublime.TRANSIENT)
# # break
else:
sublime.status_message('Not found')
else:
sublime.status_message('Put cursor on some text first')
def expand_word(view, region):
'''
Expand the region to hold the entire word it is within
'''
start = region.a
end = region.b
while re.match('[\w|_]', view.substr(start - 1)):
start -= 1
while re.match('[\w|_]', view.substr(end)):
end += 1
return sublime.Region(start, end)
class EventListener(sublime_plugin.EventListener):
def on_post_save(self, view):
start_scan(path=view.file_name())
def on_query_completions(self, view, prefix, locations):
if _scan_thread:
return
intel.reset()
data = []
found = []
point = view.sel()[0].a
if view.score_selector(point, 'source.php') == 0 or view.score_selector(point, 'string.quoted') > 0:
return False
if self.has_intel():
# Find context
source = view.substr(sublime.Region(0, view.size()))
context, visibility, operator = phpparser.get_context(source, point)
# print context, visibility, operator
if len(context) == 1 and operator != '->' and operator != '::':
context = ['__global__', context[0]]
# Iterate context and find completion at this point
if context:
for f in sublime.active_window().folders():
intel.load_index(f)
else:
return False
if not intel:
return False
context_class, context_partial = intel.get_class(context)
# print '>>>', context, visibility, context_class, context_partial, str(time.time())
if context_class:
intel.find_completions(context, operator, context_class, context_partial, found, visibility, [])
if found:
for i in found:
snippet = None
argnames = []
if i['kind'] == 'var':
snippet = i['name'].replace('$', '')
returns = i['returns'] if i['returns'] else 'mixed'
data.append(tuple([str(i['name']) + '\t' + returns, str(snippet)]))
if i['kind'] == 'class':
snippet = i['name']
returns = i['returns'] if i['returns'] else 'mixed'
data.append(tuple([str(i['name']) + '\t' + returns, str(snippet)]))
if i['kind'] == 'func':
a = []
if len(i['args']):
args = i['args']
argnames = []
for j in range(0, len(args)):
argname, argtype = args[j]
argnames.append(argname)
a.append('${' + str(j + 1) + ':' + argname.replace('$', '\\$') + '}')
snippet = '{name}({args})'.format(name=i['name'], args=', '.join(a))
returns = i['returns'] if i['returns'] else 'mixed'
data.append(tuple([str(i['name']) + '(' + ', '.join(argnames) + ')' + '\t' + returns, str(snippet)]))
if data:
# Remove duplicates and sort
data = sorted(list(set(data)))
return data
#return (data, sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS)
else:
return False
def has_intel(self):
for f in sublime.active_window().folders():
if os.path.exists(os.path.join(f, '.phpintel')):
return True
_scan_thread = None
_scan_lock = threading.RLock()
def start_scan(path='__all__'):
global _scan_thread
with _scan_lock:
if _scan_thread:
_scan_thread.queue(path)
else:
s = sublime.load_settings("SublimePHPIntel.sublime-settings")
blacklist = s.get("scan_blacklist")
folders = sublime.active_window().folders();
_scan_thread = ScanThread(blacklist, folders)
_scan_thread.queue(path)
_scan_thread.start()
def abort_scan():
global _scan_thread
if _scan_thread:
_scan_thread.abort()
class ScanThread(threading.Thread):
_scan_queue = []
_abort = False
_blacklist = None
_folders = None
def __init__(self, blacklist, folders):
self._blacklist = blacklist
self._folders = folders
threading.Thread.__init__(self)
def queue(self, path='__all__'):
if not path in self._scan_queue:
self._scan_queue.append(path)
def abort(self):
self._abort = True
def run(self):
self._abort = False
self.progress = ThreadProgress(self, '', '')
self.progress.start()
start_time = time.time()
scanned_something = False
def in_blacklist(path):
for b in self._blacklist:
if path.find(b) >= 0:
return True
return False
while True:
with _scan_lock:
if len(self._scan_queue) == 0:
global _scan_thread
_scan_thread = None
return
filename = self._scan_queue.pop()
if filename == '__all__':
# Scan entire project
for f in self._folders:
intel.reset()
if self._abort:
break
for root, dirs, files in os.walk(f, followlinks=True):
if self._abort:
break
for name in files:
if self._abort:
break
if os.path.splitext(name)[1] == '.php':
scanned_something = True
path = os.path.join(root, name)
if in_blacklist(path):
continue
newpath, currentfile = os.path.split(path)
newpath, lastdir = os.path.split(newpath)
self.progress.message = 'Scanning .../' + lastdir + '/' + currentfile
d = phpparser.scan_file(path)
if d:
intel.save(d, f, path)
intel.update_index(path, *set([x['class'] for x in d]))
time.sleep(0.010)
intel.save_index(f)
elif filename:
# Scan one file
if os.path.splitext(filename)[1] == '.php':
path = filename
if in_blacklist(path):
return
for f in self._folders:
if path.startswith(f):
self.progress.message = 'Scanning ' + path
scanned_something = True
d = phpparser.scan_file(path)
intel.save(d, f, path)
intel.reset()
intel.load_index(f)
intel.update_index(path, *set([x['class'] for x in d]))
intel.save_index(f)
break
if scanned_something:
elapsed_s = time.time() - start_time
if elapsed_s > 120:
elapsed = '{min:d}m{sec:d}s'.format(min=int(elapsed_s / 60), sec=int(elapsed_s % 60))
else:
elapsed = '{sec:.2f}s'.format(sec=elapsed_s)
self.progress.success_message = 'Scan completed in {elapsed}'.format(elapsed=elapsed)
class ThreadProgress(threading.Thread):
'''
Cribbed from Package Control and modified into a real Thread just for fun.
'''
def __init__(self, thread, message, success_message):
self.thread = thread
self.message = message
self.success_message = success_message
self.addend = 1
self.size = 8
self.i = 0
threading.Thread.__init__(self)
def run(self):
while True:
if not self.thread.is_alive():
if self.success_message:
self.update_status(self.success_message)
break
before = self.i % self.size
after = (self.size - 1) - before
if self.message:
self.update_status('[{before}={after}] {message}'.format(message=self.message, before=' ' * before, after=' ' * after))
if not after:
self.addend = -1
if not before:
self.addend = 1
self.i += self.addend
time.sleep(0.100)
def update_status(self, message):
sublime.set_timeout(lambda: sublime.status_message(message), 0)