Class: RBeautify::BlockMatcher
- Defined in:
- lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb
Instance Attribute Summary collapse
-
#ends ⇒ Object
readonly
Returns the value of attribute ends.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#starts ⇒ Object
readonly
Returns the value of attribute starts.
Class Method Summary collapse
- .debug ⇒ Object
- .debug=(value) ⇒ Object
- .parse(language, original_block, line_number, string, current_offset) ⇒ Object
Instance Method Summary collapse
- #can_nest?(parent_block) ⇒ Boolean
- #end_can_also_be_start? ⇒ Boolean
- #end_is_implicit? ⇒ Boolean
- #ends? ⇒ Boolean
-
#escape_character? ⇒ Boolean
True if blocks can contain the escape character \ which needs to be checked for on end match.
-
#format_content? ⇒ Boolean
Indent the content of this block.
- #indent_end_line?(block) ⇒ Boolean
- #indent_size(block) ⇒ Object
-
#initialize(language, name, starts, ends, options = {}) ⇒ BlockMatcher
constructor
A new instance of BlockMatcher.
- #inspect ⇒ Object
- #negate_ends_match? ⇒ Boolean
- #parse_block_start(string, parent_block, offset, line_number) ⇒ Object
-
#parse_content? ⇒ Boolean
Look for blocks within the content of this one.
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, = {}) @language = language @name = name @starts = starts @ends = ends.nil? ? starts : ends @options = end |
Instance Attribute Details
#ends ⇒ Object (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 |
#language ⇒ Object (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 |
#name ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 5 def @options end |
#starts ⇒ Object (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
.debug ⇒ Object
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
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 [:nest_only] parent_block && [:nest_only].include?(parent_block.name) else parent_block.nil? || [:nest_except].nil? || ![:nest_except].include?(parent_block.name) end end |
#end_can_also_be_start? ⇒ 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 [:end_can_also_be_start] == true else [:end_can_also_be_start] != false end end |
#end_is_implicit? ⇒ Boolean
117 118 119 |
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 117 def end_is_implicit? [:end] == :implicit end |
#ends? ⇒ 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
135 136 137 |
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 135 def escape_character? [:escape_character] == true end |
#format_content? ⇒ Boolean
Indent the content of this block
98 99 100 |
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 98 def format_content? [:format_content] != false end |
#indent_end_line?(block) ⇒ 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 |
#inspect ⇒ Object
139 140 141 |
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 139 def inspect name end |
#negate_ends_match? ⇒ Boolean
129 130 131 |
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 129 def negate_ends_match? [: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
93 94 95 |
# File 'lib/ruby-beautify/lib/ruby-beautify/block_matcher.rb', line 93 def parse_content? [:parse_content] != false end |