Class: RBeautify::BlockMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language, name, starts, ends, options = {}) ⇒ BlockMatcher

Returns a new instance of BlockMatcher.



7
8
9
10
11
12
13
# 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

Instance Attribute Details

#endsObject (readonly)

Returns the value of attribute ends.



5
6
7
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 5

def ends
  @ends
end

#languageObject (readonly)

Returns the value of attribute language.



5
6
7
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 5

def language
  @language
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 5

def options
  @options
end

#startsObject (readonly)

Returns the value of attribute starts.



5
6
7
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 5

def starts
  @starts
end

Class Method Details

.debugObject



58
59
60
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 58

def debug
  @debug
end

.debug=(value) ⇒ Object



54
55
56
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 54

def debug=(value)
  @debug = value
end

.parse(language, original_block, line_number, string, current_offset) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Method Details

#can_nest?(parent_block) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
105
106
107
108
109
110
111
# 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?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
# 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?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 117

def end_is_implicit?
  options[:end] == :implicit
end

#ends?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 113

def ends?
  ends != false
end

#escape_character?Boolean

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

Returns:

  • (Boolean)


135
136
137
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 135

def escape_character?
  options[:escape_character] == true
end

#format_content?Boolean

Indent the content of this block

Returns:

  • (Boolean)


98
99
100
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 98

def format_content?
  options[:format_content] != false
end

#indent_end_line?(block) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# 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) ⇒ Object



88
89
90
# 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

#inspectObject



139
140
141
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 139

def inspect
  name
end

#negate_ends_match?Boolean

Returns:

  • (Boolean)


129
130
131
# 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) ⇒ Object



78
79
80
81
82
# 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?Boolean

Look for blocks within the content of this one

Returns:

  • (Boolean)


93
94
95
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 93

def parse_content?
  options[:parse_content] != false
end