Skip to content

Commit a40ddc0

Browse files
authored
Add #to_rbs methods to pins, use for better .inspect() output (#789)
* Add #to_rbs methods to pins, use for better .inspect() output * Add inspect() to Chain::Link, to_rbs to Pin::Namespace * Fix typo * Show variable assignments in #desc * Method generics resolution via argument types (#794) * Improve block handling in signature selection Fixes #777 * Resolve method generics * Track generics in method signatures from RBS and YARD * Erase method generics that couldn't be resolved * Refactor Pin#resolve_types to use Pin#transform_types * Fix issues with UniqueType#transform() * Cleanups * Move to keyword parameters for resolved_generic_values * Add type erasure, method pin rewrite code * Populate Method parameter/return_type/block from signature * Mark completed TODOs * Ruby 3.0 compatibility * Add erase_generics() to ComplexType::TypeMethods * TODO -> @todo * Resolve some TODOs * Resolve TODOs * Revert accidental change * Bring back spec * Provide full tag to get_method_stack() for better inference * Fix a missed spot referring to allowlist * Ensure Signature#generics is not nil * Various #to_rbs and #inspect improvements * Clean up code * Drop accidental addition * Improve RBS generation from UniqueType * Use rooted names when generating RBS when available * Drop bad documentation
1 parent aac9e41 commit a40ddc0

File tree

13 files changed

+193
-16
lines changed

13 files changed

+193
-16
lines changed

lib/solargraph/complex_type.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ def first
4747

