From 1965cc13d437ff48a932b2d0f072abf690372ba1 Mon Sep 17 00:00:00 2001 From: luca-sartore-prorob <59091319+lucaSartore@users.noreply.github.com> Date: Wed, 2 Oct 2024 21:33:04 +0200 Subject: [PATCH] feat(scopes): expandable scopes/collapse expensive (#403) * Add ability do disable scopes that are too expensive to render * Update test for scopes component --- lua/dapui/components/scopes.lua | 34 +++++++++++++++++++++++++++-- tests/unit/elements/scopes_spec.lua | 20 ++++++++++------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/lua/dapui/components/scopes.lua b/lua/dapui/components/scopes.lua index 48ac4fe..05d6b43 100644 --- a/lua/dapui/components/scopes.lua +++ b/lua/dapui/components/scopes.lua @@ -2,12 +2,27 @@ local config = require("dapui.config") ---@param client dapui.DAPClient return function(client, send_ready) local render_vars = require("dapui.components.variables")(client, send_ready) + local closed_scopes = {} + + ---@param scope dapui.types.Scope + local function scope_prefix(scope) + if scope.indexedVariables == 0 then + return " " + end + return config.icons[closed_scopes[scope.name] and "collapsed" or "expanded"] + end ---@type dapui.types.Scope[] | nil local _scopes client.listen.scopes(function(args) if args.response then _scopes = args.response.scopes + -- when new scopes are parsed, automatically disable the scopes that are too expensive to render + for _, scope in ipairs(_scopes) do + if scope.expensive then + closed_scopes[scope.name] = true + end + end end send_ready() end) @@ -28,8 +43,23 @@ return function(client, send_ready) return end for i, scope in pairs(scopes) do - canvas:write({ { scope.name, group = "DapUIScope" }, ":\n" }) - render_vars.render(canvas, scope.name, scope.variablesReference, config.render.indent) + canvas:add_mapping("expand", function() + closed_scopes[scope.name] = not closed_scopes[scope.name] + send_ready() + end) + + canvas:write({ + { scope_prefix(scope), group = "DapUIDecoration" }, + " ", + { scope.name, group = "DapUIScope" }, + { ":\n" }, + }) + + -- only render expanded scopes to save resources + if not closed_scopes[scope.name] then + render_vars.render(canvas, scope.name, scope.variablesReference, config.render.indent) + end + if i < #scopes then canvas:write("\n") end diff --git a/tests/unit/elements/scopes_spec.lua b/tests/unit/elements/scopes_spec.lua index 80117cc..497acfa 100644 --- a/tests/unit/elements/scopes_spec.lua +++ b/tests/unit/elements/scopes_spec.lua @@ -76,11 +76,11 @@ describe("scopes element", function() a.it("renders initial lines", function() local lines = nio.api.nvim_buf_get_lines(buf, 0, -1, false) assert.same({ - "Locals:", + " Locals:", " a number = 1", "  b number = 2", "", - "Globals:", + " Globals:", " CONST_A boolean = true", }, lines) end) @@ -88,7 +88,8 @@ describe("scopes element", function() a.it("renders initial highlights", function() local formatted = tests.util.get_highlights(buf) assert.same({ - { "DapUIScope", 0, 0, 0, 6 }, + { "DapUIDecoration", 0, 0, 0, 3 }, + { "DapUIScope", 0, 4, 0, 10 }, { "DapUIDecoration", 1, 1, 1, 2 }, { "DapUIVariable", 1, 3, 1, 4 }, { "DapUIType", 1, 5, 1, 11 }, @@ -97,7 +98,8 @@ describe("scopes element", function() { "DapUIVariable", 2, 5, 2, 6 }, { "DapUIType", 2, 7, 2, 13 }, { "DapUIValue", 2, 16, 2, 17 }, - { "DapUIScope", 4, 0, 4, 7 }, + { "DapUIDecoration", 4, 0, 4, 3 }, + { "DapUIScope", 4, 4, 4, 11 }, { "DapUIDecoration", 5, 1, 5, 2 }, { "DapUIVariable", 5, 3, 5, 10 }, { "DapUIType", 5, 11, 5, 18 }, @@ -112,12 +114,12 @@ describe("scopes element", function() nio.sleep(10) local lines = nio.api.nvim_buf_get_lines(buf, 0, -1, false) assert.same({ - "Locals:", + " Locals:", " a number = 1", "  b number = 2", " c string = '3'", "", - "Globals:", + " Globals:", " CONST_A boolean = true", }, lines) end) @@ -127,7 +129,8 @@ describe("scopes element", function() nio.sleep(10) local formatted = tests.util.get_highlights(buf) assert.same({ - { "DapUIScope", 0, 0, 0, 6 }, + { "DapUIDecoration", 0, 0, 0, 3 }, + { "DapUIScope", 0, 4, 0, 10 }, { "DapUIDecoration", 1, 1, 1, 2 }, { "DapUIVariable", 1, 3, 1, 4 }, { "DapUIType", 1, 5, 1, 11 }, @@ -140,7 +143,8 @@ describe("scopes element", function() { "DapUIVariable", 3, 4, 3, 5 }, { "DapUIType", 3, 6, 3, 12 }, { "DapUIValue", 3, 15, 3, 18 }, - { "DapUIScope", 5, 0, 5, 7 }, + { "DapUIDecoration", 5, 0, 5, 3 }, + { "DapUIScope", 5, 4, 5, 11 }, { "DapUIDecoration", 6, 1, 6, 2 }, { "DapUIVariable", 6, 3, 6, 10 }, { "DapUIType", 6, 11, 6, 18 },