From 8f5d71a6b9d2c979dd831ce6e6e9ed8f47474ef6 Mon Sep 17 00:00:00 2001 From: Joppe Blondel Date: Fri, 24 Oct 2025 14:53:00 +0200 Subject: [PATCH] Added hot reload of loaders button and did a bit of luadebug --- .lite_project.lua | 1 + init.lua | 49 +++++++++++++++++++++++++++++++++------ runners/luadebug.lua | 55 +++++++++++++++++++++++++++++++++++++++++++- runners/shell.lua | 4 +++- 4 files changed, 100 insertions(+), 9 deletions(-) diff --git a/.lite_project.lua b/.lite_project.lua index 9723012..6b9083d 100644 --- a/.lite_project.lua +++ b/.lite_project.lua @@ -14,6 +14,7 @@ config.plugins.jpdebug = { type = "luadebug", entry = "test.lua", cwd = ".", + lua = {"C:\\msys64\\msys2_shell.cmd", "-defterm", "-here", "-no-start", "-ucrt64", "-shell", "bash", "-c", "lua"}, }, }, default_target = "luadebug" diff --git a/init.lua b/init.lua index 6cd1590..db2c293 100644 --- a/init.lua +++ b/init.lua @@ -12,10 +12,6 @@ core.jpdebug = core.jpdebug or {} -- Global list of all the runners core.jpdebug.runners = core.jpdebug.runners or {} -local runner_shell = require("plugins.jpdebug.runners.shell") -local runner_luadebug = require("plugins.jpdebug.runners.luadebug") -core.jpdebug.runners[runner_shell.name] = runner_shell -core.jpdebug.runners[runner_luadebug.name] = runner_luadebug -- Currently used view local active_view = nil @@ -27,6 +23,9 @@ local selected_target = nil local running_proc = nil local running_runner = nil +-- Table containing all debug information (like breakpoints) +local debug_info = {} + -- Check if required plugins are installed local required_toolbar_plugins = true if TreeView == nil or ToolbarView == nil then @@ -88,6 +87,7 @@ local JPDebugView = View:extend() function JPDebugView:new(title) JPDebugView.super.new(self) self.scrollable = true + self.context = "session" self.caption = title or "JP Debug" self.lines = { "ready.\n" } self.max_lines = 5000 -- keep memory bounded @@ -162,6 +162,7 @@ local function run_target(target, name) return end + -- TODO fix this, it throws a node is locked error once in a while local view = nil if active_view then -- If there is already a view use that one @@ -177,7 +178,7 @@ local function run_target(target, name) for runner_name,runner in pairs(core.jpdebug.runners) do if runner_name == target.type then -- Found a runner - running_proc = runner:run(target, name) + running_proc = runner:run(target, name, debug_info) running_runner = runner if running_proc == nil then @@ -221,6 +222,35 @@ local function run_target(target, name) view:push("stderr", "No suitable runners found for target "..name) end +local function hot_require(name, into) + -- blow away the cached module + package.loaded[name] = nil + local fresh = require(name) + + if into then + -- wipe old table (keep the identity) + for k in pairs(into) do into[k] = nil end + -- copy new fields in + for k, v in pairs(fresh) do into[k] = v end + -- copy metatable too, if any + local mt = getmetatable(fresh) + if mt then setmetatable(into, mt) end + return into + else + return fresh + end +end + +local function load_runners() + local runner_shell = hot_require("plugins.jpdebug.runners.shell", core.jpdebug.runners.shell) + local runner_luadebug = hot_require("plugins.jpdebug.runners.luadebug", core.jpdebug.runners.luadebug) + core.jpdebug.runners[runner_shell.name] = runner_shell + core.jpdebug.runners[runner_luadebug.name] = runner_luadebug + core.log("[jpdebug] Runners loaded") +end +-- And call it while loading the plugin +load_runners() + -- ---------- Add toolbar to treeview if plugins are installed ------ if required_toolbar_plugins and ToolbarView then @@ -234,13 +264,14 @@ if required_toolbar_plugins and ToolbarView then function Toolbar:_rebuild() local t = { - {symbol = "A", command = "jpdebug:settarget"}, + {symbol = "A", command = "jpdebug:set-target"}, } if running_proc == nil then table.insert(t, {symbol = "B", command = "jpdebug:run"}) else table.insert(t, {symbol = "D", command = "jpdebug:stop"}) end + table.insert(t, {symbol = "E", command = "jpdebug:reload-runners"}) self.toolbar_commands = t end @@ -291,7 +322,7 @@ command.add(nil, { end, -- The set target command - ["jpdebug:settarget"] = function() + ["jpdebug:set-target"] = function() core.command_view:enter("Select target", { show_suggestions = true, submit = function(selection) @@ -311,5 +342,9 @@ command.add(nil, { }) end, + ["jpdebug:reload-runners"] = function() + load_runners() + end, + }) diff --git a/runners/luadebug.lua b/runners/luadebug.lua index c5bd084..4572fd8 100644 --- a/runners/luadebug.lua +++ b/runners/luadebug.lua @@ -1,6 +1,25 @@ local core = require "core" local process = require "process" +-- Dump any table to a string +---@param o any +---@param force bool|nil +---@return nil +---@diagnostic disable-next-line: unused-function +local function dump(o, force) + force = force or false + if type(o) == 'table' or force then + local s = '{ ' + for k,v in pairs(o) do + if type(k) ~= 'number' then k = '"'..k..'"' end + s = s .. '['..k..'] = ' .. dump(v) .. ',' + end + return s .. '} ' + else + return type(o)..": "..tostring(o) + end +end + ---@class LDB local LDB = { name = "luadebug" @@ -8,8 +27,42 @@ local LDB = { ---@param target table Target table ---@param name string Name of the target to run +---@praam debuginfo table Debugging information ---@return process|nil -function LDB:run(target, name) +function LDB:run(target, name, debuginfo) + if target.entry == nil then + core.error("[jpdebug][luadebug] target.entry is required") + return + end + local entry = target.entry + local lua = target.lua or {"lua"} + local cwd = target.cwd or "." + local env = target.env or {} + + -- Build the inline lua launcher + local dbg_call = string.format([[ + print("Here will the call to mobdebug be") + ]]) + local launcher = string.format([[ + %s + dofile(%q) + ]], dbg_call, entry) + + -- Spawn the process + local cmd = lua + -- table.insert(lua, "-e") + table.insert(lua, entry) + local proc = process.start(cmd, { + cwd = cwd, + env = env, + stdout = process.REDIRECT_PIPE, + stderr = process.REDIRECT_PIPE, + }) + if proc == nil then + core.error("[jpdebug][luadebug] Failed to start "..entry) + return nil + end + return proc end -- Wait untill it ends, possibly with timeout diff --git a/runners/shell.lua b/runners/shell.lua index 0cfb479..8fb0c3f 100644 --- a/runners/shell.lua +++ b/runners/shell.lua @@ -9,8 +9,10 @@ local M = { -- Run a shell command ---@param target table Target table ---@param name string Name of the target to run +---@praam debuginfo table Debugging information ---@return process|nil -function M:run(target, name) +---@diagnostic disable-next-line: unused-local +function M:run(target, name, debuginfo) core.log("[jpdebug] Running shell command") local opts = { cwd = target.cwd or ".",