класс SyntaxSuggest::AroundBlockScan
Этот класс полезен для изучения содержимого перед и после блока.
Он ищет выше и ниже переданного блока, чтобы соответствовать заданным критериям:
Пример:
def dog # 1
puts "bark" # 2
puts "bark" # 3
end # 4
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 Содержимое также может быть отфильтровано с помощью AroundBlockScan#skip.
Чтобы получить следующий окружающий отступ, используйте AroundBlockScan#scan_adjacent_indent.
Публичные методы класса
# File lib/syntax_suggest/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 @skip_hidden = false @skip_empty = false end
Публичные методы экземпляра
# File lib/syntax_suggest/around_block_scan.rb, line 212 def after_index @after_index ||= @orig_after_index end
# File lib/syntax_suggest/around_block_scan.rb, line 208 def before_index @before_index ||= @orig_before_index end
# File lib/syntax_suggest/around_block_scan.rb, line 108
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|
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
end # File lib/syntax_suggest/around_block_scan.rb, line 200 def code_block CodeBlock.new(lines: lines) end
# File lib/syntax_suggest/around_block_scan.rb, line 204 def lines @code_lines[before_index..after_index] end
# File lib/syntax_suggest/around_block_scan.rb, line 177 def next_down @code_lines[after_index.next] end
# File lib/syntax_suggest/around_block_scan.rb, line 173 def next_up @code_lines[before_index.pred] end
# File lib/syntax_suggest/around_block_scan.rb, line 149
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 # File lib/syntax_suggest/around_block_scan.rb, line 181
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
scan_while { |line| line.not_empty? && line.indent >= indent }
self
end # File lib/syntax_suggest/around_block_scan.rb, line 169
def scan_neighbors
scan_while { |line| line.not_empty? && line.indent >= @orig_indent }
end # File lib/syntax_suggest/around_block_scan.rb, line 62
def scan_while
stop_next = false
kw_count = 0
end_count = 0
index = before_lines.reverse_each.take_while do |line|
next false if stop_next
next true if @skip_hidden && line.hidden?
next true if @skip_empty && line.empty?
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
yield line
end.last&.index
if index && index < before_index
@before_index = index
end
stop_next = false
kw_count = 0
end_count = 0
index = after_lines.take_while do |line|
next false if stop_next
next true if @skip_hidden && line.hidden?
next true if @skip_empty && line.empty?
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
yield line
end.last&.index
if index && index > after_index
@after_index = index
end
self
end # File lib/syntax_suggest/around_block_scan.rb, line 45
def skip(name)
case name
when :hidden?
@skip_hidden = true
when :empty?
@skip_empty = true
else
raise "Unsupported skip #{name}"
end
self
end # File lib/syntax_suggest/around_block_scan.rb, line 192 def start_at_next_line before_index after_index @before_index -= 1 @after_index += 1 self end
# File lib/syntax_suggest/around_block_scan.rb, line 57 def stop_after_kw @stop_after_kw = true self end
Приватные методы экземпляра
# File lib/syntax_suggest/around_block_scan.rb, line 220
def after_lines
@code_lines[after_index.next..-1] || []
end # File lib/syntax_suggest/around_block_scan.rb, line 216
def before_lines
@code_lines[0...before_index] || []
end
Ruby Core © 1993–2022 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.