Sha256: aed6c3adfb1c685471ebf58fbcdef9ecdaa7a92ddcb656cad44a1b975c79ca40

Contents?: true

Size: 1.52 KB

Versions: 27

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

module RBS
  module Annotate
    class Formatter
      attr_reader :buffer

      def initialize()
        @buffer = +""
        @pending_separator = nil
      end

      def <<(s)
        if s
          if s.is_a?(RDoc::Markup::Document)
            s = self.class.translate(s) or raise
          end

          s = s.rstrip

          unless s.empty?
            if ss = @pending_separator
              buffer << ss
              buffer << "\n"
              @pending_separator = nil
            end

            buffer << s
            buffer << "\n"
          end
        end

        self
      end

      def margin(separator: "")
        unless buffer.empty?
          @pending_separator = separator
        end

        self
      end

      def empty?
        buffer.empty?
      end

      def format(newline_at_end:)
        unless buffer.empty?
          if newline_at_end
            buffer.strip + "\n\n"
          else
            buffer.strip + "\n"
          end
        else
          buffer
        end
      end

      def self.each_part(doc, &block)
        if block
          if doc.file
            yield doc
          else
            doc.each do |d|
              each_part(d, &block)
            end
          end
        else
          enum_for :each_part, doc
        end
      end

      def self.translate(doc)
        if doc.file
          formatter = RDoc::Markup::ToMarkdown.new
          doc.accept(formatter).strip.lines.map(&:rstrip).join("\n")
        end
      end
    end
  end
end

Version data entries

27 entries across 27 versions & 2 rubygems

Version Path
getargv-0.3.3-universal-darwin vendor/bundle/ruby/3.3.0/gems/rbs-2.8.4/lib/rbs/annotate/formatter.rb
rbs-3.2.2 lib/rbs/annotate/formatter.rb
rbs-3.2.1 lib/rbs/annotate/formatter.rb
rbs-3.2.0 lib/rbs/annotate/formatter.rb
rbs-3.2.0.pre.1 lib/rbs/annotate/formatter.rb
rbs-3.1.3 lib/rbs/annotate/formatter.rb
rbs-3.1.2 lib/rbs/annotate/formatter.rb
rbs-3.1.1 lib/rbs/annotate/formatter.rb
rbs-3.1.0 lib/rbs/annotate/formatter.rb
rbs-3.0.4 lib/rbs/annotate/formatter.rb
rbs-3.0.3 lib/rbs/annotate/formatter.rb
rbs-3.0.2 lib/rbs/annotate/formatter.rb
rbs-3.0.1 lib/rbs/annotate/formatter.rb
rbs-3.0.0 lib/rbs/annotate/formatter.rb
rbs-3.0.0.dev.3 lib/rbs/annotate/formatter.rb
rbs-3.0.0.dev.2 lib/rbs/annotate/formatter.rb
rbs-2.8.4 lib/rbs/annotate/formatter.rb
rbs-2.8.3 lib/rbs/annotate/formatter.rb
rbs-3.0.0.dev.1 lib/rbs/annotate/formatter.rb
rbs-2.8.2 lib/rbs/annotate/formatter.rb