Skip to content

Commit 8c5f685

Browse files
committed
Added support for more declarations
- Attributes - Method Alias Added support for source code to be displayed in generated documentation
1 parent e6510f5 commit 8c5f685

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

lib/rdoc/discover.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
require 'rdoc/parser/rbs'
44
rescue Gem::LoadError
55
# Error :sad:
6-
rescue Exception => e
6+
rescue Exception
77
# Exception :sad:
88
end

lib/rdoc/parser/rbs.rb

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ def scan
1414
ast.each do |decl|
1515
parse_member(decl: decl, context: @top_level)
1616
end
17-
klass = @top_level.add_class(RDoc::NormalClass, 'Hello')
18-
comment = RDoc::Comment.new('Hello documentation', @top_level)
19-
klass.add_comment(comment, @top_level)
20-
@stats.add_class(klass)
2117
end
2218

2319
def parse_member(decl:, context:, outer_name: nil)
@@ -32,12 +28,18 @@ def parse_member(decl:, context:, outer_name: nil)
3228
when ::RBS::AST::Members::MethodDefinition
3329
context = @top_level.find_class_or_module outer_name.to_s if outer_name
3430
parse_method_decl(decl: decl, context: context, outer_name: outer_name)
31+
when ::RBS::AST::Members::Alias
32+
context = @top_level.find_class_or_module outer_name.to_s if outer_name
33+
parse_method_alias_decl(decl: decl, context: context, outer_name: outer_name)
34+
when ::RBS::AST::Members::AttrReader, ::RBS::AST::Members::AttrWriter, ::RBS::AST::Members::AttrAccessor
35+
context = @top_level.find_class_or_module outer_name.to_s if outer_name
36+
parse_attr_decl(decl: decl, context: context, outer_name: outer_name)
3537
end
3638
end
3739

3840
def parse_class_decl(decl:, context:, outer_name: nil)
3941
full_name = fully_qualified_name(outer_name: outer_name, decl: decl)
40-
klass = context.add_class(RDoc::NormalClass, full_name.to_s)
42+
klass = context.add_class(RDoc::NormalClass, full_name.to_s, decl.super_class&.name&.to_s || "::Object")
4143
klass.add_comment(construct_comment(context: context, comment: decl.comment.string), context) if decl.comment
4244
decl.members.each { |member| parse_member(decl: member, context: context, outer_name: full_name) }
4345
end
@@ -60,10 +62,37 @@ def parse_method_decl(decl:, context:, outer_name: nil)
6062
method.singleton = decl.singleton?
6163
method.visibility = decl.visibility
6264
method.call_seq = decl.types.map { |type| "#{decl.name.to_s}#{type.to_s}" }.join("\n")
65+
if decl.location
66+
method.start_collecting_tokens
67+
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" })
68+
method.add_token({ line_no: 1, char_no: 1, text: decl.location.source })
69+
method.line = decl.location.start_line if decl.location
70+
end
6371
method.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
6472
context.add_method(method)
6573
end
6674

75+
def parse_method_alias_decl(decl:, context:, outer_name: nil)
76+
alias_def = RDoc::Alias.new(nil, decl.old_name.to_s, decl.new_name.to_s, nil, decl.kind == :singleton)
77+
alias_def.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
78+
context.add_alias(alias_def)
79+
end
80+
81+
def parse_attr_decl(decl:, context:, outer_name: nil)
82+
rw = case decl
83+
when ::RBS::AST::Members::AttrReader
84+
'R'
85+
when ::RBS::AST::Members::AttrWriter
86+
'W'
87+
when ::RBS::AST::Members::AttrAccessor
88+
'RW'
89+
end
90+
attribute = RDoc::Attr.new(nil, decl.name.to_s, rw, nil, decl.kind == :singleton)
91+
attribute.visibility = decl.visibility
92+
attribute.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
93+
context.add_attribute(attribute)
94+
end
95+
6796
private
6897

6998
def construct_comment(context:, comment:)

0 commit comments

Comments
 (0)