4848
# @return [String]
4949
def to_rbs
50-
((@items.length > 1 ? '(' : '') + @items.map do |item|
51-
"#{item.namespace}#{item.parameters? ? "[#{item.subtypes.map { |s| s.to_rbs }.join(', ')}]" : ''}"
52-
end.join(' | ') + (@items.length > 1 ? ')' : '')).gsub(/undefined/, 'untyped')
53-
# "
50+
((@items.length > 1 ? '(' : '') +
51+
@items.map(&:to_rbs).join(' | ') +
52+
(@items.length > 1 ? ')' : ''))
5453
end
5554

5655
# @yieldparam [UniqueType]

lib/solargraph/complex_type/type_methods.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ def rooted_namespace
115115
"::#{namespace}"
116116
end
117117

118+
# @return [String]
119+
def rooted_name
120+
return name unless rooted?
121+
"::#{name}"
122+
end
123+
118124
# @return [::Symbol] :class or :instance
119125
def scope
120126
@scope ||= :instance if duck_type? || nil_type?

lib/solargraph/complex_type/unique_type.rb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,34 @@ def items
6565
[self]
6666
end
6767

68+
# @return [String]
69+
def rbs_name
70+
if name == 'undefined'
71+
'untyped'
72+
else
73+
rooted_name
74+
end
75+
end
76+
6877
# @return [String]
6978
def to_rbs
70-
"#{namespace}#{parameters? ? "[#{subtypes.map { |s| s.to_rbs }.join(', ')}]" : ''}"
71-
# "
79+
if ['Tuple', 'Array'].include?(name) && fixed_parameters?
80+
# tuples don't have a name; they're just [foo, bar, baz].
81+
if substring == '()'
82+
# but there are no zero element tuples, so we go with an array
83+
'Array[]'
84+
else
85+
# already generated surrounded by []
86+
parameters_as_rbs
87+
end
88+
else
89+
"#{rbs_name}#{parameters_as_rbs}"
90+
end
91+
end
92+
93+
# @return [String]
94+
def parameters_as_rbs
95+
parameters? ? "[#{all_params.map { |s| s.to_rbs }.join(', ')}]" : ''
7296
end
7397

7498
def generic?
@@ -180,7 +204,7 @@ def recreate(new_name: nil, new_key_types: nil, new_subtypes: nil)
180204
def transform(new_name = nil, &transform_type)
181205
new_key_types = @key_types.flat_map { |ct| ct.map { |ut| ut.transform(&transform_type) } }.compact
182206
new_subtypes = @subtypes.flat_map { |ct| ct.map { |ut| ut.transform(&transform_type) } }.compact
183-
new_type = recreate(new_name: new_name || name, new_key_types: new_key_types, new_subtypes: new_subtypes)
207+
new_type = recreate(new_name: new_name || rooted_name, new_key_types: new_key_types, new_subtypes: new_subtypes)
184208
yield new_type
185209
end
186210

lib/solargraph/pin/base.rb

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def symbol_kind
9494
end
9595

9696
def to_s
97-
name.to_s
97+
to_rbs
9898
end
9999

100100
# @return [Boolean]
@@ -257,8 +257,26 @@ def identity
257257
@identity ||= "#{closure.path}|#{name}"
258258
end
259259

260+
# @return [String, nil]
261+
def to_rbs
262+
return_type.to_rbs
263+
end
264+
265+
# @return [String, nil]
266+
def desc
267+
if path
268+
if to_rbs
269+
path + ' ' + to_rbs
270+
else
271+
path
272+
end
273+
else
274+
to_rbs
275+
end
276+
end
277+
260278
def inspect
261-
"#<#{self.class} `#{self.path}` at #{self.location.inspect}>"
279+
"#<#{self.class} `#{self.desc}` at #{self.location.inspect}>"
262280
end
263281

264282
protected

lib/solargraph/pin/base_variable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def try_merge! pin
7171
true
7272
end
7373

74+
def desc
75+
"#{to_rbs} = #{assignment&.type.inspect}"
76+
end
77+
7478
private
7579

7680
# @return [ComplexType]

lib/solargraph/pin/closure.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ def gates
4040
def generics
4141
@generics ||= docstring.tags(:generic).map(&:name)
4242
end
43+
44+
# @return [String]
45+
def generics_as_rbs
46+
return '' if generics.empty?
47+
48+
generics.join(', ') + ' '
49+
end
4350
end
4451
end
4552
end

lib/solargraph/pin/local_variable.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ def visible_at?(other_closure, other_loc)
3030
match_named_closure(other_closure, closure)
3131
end
3232

33+
# @return [String]
34+
def to_rbs
35+
(name || '(anon)') + ' ' + (@return_type&.to_rbs || 'untyped')
36+
end
37+
3338
private
3439

3540
# @param tag1 [String]

lib/solargraph/pin/method.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,27 @@ def signature_help
166166
end
167167
end
168168

169+
def desc
170+
# ensure the signatures line up when logged
171+
if signatures.length > 1
172+
"\n#{to_rbs}\n"
173+
else
174+
to_rbs
175+
end
176+
end
177+
178+
def to_rbs
179+
return nil if signatures.empty?
180+
181+
rbs = "def #{name}: #{signatures.first.to_rbs}"
182+
signatures[1..].each do |sig|
183+
rbs += "\n"
184+
rbs += (' ' * (4 + name.length))
185+
rbs += "| #{name}: #{sig.to_rbs}"
186+
end
187+
rbs
188+
end
189+
169190
def path
170191
@path ||= "#{namespace}#{(scope == :instance ? '#' : '.')}#{name}"
171192
end

lib/solargraph/pin/namespace.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ def initialize type: :class, visibility: :public, gates: [''], **splat
4040
end
4141
end
4242

43+
def to_rbs
44+
"#{@type.to_s} #{generics_as_rbs}#{return_type.to_rbs}"
45+
end
46+
47+
def desc
48+
if name.nil?
49+
'(top-level)'
50+
else
51+
to_rbs
52+
end
53+
end
54+
4355
def namespace
4456
context.namespace
4557
end

lib/solargraph/pin/parameter.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ def block?
3636
[:block, :blockarg].include?(decl)
3737
end
3838

39+
def to_rbs
40+
case decl
41+
when :optarg
42+
"?#{super}"
43+
when :kwarg
44+
"#{name}: #{return_type.to_rbs}"
45+
when :kwoptarg
46+
"#?{name}: #{return_type.to_rbs}"
47+
when :restarg
48+
"*#{super}"
49+
when :kwrestarg
50+
"**#{super}"
51+
else
52+
super
53+
end
54+
end
55+
3956
def full
4057
case decl
4158
when :optarg

lib/solargraph/pin/signature.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,25 @@ def initialize generics, parameters, return_type, block = nil
2121
@block = block
2222
end
2323

24-
# @return [Array<String>]
2524
def generics
26-
@generics ||= []
25+
@generics ||= [].freeze
26+
end
27+
28+
# @return [String]
29+
def to_rbs
30+
@rbs ||= rbs_generics + '(' + parameters.map { |param| param.to_rbs }.join(', ') + ') ' + (block.nil? ? '' : '{ ' + block.to_rbs + ' } ') + '-> ' + return_type.to_rbs
2731
end
2832

33+
# @return [String]
34+
def rbs_generics
35+
if generics.empty?
36+
return ''
37+
else
38+
return '[' + generics.map { |gen| gen.to_s }.join(', ') + '] '
39+
end
40+
end
41+
42+
# @return [Array<String>]
2943
# @yieldparam [ComplexType]
3044
# @yieldreturn [ComplexType]
3145
# @return [self]

lib/solargraph/source/chain/link.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def resolve api_map, name_pin, locals
3131
[]
3232
end
3333

34+
def inspect
35+
"#{self.class} #{word}"
36+
end
37+
3438
def head?
3539
@head ||= false
3640
end

0 commit comments

Comments
 (0)