-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Share variable inspection logic between CDP and DAP
- Loading branch information
1 parent
4ec9d7a
commit 6119aa8
Showing
6 changed files
with
413 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require "pp" | ||
|
||
module DEBUGGER__ | ||
class LimitedPP | ||
SHORT_INSPECT_LENGTH = 40 | ||
|
||
def self.pp(obj, max = 80) | ||
out = self.new(max) | ||
catch out do | ||
::PP.singleline_pp(obj, out) | ||
end | ||
out.buf | ||
end | ||
|
||
attr_reader :buf | ||
|
||
def initialize max | ||
@max = max | ||
@cnt = 0 | ||
@buf = String.new | ||
end | ||
|
||
def <<(other) | ||
@buf << other | ||
|
||
if @buf.size >= @max | ||
@buf = @buf[0..@max] + '...' | ||
throw self | ||
end | ||
end | ||
|
||
def self.safe_inspect obj, max_length: SHORT_INSPECT_LENGTH, short: false | ||
if short | ||
LimitedPP.pp(obj, max_length) | ||
else | ||
obj.inspect | ||
end | ||
rescue NoMethodError => e | ||
klass, oid = M_CLASS.bind_call(obj), M_OBJECT_ID.bind_call(obj) | ||
if obj == (r = e.receiver) | ||
"<\##{klass.name}#{oid} does not have \#inspect>" | ||
else | ||
rklass, roid = M_CLASS.bind_call(r), M_OBJECT_ID.bind_call(r) | ||
"<\##{klass.name}:#{roid} contains <\##{rklass}:#{roid} and it does not have #inspect>" | ||
end | ||
# rescue Exception => e | ||
# "<#inspect raises #{e.inspect}>" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'limited_pp' | ||
|
||
module DEBUGGER__ | ||
class VariableInspector | ||
class Member | ||
attr_reader :name, :value | ||
|
||
def initialize(name:, value:, internal: false) | ||
@name = name | ||
@value = value | ||
@is_internal = internal | ||
end | ||
|
||
def internal? | ||
@is_internal | ||
end | ||
|
||
def self.internal name:, value: | ||
new(name:, value:, internal: true) | ||
end | ||
|
||
def ==(other) | ||
other.instance_of?(self.class) && | ||
@name == other.name && | ||
@value == other.value && | ||
@is_internal == other.internal? | ||
end | ||
|
||
def inspect | ||
"#<Member name=#{@name.inspect} value=#{@value.inspect}#{@is_internal ? " internal" : ""}>" | ||
end | ||
end | ||
|
||
def indexed_members_of obj, start:, count: | ||
return [] if start > (obj.length - 1) | ||
|
||
capped_count = [count, obj.length - start].min | ||
|
||
(start...(start + capped_count)).map do |i| | ||
Member.new(name: i.to_s, value: obj[i]) | ||
end | ||
end | ||
|
||
def named_members_of obj | ||
members = case obj | ||
when Hash then obj.map { |k, v| Member.new(name: value_inspect(k), value: v) } | ||
when Struct then obj.members.map { |name| Member.new(name:, value: obj[name]) } | ||
when String | ||
members = [ | ||
Member.internal(name: '#length', value: obj.length), | ||
Member.internal(name: '#encoding', value: obj.encoding), | ||
] | ||
|
||
printed_str = value_inspect(obj) | ||
members << Member.internal(name: "#dump", value: NaiveString.new(obj)) if printed_str.end_with?('...') | ||
|
||
members | ||
when Class, Module then [Member.internal(name: "%ancestors", value: obj.ancestors[1..])] | ||
when Range then [ | ||
Member.internal(name: "#begin", value: obj.begin), | ||
Member.internal(name: "#end", value: obj.end), | ||
] | ||
else [] | ||
end | ||
|
||
unless NaiveString === obj | ||
members += M_INSTANCE_VARIABLES.bind_call(obj).sort.map{|iv| | ||
Member.new(name: iv, value: M_INSTANCE_VARIABLE_GET.bind_call(obj, iv)) | ||
} | ||
members.unshift Member.internal(name: '#class', value: M_CLASS.bind_call(obj)) | ||
end | ||
|
||
members | ||
end | ||
|
||
private | ||
|
||
MAX_LENGTH = 180 | ||
|
||
def value_inspect obj, short: true | ||
# TODO: max length should be configurable? | ||
str = LimitedPP.safe_inspect obj, short: short, max_length: MAX_LENGTH | ||
|
||
if str.encoding == Encoding::UTF_8 | ||
str.scrub | ||
else | ||
str.encode(Encoding::UTF_8, invalid: :replace, undef: :replace) | ||
end | ||
end | ||
|
||
# TODO: Replace with Reflection helpers once they are merged | ||
# https://github.com/ruby/debug/pull/1002 | ||
M_INSTANCE_VARIABLES = method(:instance_variables).unbind | ||
M_INSTANCE_VARIABLE_GET = method(:instance_variable_get).unbind | ||
M_CLASS = method(:class).unbind | ||
|
||
class NaiveString | ||
attr_reader :str | ||
def initialize str | ||
@str = str | ||
end | ||
|
||
def == other | ||
other.instance_of?(self.class) && @str == other.str | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.