-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathphpfmt.py
79 lines (62 loc) · 2.34 KB
/
phpfmt.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
import os
import os.path
import shutil
import sublime
import sublime_plugin
import subprocess
from os.path import dirname, realpath
class phpfmt(sublime_plugin.EventListener):
def __init__(self):
self.debug = True
def on_post_save(self, view):
if int(sublime.version()) < 3000:
self.on_post_save_async(view)
def on_post_save_async(self, view):
s = sublime.load_settings('phpfmt.sublime-settings')
self.debug = s.get("debug", False)
psr = s.get("psr1_and_2", False)
indent_with_space = s.get("indent_with_space", False)
php_bin = s.get("php_bin", "php")
formatter_path = os.path.join(dirname(realpath(sublime.packages_path())), "Packages", "phpfmt", "codeFormatter.php")
uri = view.file_name()
dirnm, sfn = os.path.split(uri)
ext = os.path.splitext(uri)[1][1:]
if self.debug:
print("phpfmt:", uri)
if "php" != ext:
return False
cmd = [php_bin]
if self.debug:
cmd.append("-ddisplay_errors=0")
cmd.append(formatter_path)
if psr:
cmd.append("--psr")
if indent_with_space:
cmd.append("--indent_with_space")
cmd.append(uri)
uri_tmp = uri + "~"
if self.debug:
print("cmd: ", cmd)
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=dirnm, shell=False, startupinfo=startupinfo)
else:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=dirnm, shell=False)
res, err = p.communicate()
if err:
if self.debug:
print("err: ", err)
else:
if int(sublime.version()) < 3000:
with open(uri_tmp, 'w+') as f:
f.write(res)
else:
with open(uri_tmp, 'bw+') as f:
f.write(res)
if self.debug:
print("Stored:", len(res), "bytes")
shutil.move(uri_tmp, uri)
sublime.set_timeout(self.revert_active_window, 50)
def revert_active_window(self):
sublime.active_window().active_view().run_command("revert")