/
CodeFormatter.py
168 lines (120 loc) · 5.27 KB
/
CodeFormatter.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
# @author Avtandil Kikabidze
# @copyright Copyright (c) 2008-2015, Avtandil Kikabidze aka LONGMAN (akalongman@gmail.com)
# @link http://longman.me
# @license The MIT License (MIT)
import os, sys, sublime, sublime_plugin
st_version = 2
if sublime.version() == '' or int(sublime.version()) > 3000:
st_version = 3
reloader_name = 'codeformatter.reloader'
# ST3 loads each package as a module, so it needs an extra prefix
if st_version == 3:
reloader_name = 'CodeFormatter.' + reloader_name
from imp import reload
if reloader_name in sys.modules:
reload(sys.modules[reloader_name])
try:
# Python 3
from .codeformatter import reloader
from .codeformatter.formatter import Formatter
except (ValueError):
# Python 2
from codeformatter import reloader
from codeformatter.formatter import Formatter
# fix for ST2
cprint = globals()["__builtins__"]["print"]
debug_mode = False
def plugin_loaded():
cprint('CodeFormatter: Plugin Initialized')
settings = sublime.load_settings('CodeFormatter.sublime-settings')
debug_mode = settings.get('codeformatter_debug', False)
#if debug_mode:
#from pprint import pprint
#pprint(settings)
#debug_write("Debug mode enabled")
#debug_write("Platform "+sublime.platform()+" "+sublime.arch())
#debug_write("Sublime Version "+sublime.version())
#debug_write("Settings "+pprint(settings))
if (sublime.platform() != "windows"):
import stat
path = sublime.packages_path()+"/CodeFormatter/codeformatter/lib/phpbeautifier/fmt.phar"
st = os.stat(path)
os.chmod(path, st.st_mode | stat.S_IEXEC)
if st_version == 2:
plugin_loaded()
class CodeFormatterCommand(sublime_plugin.TextCommand):
def run(self, edit, syntax=False, saving=False):
if self.view.is_scratch():
return show_error("File is scratch")
file_name = self.view.file_name()
# if not file_name:
# return show_error("File does not exist.")
# if not os.path.exists(file_name):
# return show_error("File "+file_name+" does not exist.")
formatter = Formatter(self.view, file_name, syntax, saving)
if not formatter.exists():
if saving:
return False
return show_error("Formatter for this file type ("+formatter.syntax+") not found.")
if (saving and not formatter.formatOnSaveEnabled()):
return False
file_text = sublime.Region(0, self.view.size())
file_text_utf = self.view.substr(file_text).encode('utf-8')
if (len(file_text_utf) == 0):
return show_error("No code found.")
stdout, stderr = formatter.format(file_text_utf)
if len(stderr) == 0 and len(stdout) > 0:
self.view.replace(edit, file_text, stdout)
else:
show_error("Format error:\n"+stderr)
class CodeFormatterEventListener(sublime_plugin.EventListener):
def on_pre_save(self, view):
args = {}
args['saving'] = True
view.run_command('code_formatter', args)
class CodeFormatterShowPhpTransformationsCommand(sublime_plugin.TextCommand):
def run(self, edit, syntax=False):
import subprocess, re
platform = sublime.platform()
settings = sublime.load_settings('CodeFormatter.sublime-settings')
opts = settings.get('codeformatter_php_options')
php_path = "php"
if ("php_path" in opts and opts["php_path"]):
php_path = opts["php_path"]
cmd = []
cmd.append(str(php_path))
cmd.append(sublime.packages_path()+"/CodeFormatter/codeformatter/lib/phpbeautifier/fmt.phar")
cmd.append("--list")
stderr = ""
stdout = ""
try:
if (platform == "windows"):
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
startupinfo.wShowWindow = subprocess.SW_HIDE
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, startupinfo=startupinfo, shell=False, creationflags=subprocess.SW_HIDE)
else:
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
except Exception as e:
stderr = str(e)
if (not stderr and not stdout):
stderr = "Error while gethering list of php transformations"
if len(stderr) == 0 and len(stdout) > 0:
text = stdout.decode('utf-8')
text = re.sub("Usage:.*?PASSNAME", "Available PHP Tranformations:", text)
window = self.view.window()
pt = window.get_output_panel("paneltranformations")
pt.set_read_only(False)
pt.insert(edit, pt.size(), text)
window.run_command("show_panel", {"panel": "output.paneltranformations"})
else:
show_error("Formatter error:\n"+stderr)
def console_write(text, prefix=False):
if prefix:
sys.stdout.write('CodeFormatter: ')
sys.stdout.write(text+"\n")
def debug_write(text, prefix=False):
console_write(text, True)
def show_error(text):
sublime.error_message(u'CodeFormatter\n\n%s' % text)