Class: RDoc::Parser::Ruby
- Inherits:
-
Parser
- Object
- Parser
- RDoc::Parser::Ruby
- Defined in:
- lib/rdoc/parser/ruby.rb
Overview
Extracts code elements from a source file returning a TopLevel object containing the constituent file elements.
RubyParser understands how to document:
- classes
- modules
- methods
- constants
- aliases
- private, public, protected
- private_class_method, public_class_method
- private_constant, public_constant
- module_function
- attr, attr_reader, attr_writer, attr_accessor
- extra accessors given on the command line
- metaprogrammed methods
- require
- include
Method Arguments
The parser extracts the arguments from the method definition. You can override this with a custom argument definition using the :args: directive:
##
# This method tries over and over until it is tired
def go_go_go(thing_to_try, tries = 10) # :args: thing_to_try
puts thing_to_try
go_go_go thing_to_try, tries - 1
end
If you have a more-complex set of overrides you can use the :call-seq: directive:
##
# This method can be called with a range or an offset and length
#
# :call-seq:
# my_method(Range)
# my_method(offset, length)
def my_method(*args)
end
The parser extracts yield expressions from method bodies to gather the
yielded argument names. If your method manually calls a block instead of
yielding or you want to override the discovered argument names use
the :yields: directive:
##
# My method is awesome
def my_method(&block) # :yields: happy, times
block.call 1, 2
end
Metaprogrammed Methods
To pick up a metaprogrammed method, the parser looks for a comment starting with '##' before a metaprogramming method call:
##
# This is a meta-programmed method!
add_my_method :meta_method, :arg1, :arg2
The parser looks at the first argument to determine the name, in this example, :meta_method. If a name cannot be found, a warning is printed and 'unknown' is used.
You can force the name of a method using the :method: directive:
##
# :method: some_method!
By default, meta-methods are instance methods. To indicate that a method is a singleton method instead use the :singleton-method: directive:
##
# :singleton-method:
You can also use the :singleton-method: directive with a name:
##
# :singleton-method: some_method!
You can define arguments for metaprogrammed methods via either the :call-seq:, :arg: or :args: directives.
Additionally you can mark a method as an attribute by using :attr:, :attr_reader:, :attr_writer: or :attr_accessor:. Just like for :method:, the name is optional.
##
# :attr_reader: my_attr_name
Hidden methods and attributes
You can provide documentation for methods that don't appear using the :method:, :singleton-method: and :attr: directives:
##
# :attr_writer: ghost_writer
# There is an attribute here, but you can't see it!
##
# :method: ghost_method
# There is a method here, but you can't see it!
##
# this is a comment for a regular method
def regular_method() end
Note that by default, the :method: directive will be ignored if there is a standard rdocable item following it.
Defined Under Namespace
Classes: RDocVisitor
Constant Summary collapse
- RBS_SIG_LINE =
Matches an RBS inline type annotation line: #: followed by whitespace
/\A#:\s/
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
Returns the value of attribute container.
-
#in_proc_block ⇒ Object
readonly
Returns the value of attribute in_proc_block.
-
#singleton ⇒ Object
readonly
Returns the value of attribute singleton.
-
#visibility ⇒ Object
Returns the value of attribute visibility.
Instance Method Summary collapse
-
#add_alias_method(old_name, new_name, line_no) ⇒ Object
Handles
alias foo barandalias_method :foo, :bar. -
#add_attributes(names, rw, line_no) ⇒ Object
Handles
attr :a, :b,attr_reader :a, :b,attr_writer :a, :bandattr_accessor :a, :b. -
#add_constant(constant_name, rhs_name, start_line, end_line, alias_path: nil) ⇒ Object
Adds a constant.
-
#add_extends(names, line_no) ⇒ Object
Handle
extend Foo, Bar. -
#add_includes(names, line_no) ⇒ Object
Handle
include Foo, Bar. -
#add_includes_extends(names, rdoc_class, line_no) ⇒ Object
Adds includes/extends.
-
#add_method(method_name, receiver_name:, receiver_fallback_type:, visibility:, singleton:, params:, calls_super:, block_params:, node_id:, start_line:, args_end_line:, end_line:) ⇒ Object
Adds a method defined by
defsyntax. -
#add_module_or_class(module_name, start_line, end_line, is_class: false, superclass_name: nil, superclass_expr: nil) ⇒ Object
Adds module or class.
-
#apply_document_control_directive(directives) ⇒ Object
Applies document control directives (:startdoc:, :stopdoc: and :enddoc:) to the current lexical scope.
-
#argument_name(argument_node) ⇒ Object
:nodoc:.
-
#call_node_name_argument(call_node) ⇒ Object
:nodoc:.
-
#call_node_name_arguments(call_node) ⇒ Object
:nodoc:.
-
#change_method_to_module_function(names) ⇒ Object
Handles
module_function :foo, :bar. -
#change_method_visibility(names, visibility, singleton: @singleton) ⇒ Object
Handles
public :foo, :barprivate :foo, :barandprotected :foo, :bar. -
#consecutive_comment(line_no) ⇒ Object
Returns consecutive comment linked to the given line number.
-
#document_suppressed? ⇒ Boolean
Returns true if code objects at the current position should not be documented, that is, inside a :stopdoc: or :enddoc: region.
-
#extract_section_comment(comment_text, prefix_line_count) ⇒ Object
Extracts the comment for this section from the normalized comment block.
-
#find_or_create_lexical_constant_owner_name(constant_path) ⇒ Object
Returns a pair of owner module and constant name from a given constant path using Ruby lexical nesting.
-
#find_or_create_lexical_module_path(module_name, create_mode) ⇒ Object
Find or create module or class from a given module name using Ruby lexical nesting.
-
#handle_code_object_directives(code_object, directives) ⇒ Object
:nodoc:.
-
#handle_meta_method_comment(comment, directives, node) ⇒ Object
Handles meta method comments.
-
#handle_modifier_directive(code_object, line_no) ⇒ Object
:nodoc:.
-
#handle_standalone_consecutive_comment_directive(comment, directives, start_with_sharp_sharp, line_no, start_line) ⇒ Object
:nodoc:.
-
#has_modifier_nodoc?(line_no) ⇒ Boolean
:nodoc:.
-
#initialize(top_level, content, options, stats) ⇒ Ruby
constructor
A new instance of Ruby.
-
#mark_container_documentable(container) ⇒ Object
Makes a container that was created inside a :stopdoc:/:enddoc: region (thus ignored) documentable again when it receives documentable contents outside the region, possibly from another file.
-
#normal_comment_treat_as_ghost_method_for_now?(directives, line_no) ⇒ Boolean
:nodoc:.
-
#parse_comment_text_to_directives(comment_text, start_line) ⇒ Object
Parses comment text and returns
[RDoc::Comment, directives, type_signature_lines], ornilif the comment is a section header (which has no associated code object). -
#parse_comment_tomdoc(container, comment, line_no, start_line) ⇒ Object
Creates an RDoc::Method on
containerfromcommentif there is a Signature section in the comment. -
#prepare_comments(comments) ⇒ Object
Prepares comments for processing.
-
#prepare_line_nodes(node) ⇒ Object
Assign AST node to a line.
-
#process_comments_until(line_no_until) ⇒ Object
Processes consecutive comments that were not linked to any documentable code until the given line number.
-
#record_location(container) ⇒ Object
Records the location of this
containerin the file for this parser and adds it to the list of classes and modules in the file. -
#resolve_constant_path(constant_path) ⇒ Object
Resolves constant path to a full path by searching module nesting.
-
#scan ⇒ Object
Scans this Ruby file for Ruby constructs.
-
#should_document?(code_object) ⇒ Boolean
:nodoc:.
-
#skip_comments_until(line_no_until) ⇒ Object
Skips all undocumentable consecutive comments until the given line number.
-
#with_container(container, singleton: false) ⇒ Object
Dive into another container.
-
#with_in_proc_block ⇒ Object
Suppress
extendandincludewithin block because they might be a metaprogramming block example:Module.new { include M }M.module_eval { include N }.
Constructor Details
#initialize(top_level, content, options, stats) ⇒ Ruby
Returns a new instance of Ruby.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/rdoc/parser/ruby.rb', line 139 def initialize(top_level, content, , stats) super content = handle_tab_width(content) @size = 0 @token_listeners = nil content = Encoding.remove_magic_comment content @content = content @colorizer_context = Parser::RubyColorizer::DeferredContext.new(content) @markup = @options.markup @track_visibility = :nodoc != @options.visibility @encoding = @options.encoding @module_nesting = [[top_level, false]] @container = top_level @visibility = :public @singleton = false @in_proc_block = false @doc_state = :startdoc end |
Instance Attribute Details
#container ⇒ Object (readonly)
Returns the value of attribute container.
137 138 139 |
# File 'lib/rdoc/parser/ruby.rb', line 137 def container @container end |
#in_proc_block ⇒ Object (readonly)
Returns the value of attribute in_proc_block.
137 138 139 |
# File 'lib/rdoc/parser/ruby.rb', line 137 def in_proc_block @in_proc_block end |
#singleton ⇒ Object (readonly)
Returns the value of attribute singleton.
137 138 139 |
# File 'lib/rdoc/parser/ruby.rb', line 137 def singleton @singleton end |
#visibility ⇒ Object
Returns the value of attribute visibility.
136 137 138 |
# File 'lib/rdoc/parser/ruby.rb', line 136 def visibility @visibility end |
Instance Method Details
#add_alias_method(old_name, new_name, line_no) ⇒ Object
Handles alias foo bar and alias_method :foo, :bar
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
# File 'lib/rdoc/parser/ruby.rb', line 621 def add_alias_method(old_name, new_name, line_no) comment, directives = consecutive_comment(line_no) apply_document_control_directive(directives) if directives handle_code_object_directives(@container, directives) if directives return if document_suppressed? a = Alias.new(old_name, new_name, comment, singleton: @singleton) handle_modifier_directive(a, line_no) a.store = @store a.line = line_no record_location(a) if should_document?(a) mark_container_documentable(@container) @container.add_alias(a) end end |
#add_attributes(names, rw, line_no) ⇒ Object
Handles attr :a, :b, attr_reader :a, :b, attr_writer :a, :b and attr_accessor :a, :b
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
# File 'lib/rdoc/parser/ruby.rb', line 640 def add_attributes(names, rw, line_no) comment, directives, type_signature_lines = consecutive_comment(line_no) apply_document_control_directive(directives) if directives handle_code_object_directives(@container, directives) if directives return if document_suppressed? return unless @container.document_children names.each do |symbol| a = Attr.new(symbol.to_s, rw, comment, singleton: @singleton) a.store = @store a.line = line_no a.type_signature_lines = type_signature_lines a.visibility = visibility record_location(a) handle_modifier_directive(a, line_no) if should_document?(a) @container.add_attribute(a) mark_container_documentable(@container) end end end |
#add_constant(constant_name, rhs_name, start_line, end_line, alias_path: nil) ⇒ Object
Adds a constant
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 |
# File 'lib/rdoc/parser/ruby.rb', line 837 def add_constant(constant_name, rhs_name, start_line, end_line, alias_path: nil) comment, directives = consecutive_comment(start_line) apply_document_control_directive(directives) if directives handle_code_object_directives(@container, directives) if directives return if document_suppressed? owner, name = find_or_create_lexical_constant_owner_name(constant_name) return unless owner constant = Constant.new(name, rhs_name, comment) constant.store = @store constant.line = start_line constant.is_alias_for_path = alias_path handle_modifier_directive(constant, start_line) handle_modifier_directive(constant, end_line) # A constant marked :nodoc: must not make an ignored owner documentable mark_container_documentable(owner) if constant.document_self && owner.is_a?(ClassModule) record_location(constant) owner.add_constant(constant) return unless alias_path mod = if alias_path.start_with?('::') @store.find_class_or_module(alias_path) else full_name = resolve_constant_path(alias_path) @store.find_class_or_module(full_name) end if mod && constant.document_self a = owner.add_module_alias(mod, constant, @top_level) a.store = @store a.line = start_line record_location(a) end end |
#add_extends(names, line_no) ⇒ Object
Handle extend Foo, Bar
689 690 691 |
# File 'lib/rdoc/parser/ruby.rb', line 689 def add_extends(names, line_no) # :nodoc: add_includes_extends(names, Extend, line_no) end |
#add_includes(names, line_no) ⇒ Object
Handle include Foo, Bar
683 684 685 |
# File 'lib/rdoc/parser/ruby.rb', line 683 def add_includes(names, line_no) # :nodoc: add_includes_extends(names, Include, line_no) end |
#add_includes_extends(names, rdoc_class, line_no) ⇒ Object
Adds includes/extends. Module name is resolved to full before adding.
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 |
# File 'lib/rdoc/parser/ruby.rb', line 664 def add_includes_extends(names, rdoc_class, line_no) # :nodoc: comment, directives = consecutive_comment(line_no) apply_document_control_directive(directives) if directives handle_code_object_directives(@container, directives) if directives return if document_suppressed? mark_container_documentable(@container) names.each do |name| resolved_name = resolve_constant_path(name) ie = @container.add(rdoc_class, resolved_name || name, '') ie.store = @store ie.line = line_no ie.comment = comment record_location(ie) end end |
#add_method(method_name, receiver_name:, receiver_fallback_type:, visibility:, singleton:, params:, calls_super:, block_params:, node_id:, start_line:, args_end_line:, end_line:) ⇒ Object
Adds a method defined by def syntax
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
# File 'lib/rdoc/parser/ruby.rb', line 695 def add_method(method_name, receiver_name:, receiver_fallback_type:, visibility:, singleton:, params:, calls_super:, block_params:, node_id:, start_line:, args_end_line:, end_line:) comment, directives, type_signature_lines = consecutive_comment(start_line) apply_document_control_directive(directives) if directives handle_code_object_directives(@container, directives) if directives # Resolve receiver after applying directives so that a namespace created # here is marked as ignored when the comment starts a :stopdoc: region receiver = receiver_name ? find_or_create_lexical_module_path(receiver_name, receiver_fallback_type) : @container internal_add_method( method_name, receiver, comment: comment, directives: directives, modifier_comment_lines: [start_line, args_end_line, end_line].uniq, line_no: start_line, visibility: visibility, singleton: singleton, params: params, calls_super: calls_super, block_params: block_params, node_id: node_id, type_signature_lines: type_signature_lines ) end |
#add_module_or_class(module_name, start_line, end_line, is_class: false, superclass_name: nil, superclass_expr: nil) ⇒ Object
Adds module or class
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 |
# File 'lib/rdoc/parser/ruby.rb', line 874 def add_module_or_class(module_name, start_line, end_line, is_class: false, superclass_name: nil, superclass_expr: nil) comment, directives = consecutive_comment(start_line) apply_document_control_directive(directives) if directives handle_code_object_directives(@container, directives) if directives return unless @container.document_children owner, name = find_or_create_lexical_constant_owner_name(module_name) return unless owner if is_class # RDoc::NormalClass resolves superclass name despite of the lack of module nesting information. # We need to fix it when RDoc::NormalClass resolved to a wrong constant name if superclass_name superclass_full_path = resolve_constant_path(superclass_name) superclass = @store.find_class_or_module(superclass_full_path) if superclass_full_path superclass_full_path ||= superclass_name superclass_full_path = superclass_full_path.sub(/^::/, '') end # add_class should be done after resolving superclass mod = owner.classes_hash[name] unless mod # add_class may return an existing class created by another file # (in_files is not empty then), which must not be ignored here mod = owner.add_class(NormalClass, name, superclass_name || superclass_expr || '::Object') mod.ignore if document_suppressed? && mod.in_files.empty? end # Superclass with the same full path and superclass for BasicObject are not allowed if superclass_name && mod.full_name != superclass_full_path && mod.full_name != 'BasicObject' if superclass mod.superclass = superclass elsif mod.superclass.nil? || (mod.superclass.is_a?(String) || mod.superclass.name == 'Object') && mod.superclass != superclass_full_path mod.superclass = superclass_full_path end end else mod = owner.modules_hash[name] unless mod mod = owner.add_module(NormalModule, name) mod.ignore if document_suppressed? && mod.in_files.empty? end end mod.store = @store mod.line = start_line handle_modifier_directive(mod, start_line) handle_modifier_directive(mod, end_line) unless document_suppressed? # In a :stopdoc:/:enddoc: region, the container is still created as a # namespace but is not recorded to this file nor documented. # The body is also visited: an inner :startdoc: re-enables documentation # in a :stopdoc: region (not in an :enddoc: region), and nested # namespaces need to be created for later promotion from other files if mod.ignored? # Promotes the owner chain too, unless mod received :nodoc: mark_container_documentable(mod) else # A class/module marked :nodoc: must not make an ignored owner documentable mark_container_documentable(owner) if mod.document_self && owner.is_a?(ClassModule) record_location(mod) end mod.add_comment(comment, @top_level) if comment end mod end |
#apply_document_control_directive(directives) ⇒ Object
Applies document control directives (:startdoc:, :stopdoc: and :enddoc:) to the current lexical scope. The state is restored when the enclosing class/module scope is closed.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rdoc/parser/ruby.rb', line 165 def apply_document_control_directive(directives) directives.each do |directive, (_param, line)| case directive when 'startdoc', 'stopdoc' # :enddoc: cannot be cancelled within the scope, even by :startdoc: if @doc_state == :enddoc @options.warn "#{@top_level.relative_name}:#{line}: :startdoc: is ignored after :enddoc:" if directive == 'startdoc' next end @doc_state = directive.to_sym if directive == 'startdoc' && !@container.ignored? # Compatibility: `module Net #:nodoc:` followed by :stopdoc:/:startdoc: # regions is a common pattern that expects :startdoc: to make the # container documentable again. Containers ignored here were created # in a suppressed region and need documentable contents to revive. @container.start_doc @container.force_documentation = true end when 'enddoc' @doc_state = :enddoc end end end |
#argument_name(argument_node) ⇒ Object
:nodoc:
401 402 403 404 405 406 407 408 |
# File 'lib/rdoc/parser/ruby.rb', line 401 def argument_name(argument_node) # :nodoc: case argument_node when Prism::SymbolNode argument_node.value when Prism::StringNode argument_node.unescaped end end |
#call_node_name_argument(call_node) ⇒ Object
:nodoc:
396 397 398 399 |
# File 'lib/rdoc/parser/ruby.rb', line 396 def call_node_name_argument(call_node) # :nodoc: return unless call_node.arguments argument_name(call_node.arguments.arguments.first) end |
#call_node_name_arguments(call_node) ⇒ Object
:nodoc:
390 391 392 393 394 |
# File 'lib/rdoc/parser/ruby.rb', line 390 def call_node_name_arguments(call_node) # :nodoc: return unless arguments_node = call_node.arguments names = arguments_node.arguments.filter_map { |arg| argument_name(arg) } names unless names.empty? end |
#change_method_to_module_function(names) ⇒ Object
Handles module_function :foo, :bar
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
# File 'lib/rdoc/parser/ruby.rb', line 586 def change_method_to_module_function(names) @container.set_visibility_for(names, :private, false) # In a :stopdoc:/:enddoc: region, the visibility of instance methods still # changes but the singleton method copies must not be documented return if document_suppressed? new_methods = [] @container.methods_matching(names) do |m| s_m = m.dup record_location(s_m) s_m.singleton = true new_methods << s_m end new_methods.each do |method| method.visibility = :public case method when AnyMethod @container.add_method(method) when Attr @container.add_attribute(method) end end end |
#change_method_visibility(names, visibility, singleton: @singleton) ⇒ Object
Handles public :foo, :bar private :foo, :bar and protected :foo, :bar
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
# File 'lib/rdoc/parser/ruby.rb', line 559 def change_method_visibility(names, visibility, singleton: @singleton) new_methods = [] @container.methods_matching(names, singleton) do |m| if m.parent != @container # A copy of an ancestor's method must not be documented # in a :stopdoc:/:enddoc: region next if document_suppressed? m = m.dup record_location(m) new_methods << m else m.visibility = visibility end end new_methods.each do |method| method.visibility = visibility case method when AnyMethod @container.add_method(method) when Attr @container.add_attribute(method) end end end |
#consecutive_comment(line_no) ⇒ Object
Returns consecutive comment linked to the given line number
517 518 519 520 521 |
# File 'lib/rdoc/parser/ruby.rb', line 517 def consecutive_comment(line_no) return unless @unprocessed_comments.first&.first == line_no _line_no, start_line, text = @unprocessed_comments.shift parse_comment_text_to_directives(text, start_line) end |
#document_suppressed? ⇒ Boolean
Returns true if code objects at the current position should not be documented, that is, inside a :stopdoc: or :enddoc: region.
192 193 194 |
# File 'lib/rdoc/parser/ruby.rb', line 192 def document_suppressed? @track_visibility && @doc_state != :startdoc end |
#extract_section_comment(comment_text, prefix_line_count) ⇒ Object
Extracts the comment for this section from the normalized comment block. Removes all lines before the line that contains :section: If the comment also ends with the same content, remove it as well
549 550 551 552 553 554 555 |
# File 'lib/rdoc/parser/ruby.rb', line 549 def extract_section_comment(comment_text, prefix_line_count) # :nodoc: prefix = comment_text.lines[0...prefix_line_count].join comment_text.delete_prefix!(prefix) # Comment is already normalized and doesn't end with a newline comment_text.delete_suffix!(prefix.chomp) comment_text end |
#find_or_create_lexical_constant_owner_name(constant_path) ⇒ Object
Returns a pair of owner module and constant name from a given constant path using Ruby lexical nesting. Creates owner module if it does not exist.
821 822 823 824 825 826 827 828 829 830 831 832 833 |
# File 'lib/rdoc/parser/ruby.rb', line 821 def find_or_create_lexical_constant_owner_name(constant_path) const_path, colon, name = constant_path.rpartition('::') if colon.empty? # class Foo # Within `class C` or `module C`, owner is C(== current container) # Within `class <<C`, owner is C.singleton_class # but RDoc don't track constants of a singleton class of module [(@singleton ? nil : @container), name] elsif const_path.empty? # class ::Foo [@top_level, name] else # `class Foo::Bar` or `class ::Foo::Bar` [find_or_create_lexical_module_path(const_path, :module), name] end end |
#find_or_create_lexical_module_path(module_name, create_mode) ⇒ Object
Find or create module or class from a given module name using Ruby lexical
nesting. If module or class does not exist, creates a module or a class
according to create_mode argument.
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 |
# File 'lib/rdoc/parser/ruby.rb', line 766 def find_or_create_lexical_module_path(module_name, create_mode) root_name, *path, name = module_name.split('::') add_module = ->(mod, name, mode) { created = case mode when :class mod.add_class(NormalClass, name, 'Object').tap { |m| m.store = @store } when :module mod.add_module(NormalModule, name).tap { |m| m.store = @store } end # add_class/add_module may return an existing object created by another # file (in_files is not empty then), which must not be ignored here. # Documentable again when reopened or receiving contents outside the region. created.ignore if document_suppressed? && created.in_files.empty? created } if root_name.empty? mod = @top_level else @module_nesting.reverse_each do |nesting, singleton| next if singleton mod = nesting.get_module_named(root_name) break if mod # If a constant is found and it is not a module or class, RDoc can't document about it. # Return an anonymous module to avoid wrong document creation. return NormalModule.new(nil) if nesting.find_constant_named(root_name) end last_nesting, = @module_nesting.reverse_each.find { |_, singleton| !singleton } return mod || add_module.call(last_nesting, root_name, create_mode) unless name mod ||= add_module.call(last_nesting, root_name, :module) end path.each do |name| mod = mod.get_module_named(name) || add_module.call(mod, name, :module) end mod.get_module_named(name) || add_module.call(mod, name, create_mode) end |
#handle_code_object_directives(code_object, directives) ⇒ Object
:nodoc:
610 611 612 613 614 615 616 617 |
# File 'lib/rdoc/parser/ruby.rb', line 610 def handle_code_object_directives(code_object, directives) # :nodoc: directives.each do |directive, (param)| # startdoc/stopdoc/enddoc are handled by apply_document_control_directive. # They control the lexical scope of the parser, not the code object. next if directive == 'startdoc' || directive == 'stopdoc' || directive == 'enddoc' @preprocess.handle_directive('', directive, param, code_object) end end |
#handle_meta_method_comment(comment, directives, node) ⇒ Object
Handles meta method comments
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/rdoc/parser/ruby.rb', line 412 def (comment, directives, node) apply_document_control_directive(directives) handle_code_object_directives(@container, directives) is_call_node = node.is_a?(Prism::CallNode) singleton_method = false visibility = @visibility attributes = rw = line_no = method_name = nil directives.each do |directive, (param, line)| case directive when 'attr', 'attr_reader', 'attr_writer', 'attr_accessor' attributes = [param] if param attributes ||= call_node_name_arguments(node) || [] if is_call_node rw = directive == 'attr_writer' ? 'W' : directive == 'attr_accessor' ? 'RW' : 'R' when 'method' method_name = param if param line_no = line when 'singleton-method' method_name = param if param line_no = line singleton_method = true visibility = :public end end return if document_suppressed? if attributes attributes.each do |attr| a = Attr.new(attr, rw, comment, singleton: @singleton) a.store = @store a.line = line_no a.visibility = visibility record_location(a) @container.add_attribute(a) mark_container_documentable(@container) end elsif line_no || node method_name ||= call_node_name_argument(node) if is_call_node line_no = node.location.start_line if node internal_add_method( method_name, @container, comment: comment, directives: directives, line_no: line_no, visibility: visibility, singleton: @singleton || singleton_method, params: nil, calls_super: false, block_params: nil, node_id: node&.node_id, ) end end |
#handle_modifier_directive(code_object, line_no) ⇒ Object
:nodoc:
383 384 385 386 387 388 |
# File 'lib/rdoc/parser/ruby.rb', line 383 def handle_modifier_directive(code_object, line_no) # :nodoc: if (comment_text = @modifier_comments[line_no]) _text, directives = @preprocess.parse_comment(comment_text, line_no, :ruby) handle_code_object_directives(code_object, directives) end end |
#handle_standalone_consecutive_comment_directive(comment, directives, start_with_sharp_sharp, line_no, start_line) ⇒ Object
:nodoc:
478 479 480 481 482 483 484 485 486 487 488 |
# File 'lib/rdoc/parser/ruby.rb', line 478 def handle_standalone_consecutive_comment_directive(comment, directives, start_with_sharp_sharp, line_no, start_line) # :nodoc: if start_with_sharp_sharp && start_line != @first_non_meta_comment_start_line node = @line_nodes[line_no] (comment, directives, node) elsif normal_comment_treat_as_ghost_method_for_now?(directives, line_no) && start_line != @first_non_meta_comment_start_line (comment, directives, nil) else apply_document_control_directive(directives) handle_code_object_directives(@container, directives) end end |
#has_modifier_nodoc?(line_no) ⇒ Boolean
:nodoc:
379 380 381 |
# File 'lib/rdoc/parser/ruby.rb', line 379 def has_modifier_nodoc?(line_no) # :nodoc: @modifier_comments[line_no]&.match?(/\A#\s*:nodoc:/) end |
#mark_container_documentable(container) ⇒ Object
Makes a container that was created inside a :stopdoc:/:enddoc: region (thus ignored) documentable again when it receives documentable contents outside the region, possibly from another file.
200 201 202 203 204 205 |
# File 'lib/rdoc/parser/ruby.rb', line 200 def mark_container_documentable(container) return if container.received_nodoc || !container.ignored? record_location(container) container.start_doc mark_container_documentable(container.parent) if container.parent.is_a?(ClassModule) end |
#normal_comment_treat_as_ghost_method_for_now?(directives, line_no) ⇒ Boolean
:nodoc:
472 473 474 475 476 |
# File 'lib/rdoc/parser/ruby.rb', line 472 def normal_comment_treat_as_ghost_method_for_now?(directives, line_no) # :nodoc: # Meta method comment should start with `##` but some comments does not follow this rule. # For now, RDoc accepts them as a meta method comment if there is no node linked to it. !@line_nodes[line_no] && INVALID_GHOST_METHOD_ACCEPT_DIRECTIVE_LIST.any? { |directive| directives.has_key?(directive) } end |
#parse_comment_text_to_directives(comment_text, start_line) ⇒ Object
Parses comment text and returns [RDoc::Comment, directives, type_signature_lines],
or nil if the comment is a section header (which has no associated code
object).
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/rdoc/parser/ruby.rb', line 527 def parse_comment_text_to_directives(comment_text, start_line) # :nodoc: type_signature_lines = extract_type_signature!(comment_text, start_line) comment_text, directives = @preprocess.parse_comment(comment_text, start_line, :ruby) comment = Comment.new(comment_text, @top_level, :ruby) comment.normalized = true comment.line = start_line markup, = directives['markup'] comment.format = markup&.downcase || @markup if (section, directive_line = directives['section']) # If comment has :section:, it is not a documentable comment for a code object comment.text = extract_section_comment(comment_text, directive_line - start_line) @container.set_current_section(section, comment) return end @preprocess.run_post_processes(comment, @container) [comment, directives, type_signature_lines] end |
#parse_comment_tomdoc(container, comment, line_no, start_line) ⇒ Object
Creates an RDoc::Method on container from comment if there is a
Signature section in the comment
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/rdoc/parser/ruby.rb', line 358 def parse_comment_tomdoc(container, comment, line_no, start_line) return if document_suppressed? return unless signature = TomDoc.signature(comment) name, = signature.split %r%[ \(]%, 2 meth = AnyMethod.new name record_location(meth) meth.line = start_line meth.call_seq = signature return unless meth.name node = @line_nodes[line_no] token_stream_loader = @colorizer_context.token_stream_loader(node.node_id) if node meth.start_collecting_tokens(:ruby, loader: token_stream_loader) container.add_method meth meth.comment = comment @stats.add_method meth end |
#prepare_comments(comments) ⇒ Object
Prepares comments for processing. Comments are grouped into consecutive. Consecutive comment is linked to the next non-blank line.
Example:
01| class A # modifier comment 1
02| def foo; end # modifier comment 2
03|
04| # consecutive comment 1 start_line: 4
05| # consecutive comment 1 linked to line: 7
06|
07| # consecutive comment 2 start_line: 7
08| # consecutive comment 2 linked to line: 10
09|
10| def bar; end # consecutive comment 2 linked to this line
11| end
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/rdoc/parser/ruby.rb', line 308 def prepare_comments(comments) current = [] consecutive_comments = [current] @modifier_comments = {} comments.each do |comment| if comment.is_a? Prism::EmbDocComment consecutive_comments << [comment] << (current = []) elsif comment.location.start_line_slice.match?(/\S/) text = comment.slice text = Encoding.change_encoding(text, @encoding) if @encoding @modifier_comments[comment.location.start_line] = text elsif current.empty? || current.last.location.end_line + 1 == comment.location.start_line current << comment else consecutive_comments << (current = [comment]) end end consecutive_comments.reject!(&:empty?) # Example: line_no = 5, start_line = 2, comment_text = "# comment_start_line\n# comment\n" # 1| class A # 2| # comment_start_line # 3| # comment # 4| # 5| def f; end # comment linked to this line # 6| end @unprocessed_comments = consecutive_comments.map! do |comments| start_line = comments.first.location.start_line line_no = comments.last.location.end_line + (comments.last.location.end_column == 0 ? 0 : 1) texts = comments.map do |c| c.is_a?(Prism::EmbDocComment) ? c.slice.lines[1...-1].join : c.slice end text = texts.join("\n") text = Encoding.change_encoding(text, @encoding) if @encoding line_no += 1 while @lines[line_no - 1]&.match?(/\A\s*$/) [line_no, start_line, text] end # The first comment is special. It defines markup for the rest of the comments. _, first_comment_start_line, first_comment_text = @unprocessed_comments.first if first_comment_text && @lines[0...first_comment_start_line - 1].all? { |l| l.match?(/\A\s*$/) } _text, directives = @preprocess.parse_comment(first_comment_text, first_comment_start_line, :ruby) markup, = directives['markup'] @markup = markup.downcase if markup end end |
#prepare_line_nodes(node) ⇒ Object
Assign AST node to a line. This is used to show meta-method source code in the documentation.
282 283 284 285 286 287 288 289 290 |
# File 'lib/rdoc/parser/ruby.rb', line 282 def prepare_line_nodes(node) # :nodoc: case node when Prism::CallNode, Prism::DefNode @line_nodes[node.location.start_line] ||= node end node.compact_child_nodes.each do |child| prepare_line_nodes(child) end end |
#process_comments_until(line_no_until) ⇒ Object
Processes consecutive comments that were not linked to any documentable code until the given line number
492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/rdoc/parser/ruby.rb', line 492 def process_comments_until(line_no_until) while !@unprocessed_comments.empty? && @unprocessed_comments.first[0] <= line_no_until line_no, start_line, text = @unprocessed_comments.shift if @markup == 'tomdoc' comment = Comment.new(text, @top_level, :ruby) comment.format = 'tomdoc' parse_comment_tomdoc(@container, comment, line_no, start_line) @preprocess.run_post_processes(comment, @container) elsif (comment_text, directives = parse_comment_text_to_directives(text, start_line)) handle_standalone_consecutive_comment_directive(comment_text, directives, text.start_with?(/#\#$/), line_no, start_line) end end end |
#record_location(container) ⇒ Object
Records the location of this container in the file for this parser and
adds it to the list of classes and modules in the file.
244 245 246 247 248 249 250 251 |
# File 'lib/rdoc/parser/ruby.rb', line 244 def record_location(container) # :nodoc: case container when ClassModule @top_level.add_to_classes_or_modules container end container.record_location @top_level end |
#resolve_constant_path(constant_path) ⇒ Object
Resolves constant path to a full path by searching module nesting
805 806 807 808 809 810 811 812 813 814 815 816 |
# File 'lib/rdoc/parser/ruby.rb', line 805 def resolve_constant_path(constant_path) owner_name, path = constant_path.split('::', 2) return constant_path if owner_name.empty? # ::Foo, ::Foo::Bar mod = nil @module_nesting.reverse_each do |nesting, singleton| next if singleton mod = nesting.get_module_named(owner_name) break if mod end mod ||= @top_level.get_module_named(owner_name) [mod.full_name, path].compact.join('::') if mod end |
#scan ⇒ Object
Scans this Ruby file for Ruby constructs
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/rdoc/parser/ruby.rb', line 255 def scan @lines = @content.lines result = Prism.parse(@content) @program_node = result.value @line_nodes = {} prepare_line_nodes(@program_node) prepare_comments(result.comments) return if @top_level.done_documenting @first_non_meta_comment_start_line = nil if (_line_no, start_line = @unprocessed_comments.first) @first_non_meta_comment_start_line = start_line if start_line < @program_node.location.start_line end @program_node.accept(RDocVisitor.new(self, @top_level, @store)) process_comments_until(@lines.size + 1) end |
#should_document?(code_object) ⇒ Boolean
:nodoc:
273 274 275 276 277 |
# File 'lib/rdoc/parser/ruby.rb', line 273 def should_document?(code_object) # :nodoc: return true unless @track_visibility return false if code_object.parent&.document_children == false code_object.document_self end |
#skip_comments_until(line_no_until) ⇒ Object
Skips all undocumentable consecutive comments until the given line number.
Undocumentable comments are comments written inside def or inside undocumentable class/module
509 510 511 512 513 |
# File 'lib/rdoc/parser/ruby.rb', line 509 def skip_comments_until(line_no_until) while !@unprocessed_comments.empty? && @unprocessed_comments.first[0] <= line_no_until @unprocessed_comments.shift end end |
#with_container(container, singleton: false) ⇒ Object
Dive into another container
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/rdoc/parser/ruby.rb', line 220 def with_container(container, singleton: false) old_container = @container old_visibility = @visibility old_singleton = @singleton old_in_proc_block = @in_proc_block old_doc_state = @doc_state @visibility = :public @container = container @singleton = singleton @in_proc_block = false @module_nesting.push([container, singleton]) yield container ensure @container = old_container @visibility = old_visibility @singleton = old_singleton @in_proc_block = old_in_proc_block @doc_state = old_doc_state @module_nesting.pop end |
#with_in_proc_block ⇒ Object
Suppress extend and include within block
because they might be a metaprogramming block
example: Module.new { include M } M.module_eval { include N }
211 212 213 214 215 216 |
# File 'lib/rdoc/parser/ruby.rb', line 211 def with_in_proc_block in_proc_block = @in_proc_block @in_proc_block = true yield @in_proc_block = in_proc_block end |