Skip to content

Commit

Permalink
Added support for more declarations
Browse files Browse the repository at this point in the history
- Attributes
- Method Alias

Added support for source code to be displayed in generated documentation
  • Loading branch information
raosush committed Jun 27, 2022
1 parent a42b9b6 commit 47150aa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/rdoc/discover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
require 'rdoc/parser/rbs'
rescue Gem::LoadError
# Error :sad:
rescue Exception => e
rescue Exception
# Exception :sad:
end
39 changes: 34 additions & 5 deletions lib/rdoc/parser/rbs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ def scan
ast.each do |decl|
parse_member(decl: decl, context: @top_level)
end
klass = @top_level.add_class(RDoc::NormalClass, 'Hello')
comment = RDoc::Comment.new('Hello documentation', @top_level)
klass.add_comment(comment, @top_level)
@stats.add_class(klass)
end

def parse_member(decl:, context:, outer_name: nil)
Expand All @@ -32,12 +28,18 @@ def parse_member(decl:, context:, outer_name: nil)
when ::RBS::AST::Members::MethodDefinition
context = @top_level.find_class_or_module outer_name.to_s if outer_name
parse_method_decl(decl: decl, context: context, outer_name: outer_name)
when ::RBS::AST::Members::Alias
context = @top_level.find_class_or_module outer_name.to_s if outer_name
parse_method_alias_decl(decl: decl, context: context, outer_name: outer_name)
when ::RBS::AST::Members::AttrReader, ::RBS::AST::Members::AttrWriter, ::RBS::AST::Members::AttrAccessor
context = @top_level.find_class_or_module outer_name.to_s if outer_name
parse_attr_decl(decl: decl, context: context, outer_name: outer_name)
end
end

def parse_class_decl(decl:, context:, outer_name: nil)
full_name = fully_qualified_name(outer_name: outer_name, decl: decl)
klass = context.add_class(RDoc::NormalClass, full_name.to_s)
klass = context.add_class(RDoc::NormalClass, full_name.to_s, decl.super_class&.name&.to_s || "::Object")
klass.add_comment(construct_comment(context: context, comment: decl.comment.string), context) if decl.comment
decl.members.each { |member| parse_member(decl: member, context: context, outer_name: full_name) }
end
Expand All @@ -60,10 +62,37 @@ def parse_method_decl(decl:, context:, outer_name: nil)
method.singleton = decl.singleton?
method.visibility = decl.visibility
method.call_seq = decl.types.map { |type| "#{decl.name.to_s}#{type.to_s}" }.join("\n")
if decl.location
method.start_collecting_tokens
method.add_token({ line_no: 1, char_no: 1, kind: :on_comment, text: "# File #{@top_level.relative_name}, line(s) #{decl.location.start_line}:#{decl.location.end_line}\n" })
method.add_token({ line_no: 1, char_no: 1, text: decl.location.source })
method.line = decl.location.start_line if decl.location
end
method.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
context.add_method(method)
end

def parse_method_alias_decl(decl:, context:, outer_name: nil)
alias_def = RDoc::Alias.new(nil, decl.old_name.to_s, decl.new_name.to_s, nil, decl.kind == :singleton)
alias_def.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
context.add_alias(alias_def)
end

def parse_attr_decl(decl:, context:, outer_name: nil)
rw = case decl
when ::RBS::AST::Members::AttrReader
'R'
when ::RBS::AST::Members::AttrWriter
'W'
when ::RBS::AST::Members::AttrAccessor
'RW'
end
attribute = RDoc::Attr.new(nil, decl.name.to_s, rw, nil, decl.kind == :singleton)
attribute.visibility = decl.visibility
attribute.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
context.add_attribute(attribute)
end

private

def construct_comment(context:, comment:)
Expand Down

0 comments on commit 47150aa

Please sign in to comment.