|
| 1 | +sim = require 'sim' |
| 2 | + |
| 3 | +function sysCall_info() |
| 4 | + return {autoStart = false, menu = 'Developer tools\nFind in scripts...'} |
| 5 | +end |
| 6 | + |
| 7 | +function sysCall_addOnScriptSuspend() |
| 8 | + return {cmd = 'cleanup'} |
| 9 | +end |
| 10 | + |
| 11 | +function sysCall_init() |
| 12 | + simUI = require 'simUI' |
| 13 | + showDlg() |
| 14 | +end |
| 15 | + |
| 16 | +function sysCall_nonSimulation() |
| 17 | + if leaveNow then return {cmd = 'cleanup'} end |
| 18 | +end |
| 19 | + |
| 20 | +function sysCall_beforeSimulation() |
| 21 | + hideDlg() |
| 22 | +end |
| 23 | + |
| 24 | +function sysCall_afterSimulation() |
| 25 | + showDlg() |
| 26 | +end |
| 27 | + |
| 28 | +function sysCall_cleanup() |
| 29 | + hideDlg() |
| 30 | +end |
| 31 | + |
| 32 | +function sysCall_beforeInstanceSwitch() |
| 33 | + hideDlg() |
| 34 | +end |
| 35 | + |
| 36 | +function showDlg() |
| 37 | + if not ui then |
| 38 | + local pos = 'placement="center"' |
| 39 | + if uiPos then |
| 40 | + pos = 'position="' .. uiPos[1] .. ',' .. uiPos[2] .. '" placement="absolute"' |
| 41 | + end |
| 42 | + local xml = [[ |
| 43 | + <ui title="Find in scripts" activate="true" closeable="true" on-close="close_callback" ]] .. pos .. [[> |
| 44 | + <group layout="form" flat="true"> |
| 45 | + <label text="Search string:"/> |
| 46 | + <edit value="" id="1" /> |
| 47 | + </group> |
| 48 | + <button text="Find" on-click="find_callback" id="3"/> |
| 49 | + </ui> |
| 50 | + ]] |
| 51 | + ui = simUI.create(xml) |
| 52 | + end |
| 53 | +end |
| 54 | + |
| 55 | +function hideDlg() |
| 56 | + if ui then |
| 57 | + uiPos = {} |
| 58 | + uiPos[1], uiPos[2] = simUI.getPosition(ui) |
| 59 | + simUI.destroy(ui) |
| 60 | + ui = nil |
| 61 | + end |
| 62 | +end |
| 63 | + |
| 64 | +function find_callback(ui, id, v) |
| 65 | + local searchString = simUI.getEditValue(ui, 1) |
| 66 | + if #searchString > 0 then |
| 67 | + for i, handle in ipairs(sim.getObjectsInTree(sim.handle_scene, sim.object_script_type)) do |
| 68 | + local alias = sim.getObjectAlias(handle, 2) |
| 69 | + local code = sim.getProperty(handle, 'code') |
| 70 | + grep(code, searchString, alias) |
| 71 | + end |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | +function close_callback() |
| 76 | + leaveNow = true |
| 77 | +end |
| 78 | + |
| 79 | +function grep(text, searchText, prefix) |
| 80 | + local lines = {} |
| 81 | + for line in text:gmatch("[^\r\n]+") do |
| 82 | + table.insert(lines, line) |
| 83 | + end |
| 84 | + |
| 85 | + for lineNumber, line in ipairs(lines) do |
| 86 | + if line:find(searchText) then |
| 87 | + print(string.format("%s:%d: %s", prefix, lineNumber, line)) |
| 88 | + end |
| 89 | + end |
| 90 | +end |
0 commit comments