Skip to content

Improve inspect()/desc()/to_s() methods for better debugging output #913

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/solargraph/api_map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ def hash
equality_fields.hash
end

def to_s
self.class.to_s
end

# avoid enormous dump
def inspect
to_s
end

# @param pins [Array<Pin::Base>]
# @return [self]
def index pins
Expand Down
9 changes: 9 additions & 0 deletions lib/solargraph/api_map/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ def initialize
@clips = {}
end

def to_s
self.class.to_s
end

# avoid enormous dump
def inspect
to_s
end

# @param fqns [String]
# @param scope [Symbol]
# @param visibility [Array<Symbol>]
Expand Down
8 changes: 8 additions & 0 deletions lib/solargraph/api_map/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ def initialize pins = []
index
end

def to_s
self.class.to_s
end

def inspect
to_s
end

# @param fqns [String]
# @param visibility [Array<Symbol>]
# @return [Enumerable<Solargraph::Pin::Base>]
Expand Down
4 changes: 4 additions & 0 deletions lib/solargraph/complex_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ def to_s
map(&:tag).join(', ')
end

def desc
rooted_tags
end

def rooted_tags
map(&:rooted_tag).join(', ')
end
Expand Down
4 changes: 4 additions & 0 deletions lib/solargraph/complex_type/unique_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def rbs_name
end
end

def desc
rooted_tags
end

# @return [String]
def to_rbs
if duck_type?
Expand Down
22 changes: 16 additions & 6 deletions lib/solargraph/pin/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def symbol_kind
end

def to_s
to_rbs
desc
end

# @return [Boolean]
Expand Down Expand Up @@ -276,19 +276,29 @@ def to_rbs
return_type.to_rbs
end

# @return [String, nil]
def desc
# @return [String]
def type_desc
rbs = to_rbs
# RBS doesn't have a way to represent a Class<x> type
rbs = return_type.rooted_tags if return_type.name == 'Class'
if path
if to_rbs
path + ' ' + to_rbs
if rbs
path + ' ' + rbs
else
path
end
else
to_rbs
rbs
end
end

# @return [String]
def desc
closure_info = closure&.desc
binder_info = binder&.desc
"[#{type_desc}, closure=#{closure_info}, binder=#{binder}"
end

def inspect
"#<#{self.class} `#{self.desc}` at #{self.location.inspect}>"
end
Expand Down
4 changes: 2 additions & 2 deletions lib/solargraph/pin/base_variable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ def try_merge! pin
true
end

def desc
"#{to_rbs} = #{assignment&.type.inspect}"
def type_desc
"#{super} = #{assignment&.type.inspect}"
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/solargraph/pin/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def desc
if name.nil? || name.empty?
'(top-level)'
else
to_rbs
return_type.rooted_tags
end
end

Expand Down
8 changes: 8 additions & 0 deletions lib/solargraph/source/chain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ def nullable?
links.any?(&:nullable?)
end

def desc
links.map(&:desc).to_s
end

def to_s
desc
end

private

# @param pins [::Array<Pin::Base>]
Expand Down
11 changes: 10 additions & 1 deletion lib/solargraph/source/chain/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,17 @@ def resolve api_map, name_pin, locals
[]
end

# debugging description of contents; not for machine use
def desc
word
end

def to_s
desc
end

def inspect
"#{self.class} #{word}"
"#<#{self.class} - `#{self.desc}`>"
end

def head?
Expand Down
3 changes: 2 additions & 1 deletion spec/pin/parameter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ def yielder(&blk)
expect(method.documentation).to eq("Block Params:\n* [Array] \n\nBlock Returns:\n* [Integer] \n\nReturns:\n* [Integer] \n\nVisibility: public")
expect(method.return_type.tag).to eq('Integer')

expect(map.locals.map(&:to_s)).to eq(['blk ::Proc', 'things Set'])

expect(map.locals.map(&:to_rbs)).to eq(['blk ::Proc', 'things Set'])
expect(map.locals.map(&:return_type).map(&:to_s)).to eq(%w[Proc Set])
expect(map.locals.map(&:decl)).to eq(%i[blockarg arg])
end
Expand Down