Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## HEAD (unreleased)

- Fixed internal class AroundBlockScan, minor changes in outputs (https://github.com/zombocom/dead_end/pull/131)

## 3.1.1

- Fix case where Ripper lexing identified incorrect code as a keyword (https://github.com/zombocom/dead_end/pull/122)
Expand Down
44 changes: 34 additions & 10 deletions lib/dead_end/around_block_scan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,20 @@ def initialize(code_lines:, block:)
@after_array = []
@before_array = []
@stop_after_kw = false

@skip_hidden = false
@skip_empty = false
end

def skip(name)
@skip_array << name
case name
when :hidden?
@skip_hidden = true
when :empty?
@skip_empty = true
else
raise "Unsupported skip #{name}"
end
self
end

Expand All @@ -49,39 +59,49 @@ def stop_after_kw
self
end

def scan_while(&block)
def scan_while
stop_next = false

kw_count = 0
end_count = 0
@before_index = before_lines.reverse_each.take_while do |line|
index = before_lines.reverse_each.take_while do |line|
next false if stop_next
next true if @skip_array.detect { |meth| line.send(meth) }
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

block.call(line)
yield line
end.last&.index

if index && index < before_index
@before_index = index
end

stop_next = false
kw_count = 0
end_count = 0
@after_index = after_lines.take_while do |line|
index = after_lines.take_while do |line|
next false if stop_next
next true if @skip_array.detect { |meth| line.send(meth) }
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

block.call(line)
yield line
end.last&.index

if index && index > after_index
@after_index = index
end
self
end

Expand Down Expand Up @@ -178,7 +198,11 @@ def start_at_next_line
end

def code_block
CodeBlock.new(lines: @code_lines[before_index..after_index])
CodeBlock.new(lines: lines)
end

def lines
@code_lines[before_index..after_index]
end

def before_index
Expand All @@ -190,7 +214,7 @@ def after_index
end

private def before_lines
@code_lines[0...@orig_before_index] || []
@code_lines[0...before_index] || []
end

private def after_lines
Expand Down
25 changes: 12 additions & 13 deletions lib/dead_end/block_expand.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def initialize(code_lines:)
end

def call(block)
if (next_block = expand_neighbors(block, grab_empty: true))
if (next_block = expand_neighbors(block))
return next_block
end

Expand All @@ -51,25 +51,24 @@ def expand_indent(block)
.code_block
end

def expand_neighbors(block, grab_empty: true)
scan = AroundBlockScan.new(code_lines: @code_lines, block: block)
def expand_neighbors(block)
expanded_lines = AroundBlockScan.new(code_lines: @code_lines, block: block)
.skip(:hidden?)
.stop_after_kw
.scan_neighbors
.scan_while { |line| line.empty? } # Slurp up empties
.lines

# Slurp up empties
if grab_empty
scan = AroundBlockScan.new(code_lines: @code_lines, block: scan.code_block)
.scan_while { |line| line.empty? || line.hidden? }
end

new_block = scan.code_block

if block.lines == new_block.lines
if block.lines == expanded_lines
nil
else
new_block
CodeBlock.new(lines: expanded_lines)
end
end

# Managable rspec errors
def inspect
"#<DeadEnd::CodeBlock:0x0000123843lol >"
end
end
end
2 changes: 0 additions & 2 deletions spec/integration/dead_end_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module DeadEnd
6 class SyntaxTree < Ripper
170 def self.parse(source)
174 end
176 private
❯ 754 def on_args_add(arguments, argument)
❯ 776 class ArgsAddBlock
❯ 810 end
Expand Down Expand Up @@ -119,7 +118,6 @@ module DeadEnd
7 REQUIRED_BY = {}
9 attr_reader :name
10 attr_writer :cost
11 attr_accessor :parent
❯ 13 def initialize(name)
❯ 18 def self.reset!
❯ 25 end
Expand Down
17 changes: 17 additions & 0 deletions spec/unit/around_block_scan_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@

module DeadEnd
RSpec.describe AroundBlockScan do
it "continues scan from last location even if scan is false" do
source = <<~'EOM'
print 'omg'
print 'lol'
print 'haha'
EOM
code_lines = CodeLine.from_source(source)
block = CodeBlock.new(lines: code_lines[1])
expand = AroundBlockScan.new(code_lines: code_lines, block: block)
.scan_neighbors

expect(expand.code_block.to_s).to eq(source)
expand.scan_while { |line| false }

expect(expand.code_block.to_s).to eq(source)
end

it "scan_adjacent_indent works on first or last line" do
source_string = <<~EOM
def foo
Expand Down
30 changes: 29 additions & 1 deletion spec/unit/capture_code_context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,35 @@

module DeadEnd
RSpec.describe CaptureCodeContext do
it "capture_before_after_kws" do
source = <<~'EOM'
def sit
end

def bark

def eat
end
EOM

code_lines = CodeLine.from_source(source)

block = CodeBlock.new(lines: code_lines[0])

display = CaptureCodeContext.new(
blocks: [block],
code_lines: code_lines
)
lines = display.call
expect(lines.join).to eq(<<~EOM)
def sit
end
def bark
def eat
end
EOM
end

it "handles ambiguous end" do
source = <<~'EOM'
def call # 1
Expand Down Expand Up @@ -94,7 +123,6 @@ def call
expect(lines.join).to eq(<<~EOM)
class Rexe
VERSION = '1.5.1'
PROJECT_URL = 'https://github.com/keithrbennett/rexe'
class Lookups
def format_requires
end
Expand Down