-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
executable file
·112 lines (90 loc) · 3.58 KB
/
plugin.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
import os
import sublime
import sublime_plugin
import subprocess
import os
class XpFormat(sublime_plugin.TextCommand):
def is_enabled(self):
return self.view.match_selector(0, 'embedding.php')
def run(self, edit):
# Sort imports
uses = self.view.find_all('^use [^;]+;')
if not uses:
return
imports = {'local' : [], 'remote': [], 'symbol' : []}
for match in uses:
line = self.view.substr(match)
if line.find(' function ') > -1:
kind = 'symbol'
elif line.find(' const ') > -1:
kind = 'symbol'
elif line.find(' from ') > -1:
kind = 'remote'
else:
kind = 'local'
imports[kind].append(line)
lines = ''
for kind in ['local', 'remote', 'symbol']:
listof = imports[kind]
if listof:
listof.sort()
lines += "\n".join(listof) + "\n\n"
self.view.replace(edit, sublime.Region(uses[0].begin(), uses[len(uses) - 1].end()), lines.rstrip())
class CompleteTypes(sublime_plugin.EventListener):
def on_pre_save(self, view):
if not view.match_selector(0, 'embedding.php'):
return
print('>> Saving ' + view.file_name())
view.run_command('xp_format')
def on_query_completions(self, view, prefix, locations):
if view.file_name() is None:
return
completions = []
line = view.substr(view.line(view.sel()[0]));
if line.startswith('use '):
dotted = line.replace('\\', '.')
package = dotted[4:-len(prefix) - 1]
search = dotted[4:-1]
# Find base path
base = os.path.dirname(view.file_name())
while not os.path.isfile(os.path.join(base, 'composer.json')):
parent = os.path.dirname(base)
if os.path.samefile(parent, base):
break
base = parent
print('>> ' + dotted + '* @ ' + base);
# Start process
if os.name == 'nt':
info = subprocess.STARTUPINFO()
info.dwFlags |= subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = subprocess.SW_HIDE
else:
info = None
command = 'xp "' + os.path.join(os.path.dirname(__file__), 'types.script.php') + '" "' + package + '"';
try:
proc = subprocess.Popen(
command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
startupinfo=info,
cwd=base
)
output, error = proc.communicate()
# Transform output
for line in output.decode().split("\n"):
if (line.startswith(search)):
completions.append(line.split('>>'));
return completions
except FileNotFoundError as e:
sublime.status_message(str(e) + ' @ ' + command)
return []
def disable_builtin_php():
completions= os.path.join(sublime.packages_path(), 'PHP', 'PHP.sublime-completions')
if not os.path.isfile(completions):
os.makedirs(os.path.dirname(completions))
with open(completions, 'w+') as file:
file.write(str('// Builtin completions overridden by XP Framework'))
file.write(str('// Delete this file to restore them'))
def plugin_loaded():
disable_builtin_php()