From ee5bd712610104c195aaadf82dac3059374b8a8d Mon Sep 17 00:00:00 2001 From: Koichi Sasada Date: Fri, 18 Nov 2022 04:13:22 +0900 Subject: [PATCH] change result chars: 4096 -> 180 Long results on DAP was clipped with 4096 chars. This patch shorten this limit to 180 chars. Compound data structure doesn't have any issue because expanding the result shows more details with corresponding values such as instance variables and so on. The problem is String object because we can not know the whole string body if it is > 180 chars. To see the whole body this patch provides `#dump` special field to show the whole body of the string and you can copy&paste the value. This patch also introduce `::DEBUGGER__::NaiveString` class. Evaluation results of instances of this class will not be clipped so if you need to get whole body of String, please wrap the string with `NaiveString` class. fix https://github.com/ruby/debug/issues/802 --- lib/debug/server_dap.rb | 67 ++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/lib/debug/server_dap.rb b/lib/debug/server_dap.rb index 71c3b06fb..ac25e64e6 100644 --- a/lib/debug/server_dap.rb +++ b/lib/debug/server_dap.rb @@ -690,10 +690,20 @@ def register_vars vars, tid end end + class NaiveString + attr_reader :str + def initialize str + @str = str + end + end + class ThreadClient - def value_inspect obj + + MAX_LENGTH = 180 + + def value_inspect obj, short: true # TODO: max length should be configuarable? - str = DEBUGGER__.safe_inspect obj, short: true, max_length: 4 * 1024 + str = DEBUGGER__.safe_inspect obj, short: short, max_length: MAX_LENGTH if str.encoding == Encoding::UTF_8 str.scrub @@ -805,8 +815,9 @@ def process_dap args when String vars = [ variable('#length', obj.length), - variable('#encoding', obj.encoding) + variable('#encoding', obj.encoding), ] + vars << variable('#dump', NaiveString.new(obj)) if obj.length > MAX_LENGTH when Class, Module vars = obj.instance_variables.map{|iv| variable(iv, obj.instance_variable_get(iv)) @@ -819,10 +830,12 @@ def process_dap args ] end - vars += M_INSTANCE_VARIABLES.bind_call(obj).map{|iv| - variable(iv, M_INSTANCE_VARIABLE_GET.bind_call(obj, iv)) - } - vars.unshift variable('#class', M_CLASS.bind_call(obj)) + unless NaiveString === obj + vars += M_INSTANCE_VARIABLES.bind_call(obj).map{|iv| + variable(iv, M_INSTANCE_VARIABLE_GET.bind_call(obj, iv)) + } + vars.unshift variable('#class', M_CLASS.bind_call(obj)) + end end end event! :dap_result, :variable, req, variables: (vars || []), tid: self.id @@ -940,11 +953,7 @@ def search_const b, expr end def evaluate_result r - v = variable nil, r - v.delete :name - v.delete :value - v[:result] = value_inspect(r) - v + variable nil, r end def type_name obj @@ -965,15 +974,31 @@ def variable_ name, obj, indexedVariables: 0, namedVariables: 0 vid = 0 end - ivnum = M_INSTANCE_VARIABLES.bind_call(obj).size + namedVariables += M_INSTANCE_VARIABLES.bind_call(obj).size - { name: name, - value: value_inspect(obj), - type: type_name(obj), - variablesReference: vid, - indexedVariables: indexedVariables, - namedVariables: namedVariables + ivnum, - } + if NaiveString === obj + str = obj.str.dump + vid = indexedVariables = namedVariables = 0 + else + str = value_inspect(obj) + end + + if name + { name: name, + value: str, + type: type_name(obj), + variablesReference: vid, + indexedVariables: indexedVariables, + namedVariables: namedVariables, + } + else + { result: str, + type: type_name(obj), + variablesReference: vid, + indexedVariables: indexedVariables, + namedVariables: namedVariables, + } + end end def variable name, obj @@ -983,7 +1008,7 @@ def variable name, obj when Hash variable_ name, obj, namedVariables: obj.size when String - variable_ name, obj, namedVariables: 3 # #to_str, #length, #encoding + variable_ name, obj, namedVariables: 3 # #length, #encoding, #to_str when Struct variable_ name, obj, namedVariables: obj.size when Class, Module