-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDPCompletions.py
110 lines (97 loc) · 4.18 KB
/
DPCompletions.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
import os, fnmatch, re, threading, sublime, sublime_plugin, sys, codecs
class ProjectCompletionsScan(threading.Thread):
def __init__(self, rootPath, timeout):
threading.Thread.__init__(self)
self.rootPath = rootPath
self.timeout = timeout
self.result = None
def run(self):
try:
patterns = ['.inc', '.php', '.module']
search = re.compile(r'^function\s(.+?)\((?:(.+?))?\)\s{$', re.MULTILINE)
compPath = os.path.dirname(self.rootPath) + '/Drupal.sublime-projectcompletions'
cfp = open(compPath, 'w')
cfp.close()
cfp = open(compPath, 'a')
for root, dirs, files in os.walk(os.path.dirname(self.rootPath)):
for p in patterns:
for f in files:
if f.endswith(p):
# Open the file.
fp = codecs.open(os.path.join(root, f), 'r', encoding='utf-8', errors='ignore')
content = fp.read()
# Retrieve functions from file.
funcs = search.findall(content)
for row in funcs:
args = ''
if row[1]:
i = 0
arglist = row[1].replace(', ', ',').split(',')
for i, val in enumerate(arglist):
arglist[i] = '${%s:%s}' % (i + 1, arglist[i].replace('$', '\$'))
args = '(%s)' % (', '.join(arglist))
else:
args = '()'
line = '%s\t%s%s' % (row[0], row[0], args)
# Append to file
cfp.write(line + "\n")
fp.close()
cfp.close()
return
except:
exc = sys.exc_info()[1]
sublime.status_message(str(exc))
raise
class ProjectCompletions(sublime_plugin.EventListener):
def find_file(self, start_at, look_for):
start_at = os.path.abspath(start_at)
if not os.path.isdir(start_at):
start_at = os.path.dirname(start_at)
while True:
for filename in os.listdir(start_at):
if fnmatch.fnmatch(filename, look_for):
return os.path.join(start_at, filename)
continue_at = os.path.abspath(os.path.join(start_at, '..'))
if continue_at == start_at:
return None
start_at = continue_at
def on_post_save(self, view):
if view.settings().get("drupal_project_autocomplete_ignore"):
return;
path = view.file_name()
rootPath = None
if path:
# Try to find the myproject.sublime-project file
for filename in ['*.sublime-project']:
rootPath = self.find_file(path, filename)
if rootPath:
threads = []
thread = ProjectCompletionsScan(rootPath, 5)
threads.append(thread)
thread.start()
def on_query_completions(self, view, prefix, locations):
if view.settings().get("drupal_project_autocomplete_ignore"):
return []
if not view.match_selector(locations[0], "source.php"):
return []
path = view.file_name()
completions_location = None
if path:
# Try to find the Drupal.sublime-completions file
for filename in ['*.sublime-projectcompletions']:
completions_location = self.find_file(path, filename)
if completions_location:
fp = open(completions_location, 'r')
t = ()
data = []
line = fp.readline()
while len(line) != 0:
e1, e2 = line.split("\t")
if re.match(prefix, e1, re.IGNORECASE):
t = e1, e2.rstrip()
data.append(t)
line = fp.readline()
fp.close()
return data
else:
return []