forked from aerobounce/Sublime-Pretty-Shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrettyShell.py
executable file
·240 lines (204 loc) · 7.58 KB
/
PrettyShell.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
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
#
# PrettyShell.py
#
# AGPLv3 License
# Created by github.com/aerobounce on 2019/11/18.
# Copyright © 2019-2022, aerobounce. All rights reserved.
#
from html import escape
from re import compile
from subprocess import PIPE, Popen
from sublime import LAYOUT_BELOW, Edit, Phantom, PhantomSet, Region, View
from sublime import error_message as alert
from sublime import load_settings, packages_path
from sublime_plugin import TextCommand, ViewEventListener
SETTINGS_FILENAME = "Pretty Shell.sublime-settings"
ON_CHANGE_TAG = "reload_settings"
UTF_8 = "utf-8"
PHANTOM_STYLE = """
<style>
div.error-arrow {
border-top: 0.4rem solid transparent;
border-left: 0.5rem solid color(var(--redish) blend(var(--background) 30%));
width: 0;
height: 0;
}
div.error {
padding: 0.4rem 0 0.4rem 0.7rem;
margin: 0 0 0.2rem;
border-radius: 0 0.2rem 0.2rem 0.2rem;
}
div.error span.message {
padding-right: 0.7rem;
}
div.error a {
text-decoration: inherit;
padding: 0.35rem 0.7rem 0.45rem 0.8rem;
position: relative;
bottom: 0.05rem;
border-radius: 0 0.2rem 0.2rem 0;
font-weight: bold;
}
html.dark div.error a {
background-color: #00000018;
}
html.light div.error a {
background-color: #ffffff18;
}
</style>
"""
def plugin_loaded():
PrettyShell.settings = load_settings(SETTINGS_FILENAME)
PrettyShell.reload_settings()
PrettyShell.settings.add_on_change(ON_CHANGE_TAG, PrettyShell.reload_settings)
def plugin_unloaded():
PrettyShell.settings.clear_on_change(ON_CHANGE_TAG)
class PrettyShell:
settings = load_settings(SETTINGS_FILENAME)
phantom_sets = {}
shell_command = ""
shell_cwd = ""
format_on_save = True
show_error_inline = True
scroll_to_error_point = True
@classmethod
def reload_settings(cls):
cls.format_on_save = cls.settings.get("format_on_save")
cls.show_error_inline = cls.settings.get("show_error_inline")
cls.scroll_to_error_point = cls.settings.get("scroll_to_error_point")
simplify = cls.settings.get("simplify")
minify = cls.settings.get("minify")
language = cls.settings.get("language")
indent = cls.settings.get("indent")
binop = cls.settings.get("binop")
switchcase = cls.settings.get("switchcase")
rediop = cls.settings.get("rediop")
align = cls.settings.get("align")
fnbrace = cls.settings.get("fnbrace")
cls.shell_command = cls.settings.get("shfmt_bin_path")
if simplify:
cls.shell_command += " -s"
if minify:
cls.shell_command += " -mn"
if language:
cls.shell_command += ' -ln "{}"'.format(language)
if indent:
cls.shell_command += " -i {}".format(indent)
if binop:
cls.shell_command += " -bn"
if switchcase:
cls.shell_command += " -ci"
if rediop:
cls.shell_command += " -sr"
if align:
cls.shell_command += " -kp"
if fnbrace:
cls.shell_command += " -fn"
# Note: For Windows only, UNC path error workaround.
# ("CMD does not support UNC paths as current directories.")
# This may not be needed in Sublime Text 4.
#
# Popen needs non UNC `cwd` to be specified.
# It seems `cwd` can be any path as long as it's a non UNC path
cls.shell_cwd = packages_path()
@classmethod
def update_phantoms(cls, view: View, stderr: str, error_point: int):
view_id = view.id()
if not view_id in cls.phantom_sets:
cls.phantom_sets[view_id] = PhantomSet(view, str(view_id))
# Create Phantom
def phantom_content():
return (
"<body id=inline-error>"
+ PHANTOM_STYLE
+ '<div class="error-arrow"></div><div class="error">'
+ '<span class="message">'
+ escape(stderr, quote=False)
+ "</span>"
+ "<a href=hide>"
+ chr(0x00D7)
+ "</a></div>"
+ "</body>"
)
new_phantom = Phantom(
Region(error_point, view.line(error_point).b),
phantom_content(),
LAYOUT_BELOW,
lambda _: view.erase_phantoms(str(view_id)),
)
# Store Phantom
cls.phantom_sets[view_id].update([new_phantom])
@staticmethod
def parse_error_point(view: View, stderr: str):
digits = compile(r"\d+|$").findall(stderr)
if not stderr or not digits[0]:
return
line = int(digits[0]) - 1
column = int(digits[1]) - 1
return view.text_point(line, column)
@classmethod
def execute_format(cls, view: View, edit: Edit):
# Get entire string
entire_region = Region(0, view.size())
entire_text = view.substr(entire_region)
# Early return
if not entire_text:
return
# Execute shell and get output
with Popen(cls.shell_command, cwd=cls.shell_cwd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE) as popen:
# Nil check to suppress linter
if not popen.stdin or not popen.stdout or not popen.stderr:
return
# Write target_text into stdin and ensure the descriptor is closed
popen.stdin.write(entire_text.encode(UTF_8))
popen.stdin.close()
# Read stdout and stderr
stdout = popen.stdout.read().decode(UTF_8)
stderr = popen.stderr.read().decode(UTF_8)
stderr = stderr.replace("<standard input>:", "")
stderr = stderr.replace("\n", "")
# Print command executed to the console
print("[Pretty Shell] Popen:", cls.shell_command)
# Present alert for 'command not found'
if "command not found" in stderr:
alert("Pretty Shell\n" + stderr)
return
# Parse possible error point
error_point = cls.parse_error_point(view, stderr)
# Present alert for other errors
if stderr and not error_point:
alert("Pretty Shell\n" + stderr)
return
# Print parsing error
if error_point:
print("[Pretty Shell]", stderr)
# Store original viewport position
original_viewport_position = view.viewport_position()
# Replace with stdout only if stderr is empty
if stdout and not stderr:
view.replace(edit, entire_region, stdout)
# Update Phantoms
view.erase_phantoms(str(view.id()))
if cls.show_error_inline and error_point:
cls.update_phantoms(view, stderr, error_point)
# Scroll to the syntax error point
if cls.scroll_to_error_point and error_point:
view.sel().clear()
view.sel().add(Region(error_point))
view.show_at_center(error_point)
else:
# Restore viewport position
view.set_viewport_position((0, 0), False)
view.set_viewport_position(original_viewport_position, False)
class PrettyShellCommand(TextCommand):
def run(self, edit):
PrettyShell.execute_format(self.view, edit)
class PrettyShellListener(ViewEventListener):
def on_pre_save(self):
if PrettyShell.format_on_save:
if "Bash" in self.view.settings().get("syntax"):
self.view.run_command("pretty_shell")
def on_close(self):
view_id = self.view.id()
if view_id in PrettyShell.phantom_sets:
PrettyShell.phantom_sets.pop(view_id)