-
Notifications
You must be signed in to change notification settings - Fork 1
/
juno.js
291 lines (257 loc) · 9.19 KB
/
juno.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
const commands = require(process.env.HOME + "/.atom/packages/julia-client/lib/package/commands")
const {runtime,misc,connection} = require(process.env.HOME + "/.atom/packages/julia-client")
const Highlighter = require(process.env.HOME + "/.atom/packages/julia-client/lib/ui/highlighter.coffee")
const DOM = require(process.env.HOME + "/.kip/repos/jkroso/DOM.jl/runtime.js")
atom.commands.add(".item-views > atom-text-editor", {
"julia-client:eval-block": (event) => {
atom.commands.dispatch(event.currentTarget, "autocomplete-plus:cancel")
return commands.withInk(() => {
connection.boot()
return eval_block()
})
},
"julia-client:eval-each": (event) => {
atom.commands.dispatch(event.currentTarget, "autocomplete-plus:cancel")
return commands.withInk(() => {
connection.boot()
return eval_each()
})
},
"julia-client:reset-module": () => {
connection.boot()
const {edpath} = runtime.evaluation._currentContext()
connection.client.ipc.msg("reset module", edpath)
},
"julia-client:focus-result": () => {
const {editor} = runtime.evaluation._currentContext()
const cursors = misc.blocks.get(editor)
if (cursors.length > 1) throw Error("Can't focus multiple results")
const {range} = cursors[0]
const [[start], [end]] = range
const results = runtime.evaluation.ink.Result.forLines(editor, start, end)
if (results.length > 1) throw Error("That selection has multiple results associated with it")
if (results.length == 0) return
const r = results[0]
r.isfocused || r.focus_trap.focus()
}
})
var style = document.createElement("style")
document.head.appendChild(style)
connection.client.ipc.handle("stylechange", (data) => {
const node = DOM.create(data)
style.replaceWith(node)
style = node
})
const eventJSON = (e, top_node) => event_converters[e.type](e, top_node)
const modifiers = (e) => {
const mods = []
if (e.altKey) mods.push("alt")
if (e.ctrlKey) mods.push("ctrl")
if (e.shiftKey) mods.push("shift")
if (e.metaKey) mods.push("meta")
return mods
}
const mouse_button_event = (e, top_node) => ({
type: e.type,
path: dom_path(e.target, top_node),
button: e.button,
position: [e.x, e.y]
})
const mouse_hover_event = (e, top_node) => ({
type: e.type,
path: dom_path(e.target, top_node)
})
const dom_path = (dom, top_node) => top_node.contains(dom) ? DOM.dom_path(dom, top_node) : []
const event_converters = {
click: mouse_button_event,
dblclick: mouse_button_event,
mousedown: mouse_button_event,
mouseup: mouse_button_event,
mouseover: mouse_hover_event,
mouseout: mouse_hover_event,
resize() {
return {
type: "resize",
width: window.innerWidth,
height: window.innerHeight,
}
},
scroll(e, top_node) {
return {
type: "scroll",
path: dom_path(e.target, top_node),
position: [window.scrollX, window.scrollY]
}
},
mousemove(e, top_node) {
return {
type: "mousemove",
path: dom_path(e.target, top_node),
position: [e.x, e.y]
}
}
}
const results = {}
var id = 0
const eval_block = () => {
const ctx = runtime.evaluation._currentContext()
const results = misc.blocks.get(ctx.editor).map((x)=>create_result(x, ctx))
return connection.client.ipc.rpc("rutherford eval", results)
}
const create_result = ({range, line, text}, {editor, mod, edpath}) => {
const [[start], [end]] = range
const r = new runtime.evaluation.ink.Result(editor, [start, end], {type: "inline", scope: "julia"})
const top_node = result_container()
r.view.view.replaceWith(top_node)
r.view.view = top_node
runtime.evaluation.ink.highlight(editor, start, end)
const _id = id += 1
results[_id] = r
const onDidDestroy = () => {
if (!(_id in results)) return
delete results[_id]
connection.client.ipc.msg("result done", _id)
}
r.onDidDestroy(onDidDestroy)
editor.onDidDestroy(onDidDestroy)
return {text, line: line+1, path: edpath, id: _id}
}
const eval_each = () => {
const ctx = runtime.evaluation._currentContext()
const src = ctx.editor.getBuffer().getText()
const cursors = misc.blocks.get(ctx.editor)
if (cursors.length == 0) cursors.push({range:[[0,0],[0,null]]})
return Promise.all(cursors.map(({range}) =>
connection.client.ipc.rpc("getblocks", range, ctx.edpath, src)
.then((blocks) => blocks.map((x)=>create_result(x, ctx)))
.then((results) => connection.client.ipc.rpc("rutherford eval", results))
))
}
// creating it ourselves because ink attaches event handlers to their one
const result_container = () => {
let el = document.createElement("div")
el.setAttribute("tabindex", "-1")
el.classList.add("ink", "result", "inline", "julia", "ink-hide", "loading")
setTimeout(() => el.classList.remove("ink-hide"), 20)
el.appendChild(loading_gear.cloneNode())
el.style.minHeight = "1.5em"
el.style.maxHeight = window.innerHeight/2 + "px"
return el
}
const loading_gear = document.createElement("span")
loading_gear.classList.add("loading", "icon", "icon-gear")
const notifications = {}
connection.client.ipc.handle("test passed", ({line}) => {
if (line in notifications) {
notifications[line].dismiss()
}
})
connection.client.ipc.handle("test failed", ({line, path}) => {
const n = atom.notifications.addInfo(`Test failed in ${path}:${line}`, {
dismissable: true,
buttons: [{
onDidClick: () => {
runtime.workspace.ink.Opener.open(path, line-1)
},
text: "Goto"
}]
})
if (line in notifications) notifications[line].dismiss()
n.onDidDismiss(()=>{delete notifications[line]})
notifications[line] = n
})
connection.client.ipc.handle("render", ({state, dom, id}) => {
const r = results[id]
if (r == undefined) return
r.view.toolbarView.classList.remove("hide")
var top_node = r.view.view
top_node.addEventListener("mousewheel", (e) => {
var node = e.target
while (node != top_node) {
if ((node.offsetHeight != node.scrollHeight || node.offsetWidth != node.scrollWidth) &&
((e.deltaY > 0 && node.scrollHeight - node.scrollTop > node.clientHeight) ||
(e.deltaY < 0 && node.scrollTop > 0) ||
(e.deltaX > 0 && node.scrollWidth - node.scrollLeft > node.clientWidth) ||
(e.deltaX < 0 && node.scrollLeft > 0))) {
e.stopPropagation()
break
}
node = node.parentNode
}
}, true)
r.focus_trap = document.createElement('input')
r.focus_trap.setAttribute("type", "text")
r.focus_trap.style.position = "absolute"
r.focus_trap.style.opacity = "0"
r.focus_trap.style.zIndex = "-999"
r.focus_trap.style.width = "0"
r.focus_trap.style.height = "0"
const sendKeyEvent = (e) => {
connection.client.ipc.msg("event", id, {type: e.type, key: e.key, modifiers: modifiers(e)})
e.preventDefault()
e.stopPropagation()
}
r.focus_trap.addEventListener("keydown", sendKeyEvent, true)
r.focus_trap.addEventListener("keyup", sendKeyEvent, true)
r.focus_trap.addEventListener("keypress", (e) => {
e.preventDefault()
e.stopPropagation()
}, true)
r.prev_focus = null
r.isfocused = false
r.focus_trap.addEventListener("focusout", (e) => {
top_node.style.boxShadow = ""
r.isfocused = false
}, true)
r.focus_trap.addEventListener("focusin", (e) => {
top_node.style.boxShadow = "0px 0px 3px #1f96ff"
r.prev_focus = e.relatedTarget
r.isfocused = true
}, true)
top_node.addEventListener("mousedown", (e) => {
r.isfocused || r.focus_trap.focus()
}, true)
top_node.addEventListener("keydown", (e) => {
if (e.key == "Escape" && r.prev_focus) r.prev_focus.focus()
}, true)
const sendEvent = (e) => {
connection.client.ipc.msg("event", id, eventJSON(e, top_node.lastChild))
e.preventDefault()
e.stopPropagation()
}
for (name in event_converters) {
top_node.addEventListener(name, sendEvent, true)
}
top_node.classList.toggle("error", state == "error")
top_node.classList.remove("loading")
top_node.replaceChild(r.focus_trap, top_node.lastChild)
top_node.appendChild(DOM.create(dom))
runtime.workspace.update()
})
connection.client.ipc.handle("patch", ({id, patch, state}) => {
const top_node = results[id].view.view
top_node.classList.toggle("error", state == "error")
DOM.patch(patch, top_node.lastChild)
})
connection.client.ipc.handle("AsyncNode", ({id, value}) => {
document.getElementById(String(id)).replaceWith(DOM.create(value))
})
connection.client.ipc.handle("currentfile", () => {
const {mod, edpath} = runtime.evaluation._currentContext()
return edpath
})
connection.client.ipc.handle("edit", (src, line, id) => {
const {editor} = runtime.evaluation._currentContext()
const result = results[id]
result.text = src // prevent invalidation
const marker = result.marker
const range = marker.getBufferRange()
editor.setTextInBufferRange(range, src)
})
const grammar_remap = {"source.jldoctest": "source.julia.console"}
connection.client.ipc.handle("highlight", ({src, grammar, block}) => {
if (grammar in grammar_remap) grammar = grammar_remap[grammar]
grammar = atom.grammars.grammarForScopeName(grammar) || atom.grammars.grammarForScopeName("text.plain")
return Highlighter.highlight(src, grammar, {scopePrefix: 'syntax--', block})
})
connection.client.ipc.handle("config", (key) => atom.config.get(key))