diff --git a/lib/solargraph/api_map/index.rb b/lib/solargraph/api_map/index.rb index 9c5dc2c53..ad219ad71 100644 --- a/lib/solargraph/api_map/index.rb +++ b/lib/solargraph/api_map/index.rb @@ -139,10 +139,14 @@ def map_overrides ovr.tags.each do |tag| pin.docstring.add_tag(tag) redefine_return_type pin, tag - if new_pin - new_pin.docstring.add_tag(tag) - redefine_return_type new_pin, tag - end + pin.reset_generated! + + next unless new_pin + + new_pin.docstring.add_tag(tag) + redefine_return_type new_pin, tag + new_pin.comments = new_pin.docstring.to_raw + "\n" + new_pin.reset_generated! end end end @@ -159,7 +163,6 @@ def redefine_return_type pin, tag pin.signatures.each do |sig| sig.instance_variable_set(:@return_type, ComplexType.try_parse(tag.type)) end - pin.reset_generated! end end end diff --git a/lib/solargraph/pin/base.rb b/lib/solargraph/pin/base.rb index c0eecabb2..62e07640b 100644 --- a/lib/solargraph/pin/base.rb +++ b/lib/solargraph/pin/base.rb @@ -545,9 +545,9 @@ def probed? # @param api_map [ApiMap] # @return [self] def realize api_map - return self if return_type.defined? + return self if return_type.defined? && return_type.all_rooted? type = typify(api_map) - return proxy(type) if type.defined? + return proxy(type) if type.defined? && type.all_rooted? type = probe(api_map) return self if type.undefined? result = proxy(type) @@ -565,6 +565,7 @@ def proxy return_type result = dup result.return_type = return_type result.proxied = true + result.reset_generated! result end @@ -639,6 +640,8 @@ def all_location_text # @return [void] def reset_generated! + @docstring = nil + @directives = nil end protected @@ -671,6 +674,15 @@ def parse_comments @docstring = parse.to_docstring @directives = parse.directives end + if @return_type&.defined? + @docstring ||= Solargraph::Source.parse_docstring("\n").to_docstring + rooted_types = @return_type.items.map(&:rooted_tag) + if @docstring.tags(:return)&.length == 1 + @docstring.tag(:return).types = rooted_types + else + @docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_types)) + end + end end # True if two docstrings have the same tags, regardless of any other diff --git a/lib/solargraph/pin/method.rb b/lib/solargraph/pin/method.rb index 295b72fbf..eb6cb0608 100644 --- a/lib/solargraph/pin/method.rb +++ b/lib/solargraph/pin/method.rb @@ -12,6 +12,8 @@ class Method < Callable attr_writer :signatures + attr_writer :comments + # @return [Parser::AST::Node] attr_reader :node @@ -127,7 +129,7 @@ def reset_generated! block&.reset_generated! @signatures&.each(&:reset_generated!) signature_help = nil - documentation = nil + @documentation = nil end def all_rooted? diff --git a/lib/solargraph/rbs_map/conversions.rb b/lib/solargraph/rbs_map/conversions.rb index f941aeaee..301d17cbd 100644 --- a/lib/solargraph/rbs_map/conversions.rb +++ b/lib/solargraph/rbs_map/conversions.rb @@ -250,16 +250,15 @@ def create_constant(name, tag, comments, decl, base = nil) name = parts.first closure = Solargraph::Pin::ROOT_PIN end + return_type = ComplexType.parse(tag).force_rooted constant_pin = Solargraph::Pin::Constant.new( name: name, closure: closure, type_location: location_decl_to_pin_location(decl.location), comments: comments, + return_type: return_type, source: :rbs ) - tag = "#{base}<#{tag}>" if base - rooted_tag = ComplexType.parse(tag).force_rooted.rooted_tags - constant_pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag)) constant_pin end @@ -295,15 +294,15 @@ def constant_decl_to_pin decl def global_decl_to_pin decl closure = Solargraph::Pin::ROOT_PIN name = decl.name.to_s + return_type = ComplexType.parse(other_type_to_tag(decl.type)) pin = Solargraph::Pin::GlobalVariable.new( name: name, closure: closure, comments: decl.comment&.string, type_location: location_decl_to_pin_location(decl.location), + return_type: return_type, source: :rbs ) - rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags - pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag)) pins.push pin end @@ -516,6 +515,7 @@ def attr_reader_to_pin(decl, closure, context) name = decl.name.to_s final_scope = decl.kind == :instance ? :instance : :class visibility = calculate_method_visibility(decl, context, closure, final_scope, name) + return_type = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted pin = Solargraph::Pin::Method.new( name: name, type_location: location_decl_to_pin_location(decl.location), @@ -524,10 +524,9 @@ def attr_reader_to_pin(decl, closure, context) scope: final_scope, attribute: true, visibility: visibility, + return_type: return_type, source: :rbs ) - rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags - pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag)) logger.debug { "Conversions#attr_reader_to_pin(name=#{name.inspect}, visibility=#{visibility.inspect}) => #{pin.inspect}" } pins.push pin end @@ -541,6 +540,7 @@ def attr_writer_to_pin(decl, closure, context) name = "#{decl.name.to_s}=" visibility = calculate_method_visibility(decl, context, closure, final_scope, name) type_location = location_decl_to_pin_location(decl.location) + return_type = ComplexType.try_parse(other_type_to_tag(decl.type)).force_rooted pin = Solargraph::Pin::Method.new( name: name, type_location: type_location, @@ -550,18 +550,17 @@ def attr_writer_to_pin(decl, closure, context) scope: final_scope, attribute: true, visibility: visibility, + return_type: return_type, source: :rbs ) pin.parameters << Solargraph::Pin::Parameter.new( name: 'value', - return_type: ComplexType.try_parse(other_type_to_tag(decl.type)).force_rooted, + return_type: return_type, source: :rbs, closure: pin, type_location: type_location ) - rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags - pin.docstring.add_tag(YARD::Tags::Tag.new(:return, '', rooted_tag)) pins.push pin end @@ -578,15 +577,15 @@ def attr_accessor_to_pin(decl, closure, context) # @param closure [Pin::Namespace] # @return [void] def ivar_to_pin(decl, closure) + return_type = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted pin = Solargraph::Pin::InstanceVariable.new( name: decl.name.to_s, closure: closure, type_location: location_decl_to_pin_location(decl.location), comments: decl.comment&.string, + return_type: return_type, source: :rbs ) - rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags - pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag)) pins.push pin end @@ -595,15 +594,15 @@ def ivar_to_pin(decl, closure) # @return [void] def cvar_to_pin(decl, closure) name = decl.name.to_s + return_type = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted pin = Solargraph::Pin::ClassVariable.new( name: name, closure: closure, comments: decl.comment&.string, type_location: location_decl_to_pin_location(decl.location), + return_type: return_type, source: :rbs ) - rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags - pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag)) pins.push pin end @@ -612,15 +611,15 @@ def cvar_to_pin(decl, closure) # @return [void] def civar_to_pin(decl, closure) name = decl.name.to_s + return_type = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted pin = Solargraph::Pin::InstanceVariable.new( name: name, closure: closure, comments: decl.comment&.string, type_location: location_decl_to_pin_location(decl.location), + return_type: return_type, source: :rbs ) - rooted_tag = ComplexType.parse(other_type_to_tag(decl.type)).force_rooted.rooted_tags - pin.docstring.add_tag(YARD::Tags::Tag.new(:type, '', rooted_tag)) pins.push pin end