-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFind in scripts.lua
90 lines (76 loc) · 2.08 KB
/
Find in scripts.lua
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
sim = require 'sim'
function sysCall_info()
return {autoStart = false, menu = 'Developer tools\nFind in scripts...'}
end
function sysCall_addOnScriptSuspend()
return {cmd = 'cleanup'}
end
function sysCall_init()
simUI = require 'simUI'
showDlg()
end
function sysCall_nonSimulation()
if leaveNow then return {cmd = 'cleanup'} end
end
function sysCall_beforeSimulation()
hideDlg()
end
function sysCall_afterSimulation()
showDlg()
end
function sysCall_cleanup()
hideDlg()
end
function sysCall_beforeInstanceSwitch()
hideDlg()
end
function showDlg()
if not ui then
local pos = 'placement="center"'
if uiPos then
pos = 'position="' .. uiPos[1] .. ',' .. uiPos[2] .. '" placement="absolute"'
end
local xml = [[
<ui title="Find in scripts" activate="true" closeable="true" on-close="close_callback" ]] .. pos .. [[>
<group layout="form" flat="true">
<label text="Search string:"/>
<edit value="" id="1" />
</group>
<button text="Find" on-click="find_callback" id="3"/>
</ui>
]]
ui = simUI.create(xml)
end
end
function hideDlg()
if ui then
uiPos = {}
uiPos[1], uiPos[2] = simUI.getPosition(ui)
simUI.destroy(ui)
ui = nil
end
end
function find_callback(ui, id, v)
local searchString = simUI.getEditValue(ui, 1)
if #searchString > 0 then
for i, handle in ipairs(sim.getObjectsInTree(sim.handle_scene, sim.object_script_type)) do
local alias = sim.getObjectAlias(handle, 2)
local code = sim.getProperty(handle, 'code')
grep(code, searchString, alias)
end
end
end
function close_callback()
leaveNow = true
end
function grep(text, searchText, prefix)
local lines = {}
for line in text:gmatch("[^\r\n]+") do
table.insert(lines, line)
end
for lineNumber, line in ipairs(lines) do
if line:find(searchText) then
print(string.format("%s:%d: %s", prefix, lineNumber, line))
end
end
end