Sha256: 398b81f5124311935302d4cedea444ec4c74828a5ba68c9faed235248df783ae

Contents?: true

Size: 1.34 KB

Versions: 38

Compression:

Stored size: 1.34 KB

Contents

module RBeautify
  class Line

    attr_reader :language, :content, :line_number, :original_block, :block, :indent_character

    def initialize(language, content, line_number, original_block = nil, use_tabs = false)
      @language = language
      @content = content
      @original_block = original_block
      @indent_character = use_tabs ? "\t" : "  "
      @block = BlockMatcher.parse(language, original_block, line_number, stripped, 0)
    end

    def format
      if @formatted.nil?
        if format?
          if stripped.length == 0
            @formatted = ""
          else
            @formatted = tab_string + stripped
          end
        else
          @formatted = content
        end
      end

      @formatted
    end

    private
    def format?
      original_block.nil? || original_block.format_content?
    end

    def indent_size
      if (block.nil? || block.strict_ancestor_of?(original_block)) && (original_block && original_block.indent_end_line?)
        original_block.total_indent_size
      else
        common_ancestor = BlockStart.first_common_ancestor(original_block, block)
        common_ancestor.nil? ? 0 : common_ancestor.total_indent_size
      end
    end

    def tab_string
      indent_character * (indent_size / 2 ) + (indent_size.odd? ? ' ' : '')
    end

    def stripped
      @stripped = content.strip
    end

  end
end

Version data entries

38 entries across 38 versions & 2 rubygems

Version Path
knife-chop-0.12.18 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.12.17 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.12.03 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.11.12 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.9.5 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.9.4 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.9.2 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.9.1 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.9.0 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.8.3 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.8.2 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.8.0 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.7.8 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.7.2 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.7.1 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.7.0 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.6.7 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.5.15 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.4.1 lib/ruby-beautify/lib/ruby-beautify/line.rb
knife-chop-0.4.0 lib/ruby-beautify/lib/ruby-beautify/line.rb