/
eval.jl
216 lines (184 loc) · 5.51 KB
/
eval.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
using CodeTools, LNR, Media
using CodeTools: getthing, getmodule
import REPL
using Logging: with_logger, current_logger
using .Progress: JunoProgressLogger
ends_with_semicolon(x) = REPL.ends_with_semicolon(split(x,'\n',keepempty = false)[end])
LNR.cursor(data::AbstractDict) = cursor(data["row"], data["column"])
exit_on_sigint(on) = ccall(:jl_exit_on_sigint, Nothing, (Cint,), on)
function modulenames(data, pos)
main = haskey(data, "module") ? data["module"] :
haskey(data, "path") ? CodeTools.filemodule(data["path"]) :
"Main"
main == "" && (main = "Main")
sub = CodeTools.codemodule(data["code"], pos)
main, sub
end
function getmodule′(args...)
m = getmodule(args...)
return m == nothing ? Main : m
end
handle("module") do data
main, sub = modulenames(data, cursor(data))
mod = getmodule(main)
smod = getmodule(mod, sub)
return d(:main => main,
:sub => sub,
:inactive => (mod==nothing),
:subInactive => smod==nothing)
end
handle("allmodules") do
sort!([string(m) for m in CodeTools.allchildren(Main)])
end
isselection(data) = data["start"] ≠ data["stop"]
withpath(f, path) =
CodeTools.withpath(f, path == nothing || isuntitled(path) ? nothing : path)
const evallock = ReentrantLock()
handle("evalshow") do data
@dynamic let Media.input = Editor()
@destruct [text, line, path, mod] = data
mod = getmodule′(mod)
lock(evallock)
result = hideprompt() do
withpath(path) do
try
res = include_string(mod, text, path, line)
res ≠ nothing && display(res)
res
catch e
# should hide parts of the backtrace here
Base.display_error(stderr, e, catch_backtrace())
end
end
end
unlock(evallock)
Base.invokelatest() do
display = Media.getdisplay(typeof(result), Media.pool(Editor()), default = Editor())
!isa(result, EvalError) && ends_with_semicolon(text) && (result = nothing)
display ≠ Editor() && result ≠ nothing && render(display, result)
end
nothing
end
end
handle("eval") do data
@dynamic let Media.input = Editor()
@destruct [text, line, path, mod, displaymode || "editor"] = data
mod = getmodule′(mod)
lock(evallock)
result = hideprompt() do
withpath(path) do
@errs include_string(mod, text, path, line)
end
end
unlock(evallock)
Base.invokelatest() do
!isa(result, EvalError) && ends_with_semicolon(text) && (result = nothing)
display(JunoEditorInput(result))
end
end
end
handle("evalall") do data
@dynamic let Media.input = Editor()
@destruct [setmod = :module || nothing, path || "untitled", code] = data
mod = if setmod ≠ nothing
getmodule′(setmod)
elseif isabspath(path)
getmodule′(CodeTools.filemodule(path))
else
Main
end
lock(evallock)
hideprompt() do
withpath(path) do
result = nothing
try
result = include_string(mod, code, path)
catch e
bt = catch_backtrace()
ee = EvalError(e, stacktrace(bt))
if isREPL()
printstyled(stderr, "ERROR: ", color=:red)
Base.showerror(stderr, e, bt)
println(stderr)
else
render(Console(), ee)
end
@msg error(d(:msg => "Error evaluating $(basename(path))",
:detail => string(ee),
:dismissable => true))
end
display(JunoEditorInput(result))
end
end
unlock(evallock)
end
return
end
handle("evalrepl") do data
@dynamic let Media.input = Console()
@destruct [mode || nothing, code, mod || "Main"] = data
if mode == "shell"
code = "Base.repl_cmd(`$code`, STDOUT)"
elseif mode == "help"
render′(@errs getdocs(mod, code))
return
end
mod = getmodule′(mod)
if isdebugging()
render(Console(), @errs Debugger.interpret(code))
else
try
lock(evallock)
withpath(nothing) do
result = @errs Core.eval(mod, :(ans = include_string($mod, $code, "console")))
!isa(result,EvalError) && ends_with_semicolon(code) && (result = nothing)
Base.invokelatest(render′, result)
end
unlock(evallock)
catch e
showerror(stderr, e, catch_stacktrace())
end
end
end
return
end
handle("docs") do data
@destruct [mod || "Main", word] = data
docstring = @errs getdocs(mod, word)
docstring isa EvalError && return Dict(:error => true)
mtable = try getmethods(mod, word)
catch e
[]
end
Dict(:error => false,
:type => :dom,
:tag => :div,
:contents => map(x -> render(Inline(), x), [docstring; mtable]))
end
handle("methods") do data
@destruct [mod || "Main", word] = data
mtable = @errs getmethods(mod, word)
if mtable isa EvalError
Dict(:error => true, :items => sprint(showerror, mtable.err))
else
Dict(:items => [gotoitem(m) for m in mtable])
end
end
getmethods(mod, word) = include_string(getmodule′(mod), "methods($word)")
function getdocs(mod, word)
md = if Symbol(word) in keys(Docs.keywords)
Core.eval(Main, :(@doc($(Symbol(word)))))
else
include_string(getmodule′(mod), "@doc $word")
end
return md_hlines(md)
end
function gotoitem(m::Method)
_, link = view(m)
sig = sprint(show, m)
sig = sig[1:something(findlast(isequal(')'), sig), 0)]
Dict(:text => sig,
:file => link.file,
:line => link.line - 1,
:secondary => join(link.contents))
end