Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
- Added code required for RDoc plugin
- Currently documentation supported for classes, modules, constant, methods

Signed-off-by: Sushanth Sathesh Rao <[email protected]>
  • Loading branch information
raosush committed Jun 27, 2022
1 parent 08c251c commit a42b9b6
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
lib/**/*.bundle
lib/**/*.so
lib/**/*.dll
doc/
**/.gem
8 changes: 8 additions & 0 deletions lib/rdoc/discover.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
begin
gem 'rdoc', '~> 6.4.0'
require 'rdoc/parser/rbs'
rescue Gem::LoadError
# Error :sad:
rescue Exception => e
# Exception :sad:
end
82 changes: 82 additions & 0 deletions lib/rdoc/parser/rbs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# frozen_string_literal: true

require 'rbs'

class RDoc::Parser::RBS < RDoc::Parser
parse_files_matching(/\.rbs$/)

def initialize(top_level, file_name, content, options, stats)
super
end

def scan
ast = ::RBS::Parser.parse_signature(@content)
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)
case decl
when ::RBS::AST::Declarations::Class
parse_class_decl(decl: decl, context: context, outer_name: outer_name)
when ::RBS::AST::Declarations::Module
parse_module_decl(decl: decl, context: context, outer_name: outer_name)
when ::RBS::AST::Declarations::Constant
context = @top_level.find_class_or_module outer_name.to_s if outer_name
parse_constant_decl(decl: decl, context: context, outer_name: outer_name)
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)
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.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

def parse_module_decl(decl:, context:, outer_name: nil)
full_name = fully_qualified_name(outer_name: outer_name, decl: decl)
kmodule = context.add_module(RDoc::NormalModule, full_name.to_s)
kmodule.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: outer_name) }
end

def parse_constant_decl(decl:, context:, outer_name: nil)
comment = decl.comment ? construct_comment(context: context, comment: decl.comment.string) : nil
constant = RDoc::Constant.new(decl.name.to_s, decl.type.to_s, comment)
context.add_constant(constant)
end

def parse_method_decl(decl:, context:, outer_name: nil)
method = RDoc::AnyMethod.new(nil, decl.name.to_s)
method.singleton = decl.singleton?
method.visibility = decl.visibility
method.call_seq = decl.types.map { |type| "#{decl.name.to_s}#{type.to_s}" }.join("\n")
method.comment = construct_comment(context: context, comment: decl.comment.string) if decl.comment
context.add_method(method)
end

private

def construct_comment(context:, comment:)
comment = RDoc::Comment.new(comment, context)
comment.format = "markdown"
comment
end

def fully_qualified_name(outer_name:, decl:)
if outer_name
(outer_name + decl.name)
else
decl.name
end
end
end

0 comments on commit a42b9b6

Please sign in to comment.