class DeadEnd::AroundBlockScan
This class is useful for exploring contents before and after a block
It searches above and below the passed in block to match for whatever criteria you give it:
Example:
def dog puts "bark" puts "bark" end scan = AroundBlockScan.new( code_lines: code_lines block: CodeBlock.new(lines: code_lines[1]) ) scan.scan_while { true } puts scan.before_index # => 0 puts scan.after_index # => 3
Contents can also be filtered using AroundBlockScan#skip
To grab the next surrounding indentation use AroundBlockScan#scan_adjacent_indent
Public Class Methods
new(code_lines: , block:)
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 31 def initialize(code_lines: , block:) @code_lines = code_lines @orig_before_index = block.lines.first.index @orig_after_index = block.lines.last.index @orig_indent = block.current_indent @skip_array = [] @after_array = [] @before_array = [] @stop_after_kw = false end
Public Instance Methods
after_index()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 190 def after_index @after_index ||= @orig_after_index end
before_index()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 186 def before_index @before_index ||= @orig_before_index end
capture_neighbor_context()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 88 def capture_neighbor_context lines = [] kw_count = 0 end_count = 0 before_lines.reverse.each do |line| next if line.empty? break if line.indent < @orig_indent next if line.indent != @orig_indent kw_count += 1 if line.is_kw? end_count += 1 if line.is_end? if kw_count != 0 && kw_count == end_count lines << line break end lines << line end lines.reverse! kw_count = 0 end_count = 0 after_lines.each do |line| # puts "line: #{line.number} #{line.original_line}, indent: #{line.indent}, #{line.empty?} #{line.indent == @orig_indent}" next if line.empty? break if line.indent < @orig_indent next if line.indent != @orig_indent kw_count += 1 if line.is_kw? end_count += 1 if line.is_end? if kw_count != 0 && kw_count == end_count lines << line break end lines << line end lines.select! {|line| !line.is_comment? } lines end
code_block()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 182 def code_block CodeBlock.new(lines: @code_lines[before_index..after_index]) end
next_down()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 160 def next_down @code_lines[after_index.next] end
next_up()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 156 def next_up @code_lines[before_index.pred] end
on_falling_indent() { |line| ... }
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 132 def on_falling_indent last_indent = @orig_indent before_lines.reverse.each do |line| next if line.empty? if line.indent < last_indent yield line last_indent = line.indent end end last_indent = @orig_indent after_lines.each do |line| next if line.empty? if line.indent < last_indent yield line last_indent = line.indent end end end
scan_adjacent_indent()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 164 def scan_adjacent_indent before_after_indent = [] before_after_indent << (next_up&.indent || 0) before_after_indent << (next_down&.indent || 0) indent = before_after_indent.min self.scan_while {|line| line.not_empty? && line.indent >= indent } self end
scan_neighbors()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 152 def scan_neighbors self.scan_while {|line| line.not_empty? && line.indent >= @orig_indent } end
scan_while(&block)
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 52 def scan_while(&block) stop_next = false kw_count = 0 end_count = 0 @before_index = before_lines.reverse_each.take_while do |line| next false if stop_next next true if @skip_array.detect {|meth| line.send(meth) } kw_count += 1 if line.is_kw? end_count += 1 if line.is_end? if @stop_after_kw && kw_count > end_count stop_next = true end block.call(line) end.reverse.first&.index stop_next = false kw_count = 0 end_count = 0 @after_index = after_lines.take_while do |line| next false if stop_next next true if @skip_array.detect {|meth| line.send(meth) } kw_count += 1 if line.is_kw? end_count += 1 if line.is_end? if @stop_after_kw && end_count > kw_count stop_next = true end block.call(line) end.last&.index self end
skip(name)
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 42 def skip(name) @skip_array << name self end
start_at_next_line()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 175 def start_at_next_line before_index; after_index @before_index -= 1 @after_index += 1 self end
stop_after_kw()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 47 def stop_after_kw @stop_after_kw = true self end
Private Instance Methods
after_lines()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 198 def after_lines @code_lines[after_index.next..-1] || [] end
before_lines()
click to toggle source
# File lib/dead_end/around_block_scan.rb, line 194 def before_lines @code_lines[0...@orig_before_index] || [] end