-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
286 lines (225 loc) · 7.96 KB
/
main.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import libcst as cst
from functools import singledispatchmethod, singledispatch
from contextlib import contextmanager
from pyweb import pydom
from pyodide.ffi.wrappers import add_event_listener
from pyscript.js_modules.monaco import editor
from pyscript import document
from pyscript.ffi import create_proxy, to_js
from js import localStorage, Array
DEFAULT_CONTENT = """
def foo[T](bar: T | None = None) -> T | None:
if bar is None:
return None
# increment
return bar + 1
"""
EDITOR_CONTENT_KEY = "libcst-me-editor-content"
RENDER_BODY_ELEMENT_ID = "render-body"
MODULE = None
POS = {}
IDDICT: dict[cst.CSTNode, str] = {}
IGNORE_NODES = (cst.TrailingWhitespace, cst.SimpleWhitespace, cst.Newline)
def render_cst(editor) -> None:
content = editor.getModel().getValue()
localStorage.setItem(EDITOR_CONTENT_KEY, content)
global MODULE, POS, IDDICT
wrapper = cst.metadata.MetadataWrapper(cst.parse_module(content))
POS = {
k: v
for k, v in wrapper.resolve(cst.metadata.PositionProvider).items()
if not isinstance(k, IGNORE_NODES)
}
MODULE = wrapper.module
IDDICT = dict(node2id(MODULE, "$"))
render_module_object(
editor, MODULE, document.getElementById(RENDER_BODY_ELEMENT_ID)
)
last_decorations = None
def did_cst_click(event, position, editor) -> None:
event.stopPropagation()
range = {
"startLineNumber": position.start.line,
"startColumn": position.start.column + 1,
"endLineNumber": position.end.line,
"endColumn": position.end.column + 1,
}
remove_highlights()
global last_decorations
last_decorations = editor.createDecorationsCollection(
Array.from_([to_js({"options": {"className": "hghlight"}, "range": range})])
)
def remove_highlights() -> None:
global last_decorations
if last_decorations is not None:
last_decorations.clear()
if (highlights := pydom[".hghlight"]) is not None:
for item in highlights:
item.remove_class("hghlight")
def scroll_to_cst_node(editor) -> None:
position = editor.getPosition()
line = position.lineNumber
column = position.column - 1
node_id = get_node_id_by_pos(line, column)
node_dom = pydom.Element(document.getElementById(node_id))
remove_highlights()
node_dom._js.scrollIntoView(to_js({"behavior": "smooth", "block": "center"}))
node_dom.add_class("hghlight")
document.getElementById("status-bar").innerText = f"Line: {line}, Column: {column}"
document.getElementById("render-footer").innerText = f"id: {node_id}"
def get_default_content() -> str:
content = localStorage.getItem(EDITOR_CONTENT_KEY)
return content or DEFAULT_CONTENT
def get_node_id_by_pos(line, column) -> str:
render_pannel = document.getElementById(RENDER_BODY_ELEMENT_ID)
node = find_closes_node_to_pos(line, column)
return IDDICT[node]
####
@singledispatch
def node2id(node, id_prefix):
yield from []
@node2id.register(list)
@node2id.register(set)
@node2id.register(tuple)
def _(e: list, id_prefix: str):
for n, elem in enumerate(e):
yield from node2id(elem, f"{id_prefix}[{n}]")
# yield self.text("".join(self.display_as_html(elem, )))
@node2id.register(cst.CSTNode)
def _(node, id_prefix: str):
id_prefix = f"{id_prefix}({type(node).__name__})"
yield node, id_prefix
for attr in node.__dataclass_fields__:
yield from [
inner
for inner in node2id(getattr(node, attr), id_prefix=f"{id_prefix}.{attr}")
if inner
]
def find_closes_node_to_pos(target_line: int, target_column: int):
global POS
best_line_so_far = -1
best_column_so_far = -1
best_node = None
for node, v in sorted(
POS.items(),
key=lambda x: (
x[1].start.line,
x[1].start.column,
-x[1].end.line,
-x[1].end.column,
),
):
if v.start.line > target_line:
break
if v.start.line > best_line_so_far:
best_node = node
best_line_so_far = v.start.line
best_column_so_far = -1
continue
if v.start.column > target_column:
continue
if v.start.column >= best_column_so_far:
best_node = node
best_column_so_far = v.start.column
continue
return best_node
####
def kill_whitespace():
global POS, IDDICT
list(
map(
POS.pop,
[
pos
for pos in POS
if isinstance(pos, cst.SimpleWhitespace) or isinstance(pos, cst.Newline)
],
)
)
list(
map(
IDDICT.pop, [pos for pos in IDDICT if isinstance(pos, cst.SimpleWhitespace)]
)
)
class DisplayNodes:
def __init__(self, editor, root_node):
self.editor = editor
self.root_node = root_node
self.indent_level = 0
self.indent_str = " " * 2
def as_html(self):
yield from self.display_as_html(self.root_node, id_prefix="$")
def text(self, msg):
return (self.indent_str * self.indent_level) + msg
@contextmanager
def indent(self):
self.indent_level += 1
yield
self.indent_level -= 1
@singledispatchmethod
def display_as_html(self, e, id_prefix: str):
if e == None:
yield pydom.create("span", classes=("pynone",), html="None")
else:
yield pydom.create("span", html=repr(e))
@display_as_html.register(str)
def _(self, e, id_prefix: str):
yield pydom.create("span", classes=("pystr",), html=repr(e))
@display_as_html.register(int)
@display_as_html.register(float)
def _(self, e, id_prefix: str):
yield pydom.create("span", classes=("pynum",), html=repr(e))
@display_as_html.register(list)
@display_as_html.register(set)
@display_as_html.register(tuple)
def _(self, e: list, id_prefix: str):
o, c = str(type(e)())
if not len(e):
yield pydom.create("span", html=f"{o}{c}")
return
yield pydom.create("span", html=f"{o}\n{self.text('')}")
with self.indent():
for n, elem in enumerate(e):
for child in self.display_as_html(elem, f"{id_prefix}[{n}]"):
yield child
yield pydom.create("span", html=",\n")
yield pydom.create("span", html=self.text(c))
@display_as_html.register(cst.CSTNode)
def _(self, e, id_prefix: str):
global MODULE, POS, IDDICT
id_ = f"{id_prefix}.{type(e).__name__}"
pos = POS.get(e)
div = pydom.create("div", classes=("cstnode",))
div.id = IDDICT[e]
if pos:
add_event_listener(
div._js, "click", lambda ev: did_cst_click(ev, pos, self.editor)
)
div.create("span", classes=("cstnodename",), html=type(e).__name__)
div.create("span", html="(\n")
with self.indent():
for attr in e.__dataclass_fields__:
if attr == "__slots__":
continue
div.create("span", html=f"{self.text(attr)}=")
for child in self.display_as_html(getattr(e, attr), id_prefix=id_):
div.append(child)
div.create("span", html=",\n")
div.create("span", html=self.text(")"))
yield div
def render_module_object(editor, module, output):
pre = pydom.create("pre")
for node in DisplayNodes(editor, module).as_html():
pre.append(node)
output.innerHTML = ""
output.appendChild(pre._js)
document.getElementById("libcst-version").innerHTML = f"(LibCST {cst._version.version})"
ed = editor.create(
document.getElementById("editor"),
to_js(
{"language": "python", "automaticLayout": True, "value": get_default_content()}
),
)
ed.onDidChangeModelContent(create_proxy(lambda _: render_cst(ed)))
ed.onDidChangeCursorPosition(create_proxy(lambda _: scroll_to_cst_node(ed)))
render_cst(ed)