diff --git a/CHANGELOG.md b/CHANGELOG.md index a77e2b4..f4ca4c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/lib/dead_end/around_block_scan.rb b/lib/dead_end/around_block_scan.rb index ddb8198..8c12c68 100644 --- a/lib/dead_end/around_block_scan.rb +++ b/lib/dead_end/around_block_scan.rb @@ -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 @@ -49,14 +59,15 @@ 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? @@ -64,15 +75,20 @@ def scan_while(&block) 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? @@ -80,8 +96,12 @@ def scan_while(&block) stop_next = true end - block.call(line) + yield line end.last&.index + + if index && index > after_index + @after_index = index + end self end @@ -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 @@ -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 diff --git a/lib/dead_end/block_expand.rb b/lib/dead_end/block_expand.rb index de47e6a..7f3396f 100644 --- a/lib/dead_end/block_expand.rb +++ b/lib/dead_end/block_expand.rb @@ -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 @@ -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 + "#" + end end end diff --git a/spec/integration/dead_end_spec.rb b/spec/integration/dead_end_spec.rb index 8ad9913..bd2746a 100644 --- a/spec/integration/dead_end_spec.rb +++ b/spec/integration/dead_end_spec.rb @@ -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 @@ -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 diff --git a/spec/unit/around_block_scan_spec.rb b/spec/unit/around_block_scan_spec.rb index 781a4f1..8016b7f 100644 --- a/spec/unit/around_block_scan_spec.rb +++ b/spec/unit/around_block_scan_spec.rb @@ -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 diff --git a/spec/unit/capture_code_context_spec.rb b/spec/unit/capture_code_context_spec.rb index f001e0e..0eb7300 100644 --- a/spec/unit/capture_code_context_spec.rb +++ b/spec/unit/capture_code_context_spec.rb @@ -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 @@ -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