-
Notifications
You must be signed in to change notification settings - Fork 1
/
draw.jl
523 lines (459 loc) · 17.9 KB
/
draw.jl
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
@use "github.com/jkroso" [
"DOM.jl" => DOM @dom @css_str add_attr [
"ansi.jl" ansi
"html.jl"]
"Prospects.jl" assoc interleave
"Destructure.jl" @destruct
"Source.jl" source
"DynamicVar.jl" @dynamic!]
@use "." @component Context path data stop intent context
@use "./markdown" renderMD
@use CodeTools
@use Atom
using InteractiveUtils
import Markdown
import Dates
"render data specifically for a certain context and/or intent"
draw(data) = draw(intent[], context[], data)
draw(intent, ctx, data) = begin
c = component(intent, ctx, data)
isnothing(c) ? draw(ctx, data) : c
end
draw(ctx::Context, data) = draw(ctx.component, data)
draw(_, data) = doodle(data)
component(intent, ctx, data) = nothing
"fallback rendering method that ignores context and intent"
function doodle end
"Formats long numbers with commas seperating it into chunks"
seperate(value::Number; kwargs...) = seperate(string(convert(Float64, value)), kwargs...)
seperate(value::Integer; kwargs...) = seperate(string(value), kwargs...)
seperate(str::String, sep = ",", k = 3) = begin
parts = split(str, '.')
int = parts[1]
uint = split(int, '-')[end]
groups = (uint[max(x-k+1, 1):x] for x in reverse(length(uint):-k:1))
whole_part = @dom[:span startswith(int, '-') ? "-" : "" interleave(groups, @dom[:span css"color: grey" sep])...]
length(parts) == 1 && return whole_part
@dom[:span whole_part @dom[:span css"color: grey" "."] parts[2]]
end
vstack(attrs, children) = @dom[:div{attrs...} css"display: flex; flex-direction: column" children...]
hstack(attrs, children) = @dom[:div{attrs...} css"display: flex; flex-direction: row" children...]
spacer(attrs, children) = begin
@destruct {width=0,height=0,rest...} = attrs
@dom[:div{style.width=string(width, "px"), style.height=string(height, "px"), rest...}]
end
syntax(x) = @dom[:span class="syntax--language syntax--julia" class=syntax_class(x) repr(x)]
syntax_class(::Bool) = ["syntax--constant", "syntax--boolean"]
syntax_class(::Number) = ["syntax--constant", "syntax--numeric"]
syntax_class(::AbstractString) = ["syntax--string", "syntax--quoted", "syntax--double"]
syntax_class(::Regex) = ["syntax--string", "syntax--regexp"]
syntax_class(::Symbol) = ["syntax--constant", "syntax--other", "syntax--symbol"]
syntax_class(::Char) = ["syntax--string", "syntax--quoted", "syntax--single"]
syntax_class(::VersionNumber) = ["syntax--string", "syntax--quoted", "syntax--other"]
syntax_class(::Nothing) = ["syntax--constant"]
syntax_class(::Function) = ["syntax--support", "syntax--function"]
syntax_class(::Missing) = []
doodle(n::Union{AbstractFloat,Integer}) = @dom[:span class="syntax--language syntax--julia syntax--constant syntax--numeric" seperate(n)]
doodle(n::Unsigned) = @dom[:span class="syntax--language syntax--julia syntax--constant syntax--numeric" repr(n)]
doodle(x::Union{Regex,Symbol,Char,VersionNumber,Nothing,Number,Missing}) = syntax(x)
doodle(b::Bool) = syntax(b)
doodle(d::Dates.Date) = @dom[:span Dates.format(d, Dates.dateformat"dd U Y")]
doodle(d::Dates.DateTime) = @dom[:span Dates.format(d, Dates.dateformat"dd/mm/Y H\h M\m S.s\s")]
doodle(d::Dates.Time) = @dom[:span Dates.format(d, Dates.dateformat"HH:MM:S.s")]
doodle(s::AbstractString) =
if occursin(r"\n", s)
@dom[vstack class="syntax--string syntax--quoted syntax--triple syntax--double syntax--julia"
[:span "\"\"\""]
[:span css"white-space: pre" s "\"\"\""]]
else
syntax(s)
end
doodle(r::Rational) = begin
whole, part = divrem(r.num, r.den)
if whole == 0
fraction(r)
else
@dom[:span css"> :first-child {margin-right: 2px}" doodle(whole) fraction(part//r.den)]
end
end
fraction(r::Rational) =
@dom[:span css"""
display: inline-flex
flex-direction: column
line-height: 1em
vertical-align: middle
> .syntax--numeric {font-size: 0.8em}
> :first-child {border-bottom: 1px solid rgb(185,185,185); width: 100%; text-align: center}
"""
doodle(r.num) doodle(r.den)]
brief(m::Module) = @dom[:span class="syntax--keyword syntax--other" replace(repr(m), r"^Main\."=>"")]
getfile(m::Module) = begin
if pathof(m) != nothing
return pathof(m)
end
for (file, mod) in Kip.modules
mod === m && return file
end
end
issubmodule(m::Module) = parentmodule(m) != m && parentmodule(m) != Main
getreadme(::Nothing) = nothing
getreadme(file::AbstractString) = begin
dir = dirname(file)
path = joinpath(dir, "Readme.md")
isfile(path) && return path
basename(dir) == "src" || return nothing
path = joinpath(dirname(dir), "Readme.md")
isfile(path) && return path
nothing
end
doodle(m::Module) = begin
readme = nothing
header = if issubmodule(m)
brief(m)
else
file = getfile(m)
readme = getreadme(file)
@dom[:span brief(m) " from " stacklink(file, 0)]
end
expandable(header) do
@dom[vstack css"max-width: 1000px"
if readme != nothing
expandable(@dom[:h3 "Readme.md"]) do
@dom[:div css"margin-bottom: 20px" drawMDFile(readme)]
end
end
(@dom[hstack
[:span String(name)]
[:span css"padding: 0 10px" "→"]
isdefined(m, name) ? @dom[PropertyValue key=name] : fade("#undef")]
for name in names(m, all=true) if !occursin('#', String(name)) && name != nameof(m))...]
end
end
drawMDFile(path) = resolveLinks(doodle(Markdown.parse_file(path, flavor=Markdown.github)), dirname(path))
resolveLinks(c::DOM.Node, dir) = c
resolveLinks(c::DOM.Container, dir) = assoc(c, :children, map(c->resolveLinks(c, dir), c.children))
resolveLinks(c::DOM.Container{:img}, dir) = begin
haskey(c.attrs, :src) || return c
src = c.attrs[:src]
path = if occursin(r"^https?://", src) || isabspath(src)
src
else
joinpath(dir, src)
end
assoc(c, :attrs, assoc(c.attrs, :src, path))
end
"render a chevron symbol that rotates down when open"
chevron(open) =
@dom[:span class.open=open
class="icon-chevron-right"
css"""
&.open {transform: rotate(0.25turn)}
text-align: center
transition: transform 0.1s ease-out
float: left
width: 1em
margin-right: 4px
"""]
"A summary of a datastructure"
brief(data::Union{AbstractDict,AbstractVector,Set,Tuple,NamedTuple}) =
@dom[:span brief(typeof(data)) [:span css"color: rgb(104, 110, 122)" "[$(length(data))]"]]
brief(u::Union) = @dom[:span
[:span class="syntax--support syntax--type" "Union"]
[:span "{"]
interleave(map(brief, union_params(u)), ",")...
[:span "}"]]
union_params(u::Union) = push!(union_params(u.b), u.a)
union_params(u) = Any[u]
"By default just render the structure of the object"
doodle(data::T) where T =
if showable("text/html", data)
parse(MIME("text/html"), sprint(show, MIME("text/html"), data))
else
attrs = propertynames(data)
isempty(attrs) ? brief(T) : @dom[Expandable]
end
@component PropertyName
@component PropertyValue
data(ctx::Context{PropertyName}) = propertynames(data(ctx.parent))[path(ctx)]
path(c::PropertyName) = c.attrs[:index]
doodle(::PropertyName, name) = @dom[:span string(name)]
brief(data::T) where T = @dom[:span brief(T) '[' length(propertynames(data)) ']']
brief(n::Union{Number,Char}) = syntax(n)
brief(e::Enum) = doodle(e)
body(data::T) where T = begin
@dom[vstack
(@dom[hstack
[PropertyName index=i]
[:span css"padding: 0 10px" "→"]
hasproperty(data, field) ? @dom[PropertyValue key=field] : fade("#undef")]
for (i, field) in enumerate(propertynames(data)))...]
end
brief(x::UnionAll) = begin
body, = flatten_unionall(x)
@dom[:span
[:span class="syntax--support syntax--type" body.name.name]
[:span "{" interleave(map(brief_param, body.parameters), ",")... "}"]]
end
brief_param(t::TypeVar) = @dom[:span class="syntax--keyword syntax--operator syntax--relation syntax--julia" "<:" brief(t.ub)]
brief_param(x) = brief(x)
brief(T::DataType) = begin
@dom[:span
[:span class="syntax--support syntax--type" T.name.name]
if !isempty(T.parameters)
@dom[:span css"display: inline-flex; flex-direction: row"
[:span "{"] interleave(map(brief, T.parameters), ",")... [:span "}"]]
end]
end
header(T::DataType) = begin
if supertype(T) ≠ Any
@dom[:span
brief(T)
[:span class="syntax--keyword syntax--operator syntax--relation syntax--julia" " <: "]
brief(supertype(T))]
else
brief(T)
end
end
brief(t::TypeVar) = @dom[:span t.name]
brief(s::Symbol) = doodle(s)
flatten_unionall(x::DataType, vars=[]) = x, vars
flatten_unionall(x::UnionAll, vars=[]) = flatten_unionall(x.body, push!(vars, x.var))
doodle(t::TypeVar) = begin
t.ub == Any && return @dom[:span t.name]
@dom[:span t.name [:span class="syntax--keyword syntax--operator syntax--relation syntax--julia" "<:"] brief(t.ub)]
end
header(x::UnionAll) = begin
body, vars = flatten_unionall(x)
vars = map(doodle, vars)
@dom[:span header(body)
[:span class="syntax--keyword syntax--other" css"padding: 0 0.5em" "where"]
length(vars) == 1 ? vars[1] : @dom[:span "{" interleave(vars, ",")... "}"]]
end
@component FieldType
data(ctx::Context{FieldType}) = fieldtype(data(ctx.parent), path(ctx))
path(c::FieldType) = c.attrs[:name]
doodle(T::Union{DataType,UnionAll}) = begin
attrs = fields(T)
isempty(attrs) && return header(T)
expandable(header(T)) do
@dom[vstack
Atom.CodeTools.hasdoc(T) ? doodle(Base.doc(T)) : nothing
[vstack css"padding: 3px 5px; background: white; border-radius: 3px; margin: 3px 0"
(@dom[hstack
[:span String(name)]
[:span "::"]
[FieldType name=name]]
for name in attrs)...]
expandable(@dom[:h4 "Constructors"]) do
name = @dom[:span class="syntax--support syntax--function" unwrap(T).name.name]
@dom[vstack (render_method(m, name=name) for m in methods(T))...]
end
expandable(@dom[:h4 "Instance Methods"]) do
ms = methodswith(T)
isempty(ms) && return @dom[:span "No methods for this type"]
@dom[vstack map(doodle, ms)...]
end]
end
end
unwrap(u::UnionAll) = unwrap(u.body)
unwrap(u::DataType) = u
doodle(::Type{T}) where T <: Tuple = brief(T)
doodle(u::Type{Union{}}) = @dom[:span "Union{}"]
doodle(u::Union) = brief(u)
doodle(e::Enum) = @dom[:span string(nameof(typeof(e))) "::" string(e)]
doodle(E::Type{<:Enum}) = @dom[:span string(nameof(E)) "::(" join(map(string, instances(E)), ", ") ")"]
fields(T) = try fieldnames(T) catch; () end
fade(s) = @dom[:span class="fade" s]
expandpath(path) = begin
isempty(path) && return (path, path)
Atom.isuntitled(path) && return ("untitled", path)
!isabspath(path) && return (normpath(joinpath("base", path)), Atom.basepath(path))
("./" * relpath(path, homedir()), path)
end
stacklink(::Nothing, line) = fade("<unknown file>")
stacklink(path, line) = begin
path == "none" && return fade("$path:$line")
path == "./missing" && return fade("./missing")
name, path = expandpath(path)
onmousedown(e) = begin
Atom.@msg openFile(path, line-1)
stop[] = true
end
@dom[:a{onmousedown} Atom.appendline(name, line)]
end
brief(f::StackTraces.StackFrame) = begin
f.linfo isa Nothing && return @dom[:span string(f.func)]
f.linfo isa Core.CodeInfo && return @dom[:span repr(f.linfo.code[1])]
@dom[:span replace(sprint(Base.show_tuple_as_call, f.linfo.def.name, f.linfo.specTypes),
r"^([^(]+)\(.*\)$"=>s"\1")]
end
doodle(trace::StackTraces.StackTrace) = begin
@dom[:div class="error-trace"
map(trace) do frame
@dom[:div class="trace-entry $(Atom.locationshading(string(frame.file))[2:end])"
fade("in ")
brief(frame)
fade(" at ")
stacklink(String(frame.file), frame.line)
fade(frame.inlined ? " <inlined>" : "")]
end...]
end
stripparams(t) = replace(t, r"\{([A-Za-z, ]*?)\}"=>"")
interpose(xs, y) = map(i -> iseven(i) ? xs[i÷2] : y, 2:2length(xs))
doodle(m::Method) = render_method(m)
render_method(m::Method; name=name(m)) = begin
tv, decls, file, line = Base.arg_decl_parts(m)
params = [@dom[:span x isempty(T) ? "" : "::" [:span class="syntax--support syntax--type" stripparams(T)]]
for (x, T) in decls[2:end]]
sig = @dom[:span name "(" interpose(params, ", ")... ")"]
link = file == :null ? "not found" : stacklink(string(file), line)
@dom[:span sig " at " link]
end
name(m::Base.MethodList) = @dom[:span class="syntax--support syntax--function" string(m.mt.name)]
name(m::Method) = @dom[:span class="syntax--support syntax--function" string(m.name)]
doodle(m::Base.MethodList) = begin
ms = collect(m)
isempty(ms) && return @dom[:span name(m) " has no methods"]
length(ms) == 1 && return doodle(ms[1])
@dom[Expandable]
end
brief(m::Base.MethodList) = @dom[:span name(m) " has $(length(collect(m))) methods"]
body(m::Base.MethodList) = @dom[vstack (doodle(method) for method in m)...]
@component MethodListView
data(ctx::Context{MethodListView}) = methods(data(ctx.parent))
doodle(f::Function) = @dom[Expandable]
brief(f::Function) = name(f)
body(f::Function) = begin
@dom[:div css"""
max-width: 800px
white-space: normal
h1 {font-size: 1.4em}
pre {padding: 0}
> div:last-child > div:last-child {overflow: visible}
"""
Atom.CodeTools.hasdoc(f) ? @dom[:div css"padding: 8px 0" doodle(Base.doc(f))] : nothing
[MethodListView]]
end
isanon(f) = occursin('#', String(nameof(f)))
name(f::Function) = @dom[:span class=syntax_class(f) isanon(f) ? "λ" : String(nameof(f))]
# Markdown is loose with its types so we need special functions `renderMD`
doodle(m::Markdown.MD) =
@dom[:div
css"""
max-width: 50em
margin: 0 auto
padding: 1.5em
white-space: normal
font: 1em/1.6em helvetica-light, sans-serif
code.inline
font-family: SourceCodePro-light
border-radius: 3px
padding: 0px 8px
background: #f9f9f9
border: 1px solid #e8e8e8
h1, h2, h3, h4
font-weight: 600
margin: 0.5em 0
h1 {font-size: 2em; margin: 1.5em 0}
h2 {font-size: 1.5em}
h3 {font-size: 1.25em}
ul, ol
margin: 0
padding-left: 2em
li {line-height: 1.5em; margin: 0.4em 0}
li p {margin: 0}
ul > li {list-style: circle; margin: 0}
ul > li > *, ol > li > * {margin: 0}
blockquote
padding: 0 1em
color: #6a737d
border-left: .25em solid #dfe2e5
p, blockquote {margin-bottom: 1em}
"""
map(renderMD, CodeTools.flatten(m).content)...]
doodle(x::Union{AbstractDict,AbstractVector,Set}) = isempty(x) ? brief(x) : @dom[Expandable]
doodle(a::AbstractMatrix) =
@dom[:table css"""
display: grid
border: 1px solid lightgrey
margin: 0.5em 0
border-radius: 5px
background: rgb(250,250,250)
td {padding: 0.4em 1em}
tr:nth-child(even) {background: rgb(235,235,235)}
tr > td:first-child {padding-left: 1em}
"""
[:tbody
(@dom[:tr (@dom[:td doodle(x)] for x in row)...] for row in eachrow(a))...]]
@component DictKey
@component DictValue
@component IndexedItem
@component SetItem
data(ctx::Context{SetItem}) = ctx.node.attrs[:value]
data(ctx::Context{DictKey}) = collect(keys(data(ctx.parent)))[path(ctx)]
body(dict::AbstractDict) =
@dom[:div
(@dom[:div css"display: flex"
[DictKey key=i]
[:span css"padding: 0 10px" "→"]
[DictValue key=key]]
for (i,key) in enumerate(keys(dict)))...]
doodle(t::NamedTuple) = length(t) < 5 ? literal(t) : @dom[Expandable]
doodle(t::Tuple) = length(t) < 10 ? literal(t) : @dom[Expandable]
literal(t::Tuple) = begin
items = (@dom[IndexedItem key=i] for i in 1:length(t))
content = collect(interleave(items, @dom[:span css"padding: 0 6px 0 0" ',']))
length(content) == 1 && push!(content, @dom[:span ','])
@dom[hstack [:span '('] content... [:span ')']]
end
literal(t::NamedTuple) = begin
items = (@dom[hstack string(k) '=' [DictValue key=k]] for k in keys(t))
content = collect(interleave(items, @dom[:span css"padding: 0 6px 0 0" ',']))
length(content) == 1 && push!(content, @dom[:span ','])
@dom[hstack [:span '('] content... [:span ')']]
end
body(s::Set) = @dom[vstack (@dom[SetItem value=v] for v in s)...]
brief(nt::NamedTuple) =
@dom[:span
[:span class="syntax--support syntax--type" "NamedTuple"]
[:span css"color: rgb(104, 110, 122)" "[$(length(nt))]"]]
body(nt::NamedTuple) =
@dom[vstack
(@dom[hstack String(key) [:span css"padding: 0 5px" "="] [IndexedItem key=key]] for key in keys(nt))...]
body(v::Union{Tuple,AbstractVector}) =
@dom[vstack (@dom[IndexedItem key=i] for i in keys(v))...]
"Shows a brief view that can be toggled into a more detailed view"
@component Expandable(state=false)
draw(e::Expandable, data) = begin
isopen = e.state
@dom[:div
[hstack css"align-items: center" onmousedown=(_)->e.state = !isopen
chevron(isopen)
haskey(e.attrs, :head) ? e.attrs[:head] : brief(data)]
if isopen
@dom[:div css"padding: 0 0 3px 20px; overflow: auto; max-height: 500px"
haskey(e.attrs, :thunk) ? e.attrs[:thunk]() : body(data)]
end]
end
expandable(thunk, head) = @dom[Expandable thunk=thunk head=head]
doodle(e::Atom.EvalError) = begin
trace = Atom.cliptrace(Atom.errtrace(e))
head = @dom[:strong class="error-description" ansi(sprint(showerror, e.err))]
isempty(trace) && return head
@dom[:div head doodle(trace)]
end
doodle(e::Expr) = begin
html = Atom.@rpc highlight((src=source(e), grammar="source.julia", block=true))
font = Atom.@rpc config("editor.fontFamily")
dom = parse(MIME("text/html"), html)
dom = add_attr(dom, :style, :fontFamily => font)
add_attr(dom, :class, css"""
display: flex
flex-direction: column
background: none
border-radius: 5px
padding: 0.3em
margin: 0
""")
end
doodle(bits::BitVector) = @dom[:span "BitVector[" doodle(length(bits)) "] " map(doodle∘Int, bits)...]