class RBeautify::BlockMatcher

Attributes

ends[R]
language[R]
name[R]
options[R]
starts[R]

Public Class Methods

debug() click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 58
def debug
  @debug
end
debug=(value) click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 54
def debug=(value)
  @debug = value
end
new(language, name, starts, ends, options = {}) click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 7
def initialize(language, name, starts, ends, options = {})
  @language = language
  @name = name
  @starts = starts
  @ends = ends.nil? ? starts : ends
  @options = options
end
parse(language, original_block, line_number, string, current_offset) click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 16
def parse(language, original_block, line_number, string, current_offset)
  block_end = original_block && original_block.parse_block_end(string, current_offset)

  if (block_start = first_block_start(language,
                                      original_block,
                                      line_number,
                                      string,
                                      current_offset,
                                      block_end.nil? ? nil : block_end.offset))

    block_end = nil
  end

  if block_end
    # Check whether the section of the line which is a block end is also a block start
    if block_end.end_can_also_be_start? &&
        (block_start_candidate = first_block_start(language, block_end.block_start.parent, line_number, string, current_offset)) &&
        block_start_candidate.offset == block_end.offset
      block_start = block_start_candidate
    end

  end

  if block_start
    if debug
      puts "MATCH: '#{string.slice(0, string.length - block_start.match.length - block_start.after_match.length)}<START type=#{block_start.name}>#{block_start.match}</START>#{block_start.after_match}'"
    end
    parse(language, block_start, line_number, block_start.after_match, block_start.end_offset)
  elsif block_end
    if debug
      puts "MATCH: '#{string.slice(0, string.length - block_end.match.length - block_end.after_match.length)}<END>#{block_end.match}</END>#{block_end.after_match}'"
    end
    parse(language, block_end.block_start.parent, line_number, block_end.after_match, block_end.end_offset)
  else
    original_block
  end
end

Private Class Methods

first_block_start(language, parent_block, line_number, string, offset, maximum_offset = nil) click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 63
def first_block_start(language, parent_block, line_number, string, offset, maximum_offset = nil)
  first_block_start = nil
  language.matchers.each do |matcher|
    if matcher.can_nest?(parent_block)
      if (block_start_candidate = matcher.parse_block_start(string, parent_block, offset, line_number)) &&
          (maximum_offset.nil? || maximum_offset > block_start_candidate.offset)
        first_block_start = block_start_candidate
        maximum_offset = first_block_start.offset
      end
    end
  end
  first_block_start
end

Public Instance Methods

can_nest?(parent_block) click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 102
def can_nest?(parent_block)
  return false unless parent_block.nil? || parent_block.parse_content?

  if options[:nest_only]
    parent_block && options[:nest_only].include?(parent_block.name)
  else
    parent_block.nil? ||
      options[:nest_except].nil? || !options[:nest_except].include?(parent_block.name)
  end
end
end_can_also_be_start?() click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 121
def end_can_also_be_start?
  if ends == starts
    options[:end_can_also_be_start] == true
  else
    options[:end_can_also_be_start] != false
  end
end
end_is_implicit?() click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 117
def end_is_implicit?
  options[:end] == :implicit
end
ends?() click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 113
def ends?
  ends != false
end
escape_character?() click to toggle source

True if blocks can contain the escape character \ which needs to be checked for on end match

# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 135
def escape_character?
  options[:escape_character] == true
end
format_content?() click to toggle source

Indent the content of this block

# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 98
def format_content?
  options[:format_content] != false
end
indent_end_line?(block) click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 84
def indent_end_line?(block)
  evaluate_option_for_block(:indent_end_line, block)
end
indent_size(block) click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 88
def indent_size(block)
  evaluate_option_for_block(:indent_size, block) || language.indent_size
end
inspect() click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 139
def inspect
  name
end
negate_ends_match?() click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 129
def negate_ends_match?
  options[:negate_ends_match]
end
parse_block_start(string, parent_block, offset, line_number) click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 78
def parse_block_start(string, parent_block, offset, line_number)
  if !string.empty? && (match = starts.match(string))
    RBeautify::BlockStart.new(self, parent_block, line_number, offset + match.begin(0), match[0], match.post_match)
  end
end
parse_content?() click to toggle source

Look for blocks within the content of this one

# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 93
def parse_content?
  options[:parse_content] != false
end

Private Instance Methods

evaluate_option_for_block(key, block) click to toggle source
# File lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb, line 144
def evaluate_option_for_block(key, block)
  if options[key] && options[key].respond_to?(:call)
    options[key].call(block)
  else
    options[key]
  end
end