-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
364 lines (325 loc) · 10.3 KB
/
script.js
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
let tree, deMOSSables = []
const codeInput0 = document.getElementById('code-input-0')
const codeInput = document.getElementById('code-input')
const outputContainer = document.getElementById('output-container')
const outputContainerScroll = document.getElementById('output-container-scroll')
const updateTimeSpan = document.getElementById('update-time')
// outputContainerScroll.hidden = false
;
(async () => {
const CAPTURE_REGEX = /@\s*([\w\._-]+)/g
loadState()
await TreeSitter.init()
window.parser = new TreeSitter()
const config = {
lineNumbers: true,
showCursorWhenSelecting: true,
mode: 'text/x-c++src'
}
window.codeEditor0 = CodeMirror.fromTextArea(codeInput0, config)
window.codeEditor = CodeMirror.fromTextArea(codeInput, config)
const cluster = new Clusterize({
rows: [],
noDataText: null,
contentElem: outputContainer,
scrollElem: outputContainerScroll
})
const renderTreeOnCodeChange = debounce(renderTree, 50)
const saveStateOnChange = debounce(saveState, 2000)
const runTreeQueryOnChange = debounce(runTreeQuery, 50)
let treeRows = null
let treeRowHighlightedIndex = -1
let parseCount = 0
let isRendering = 0
window.query = null
codeEditor0.on('changes', handleCodeChange)
codeEditor.on('changes', handleCodeChange)
codeEditor.on('viewportChange', runTreeQueryOnChange)
codeEditor.on('cursorActivity', debounce(handleCursorMovement, 150))
outputContainer.addEventListener('click', handleTreeClick)
tree = null
parser.setLanguage(await TreeSitter.Language.load("tree-sitter-cpp.wasm"))
handleCodeChange()
async function handleCodeChange(editor, changes) {
const newText = codeEditor.getValue() + '\n'
const edits = tree && changes && changes.map(treeEditForEditorChange)
// const start = performance.now()
if (edits)
for (const edit of edits)
tree.edit(edit)
const newTree = parser.parse(newText, tree)
// console.log(newTree)
// const duration = (performance.now() - start).toFixed(1)
// updateTimeSpan.innerText = `${duration} ms`
if (tree) tree.delete()
tree = newTree
parseCount++
renderTreeOnCodeChange()
runTreeQueryOnChange()
saveStateOnChange()
}
async function renderTree() {
isRendering++
const cursor = tree.walk()
let currentRenderCount = parseCount
let row = ''
let rows = []
let finishedRow = false
let visitedChildren = false
let indentLevel = 0
let rowCount = 0
for (let i = 0; ; i++) {
if (i > 0 && i % 10000 === 0) {
await new Promise(r => setTimeout(r, 0))
if (parseCount !== currentRenderCount) {
cursor.delete()
isRendering--
return
}
}
let displayName = cursor.nodeType
if (cursor.nodeIsMissing) cursor.nodeType = `MISSING ${cursor.nodeType}`
// else if (cursor.nodeIsNamed) {
// displayName = cursor.nodeType
// }
if (visitedChildren) {
if (displayName) {
finishedRow = true
}
if (cursor.gotoNextSibling()) {
visitedChildren = false
} else if (cursor.gotoParent()) {
visitedChildren = true
indentLevel--
} else {
break
}
} else {
if (displayName) {
if (finishedRow) {
// row += '</div>'
rows.push(row)
finishedRow = false
}
const start = cursor.startPosition
const end = cursor.endPosition
const id = cursor.nodeId
let fieldName = cursor.currentFieldName()
if (fieldName) {
fieldName += ': '
} else {
fieldName = ''
}
// row = `<div>${' '.repeat(indentLevel)}${fieldName}<a class='plain' href="#" data-id=${id} data-range="${start.row},${start.column},${end.row},${end.column}">${displayName}</a>`
if (rowCount != start.row) {
row = "<hr>"
rowCount = start.row
} else {
row = "<br>"
}
row += `${' '.repeat(indentLevel)}`
if (cursor.nodeIsNamed)
row += `${fieldName}`
else
row += `<i>${fieldName}</i>`
row += `<a class='plain' data-id=${id} data-range="${start.row},${start.column},${end.row},${end.column}">${displayName}</a>`
// row += `${codeEditor.getDoc().getRange({ line: start.row, ch: start.column }, { line: end.row, ch: end.column })}`
finishedRow = true
}
if (cursor.gotoFirstChild()) {
visitedChildren = false
indentLevel++
} else {
visitedChildren = true
}
}
}
if (finishedRow) {
// row += '</div>'
row += "<hr>"
rows.push(row)
}
cursor.delete()
cluster.update(rows)
treeRows = rows
isRendering--
handleCursorMovement()
}
function handleCursorMovement() {
if (isRendering) return
const selection = codeEditor.doc.listSelections()[0]
let start = { row: selection.anchor.line, column: selection.anchor.ch }
let end = { row: selection.head.line, column: selection.head.ch }
if (
start.row > end.row ||
(
start.row === end.row &&
start.column > end.column
)
) {
let swap = end
end = start
start = swap
}
const node = tree.rootNode.descendantForPosition(start, end)
if (treeRows) {
if (treeRowHighlightedIndex !== -1) {
const row = treeRows[treeRowHighlightedIndex]
if (row) treeRows[treeRowHighlightedIndex] = row.replace('highlighted', 'plain')
}
treeRowHighlightedIndex = treeRows.findIndex(row => row.includes(`data-id=${node.id} `))
if (treeRowHighlightedIndex !== -1) {
const row = treeRows[treeRowHighlightedIndex]
// codeEditorMark(/\d+,\d+,\d+,\d/.exec(row)[0])
if (row) treeRows[treeRowHighlightedIndex] = row.replace('plain', 'highlighted')
}
cluster.update(treeRows)
const lineHeight = cluster.options.item_height
const scrollTop = outputContainerScroll.scrollTop
const containerHeight = outputContainerScroll.clientHeight
const offset = treeRowHighlightedIndex * lineHeight
if (scrollTop > offset - 20) {
$(outputContainerScroll).animate({ scrollTop: offset - 20 }, 150)
} else if (scrollTop < offset + lineHeight + 40 - containerHeight) {
$(outputContainerScroll).animate({ scrollTop: offset - containerHeight + 40 }, 150)
}
}
}
function handleTreeClick(event) {
if (event.target.tagName === 'A') {
event.preventDefault()
const [startRow, startColumn, endRow, endColumn] =
event.target.dataset.range.split(',').map(n => parseInt(n))
codeEditor.focus()
codeEditor.setSelection(
{ line: startRow, ch: startColumn },
{ line: endRow, ch: endColumn }
)
}
}
function treeEditForEditorChange(change) {
const oldLineCount = change.removed.length
const newLineCount = change.text.length
const lastLineLength = change.text[newLineCount - 1].length
const startPosition = { row: change.from.line, column: change.from.ch }
const oldEndPosition = { row: change.to.line, column: change.to.ch }
const newEndPosition = {
row: startPosition.row + newLineCount - 1,
column: newLineCount === 1
? startPosition.column + lastLineLength
: lastLineLength
}
const startIndex = codeEditor.indexFromPos(change.from)
let newEndIndex = startIndex + newLineCount - 1
let oldEndIndex = startIndex + oldLineCount - 1
for (let i = 0; i < newLineCount; i++) newEndIndex += change.text[i].length
for (let i = 0; i < oldLineCount; i++) oldEndIndex += change.removed[i].length
return {
startIndex, oldEndIndex, newEndIndex,
startPosition, oldEndPosition, newEndPosition
}
}
function loadState() {
const sourceCode0 = localStorage.getItem("sourceCode0")
if (sourceCode0 != null) codeInput0.value = sourceCode0
const sourceCode = localStorage.getItem("sourceCode")
if (sourceCode != null) codeInput.value = sourceCode
}
function saveState() {
localStorage.setItem("sourceCode0", codeEditor0.getValue())
localStorage.setItem("sourceCode", codeEditor.getValue())
}
function debounce(func, wait, immediate) {
var timeout
return function () {
var context = this, args = arguments
var later = function () {
timeout = null
if (!immediate) func.apply(context, args)
}
var callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
}
function runTreeQuery() {
for (let m of codeEditor.getAllMarks()) m.clear()
for (const w of deMOSSables) w.widget.clear()
deMOSSables = []
$("#status").text("")
if (!enable_demosser.checked) return
let i = -1
const viewport = codeEditor.getViewport()
for (let x of tricks) {
const expected_param_count = x.match.match(/@\w+/g).length
if (query) {
query.delete()
query.deleted = true
query = null
}
// try {
query = parser.getLanguage().query(x.match)
const captures = query.captures(
tree.rootNode,
{ row: viewport.from, column: 0 },
{ row: viewport.to, column: 0 },
)
let names = {}
let count = 0
for (let k = captures.length - 1; k >= 0; k--) {
names[captures[k].name] = captures[k].node.text
count++
if (captures[k].name == 'match') {
if (count == expected_param_count) {
const { startPosition, endPosition } = captures[k].node
const css = `background: hsla(${i++ * (1e7 + 1) % 360}, 100%, 90%, 50%)`;
const trick = {
trick: x,
start: { line: startPosition.row, ch: startPosition.column },
end: { line: endPosition.row, ch: endPosition.column },
names: names,
widget: codeEditor.addLineWidget(
startPosition.row,
$(`<button style="${css}" onclick="applyTrick(${deMOSSables.length})">${x.name}</button>`)[0],
{ above: true }
)
}
codeEditor.markText(
trick.start, trick.end,
{ css: css, attributes: { title: demossText(trick) } }
)
deMOSSables.push(trick)
}
names = {}
count = 0
}
}
// } catch (error) {
// }
}
$("#status").text(`${deMOSSables.length} changes can be made.`)
}
})()
function applyTrick(index) {
const x = deMOSSables[index]
let text = demossText(x);
codeEditor.operation(e => {
codeEditor.replaceRange(text, x.start, x.end)
codeEditor.setSelection(x.start, codeEditor.posFromIndex(codeEditor.indexFromPos(x.start) + text.length))
})
x.widget.clear()
}
function demossText(x) {
let text
if (typeof x.trick.replace == 'string') {
text = x.trick.replace;
for (const name in x.names)
text = text.replace("@" + name, x.names[name]);
} if (typeof x.trick.replace == 'object') {
text = codeEditor.getRange(x.start, x.end);
for (const item in x.trick.replace)
text = text.replace(x.names[item], x.trick.replace[item]);
}
return text.trim();
}