Sha256: e998b032ad2ed473c6d1d3eb834cccc714273c0fddc906b43dcf63afea78e2e8

Contents?: true

Size: 796 Bytes

Versions: 2

Compression:

Stored size: 796 Bytes

Contents

require 'ripper'
module RipperRubyParser
  class CommentingSexpBuilder < Ripper::SexpBuilderPP
    def initialize *args
      super
      @comment = nil
      @comment_stack = []
    end

    def on_comment tok
      @comment ||= ""
      @comment += tok
      super
    end

    def on_kw tok
      case tok
      when "class", "def", "module"
        @comment_stack.push @comment
        @comment = nil
      end
      super
    end

    def on_class *args
      commentize(super)
    end

    def on_def *args
      commentize(super)
    end

    def on_module *args
      commentize(super)
    end

    private

    def commentize exp
      comment = @comment_stack.pop
      if comment.nil?
        [:comment, "", exp]
      else
        [:comment, comment, exp]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ripper_ruby_parser-0.0.3 lib/ripper_ruby_parser/commenting_sexp_builder.rb
ripper_ruby_parser-0.0.2 lib/ripper_ruby_parser/commenting_sexp_builder.rb