-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathevaluate.lua
More file actions
94 lines (86 loc) · 2.63 KB
/
evaluate.lua
File metadata and controls
94 lines (86 loc) · 2.63 KB
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
local variables = require 'backend.worker.variables'
local eval = require 'backend.worker.eval'
local function run_repl(frameId, expression)
local res = table.pack(eval.readwrite('return ' .. expression, frameId))
if not res[1] then
res = table.pack(eval.readwrite(expression, frameId))
if not res[1] then
return false, res[2]
end
end
local var = variables.createRef(res[2], expression, "repl")
local result = {var.value}
for i = 3, res.n do
result[i-1] = variables.createText(res[i], "repl")
end
var.result = table.concat(result, ',')
var.value = nil
return true, var
end
local function run_watch(frameId, expression)
local res = table.pack(eval.readonly(expression, frameId))
if not res[1] then
return false, res[2]
end
local var = variables.createRef(res[2], expression, "watch")
local result = {var.value}
for i = 3, res.n do
result[i-1] = variables.createText(res[i], "watch")
end
var.result = table.concat(result, ',')
var.value = nil
return true, var
end
local function run_hover(frameId, expression)
local ok, res = eval.readonly(expression, frameId)
if not ok then
return false, res
end
local var = variables.createRef(res, expression, "hover")
var.result = var.value
var.value = nil
return true, var
end
local function run_clipboard(frameId, expression)
local res = table.pack(eval.readonly(expression, frameId))
if not res[1] then
return false, res[2]
end
if res.n == 1 then
return true, { result = 'nil' }
end
local result = {}
for i = 2, res.n do
result[i-1] = variables.createText(res[i], "clipboard")
end
return true, { result = table.concat(result, ',') }
end
local m = {}
function m.run(frameId, expression, context)
if context == "watch" then
return run_watch(frameId, expression)
end
if context == "hover" then
return run_hover(frameId, expression)
end
if context == "repl" then
return run_repl(frameId, expression)
end
if context == "clipboard" then
return run_clipboard(frameId, expression)
end
--兼容旧版本VSCode
if context == "variables" then
return run_clipboard(frameId, expression)
end
return nil, ("unknown context `%s`"):format(context)
end
function m.set(frameId, expression, value)
local ok, res = eval.readwrite(expression.."="..value..";return "..expression, frameId)
if not ok then
return false, res
end
local var = variables.createRef(res, expression, "variables")
return true, var
end
return m