Skip to content

Commit 89c6882

Browse files
authored
Merge pull request #131 from zombocom/schneems/fix-around-block-before-index
Fix AroundBlockScan
2 parents 4255d30 + b579f5f commit 89c6882

File tree

6 files changed

+94
-26
lines changed

6 files changed

+94
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## HEAD (unreleased)
22

3+
- Fixed internal class AroundBlockScan, minor changes in outputs (https://github.com/zombocom/dead_end/pull/131)
4+
35
## 3.1.1
46

57
- Fix case where Ripper lexing identified incorrect code as a keyword (https://github.com/zombocom/dead_end/pull/122)

lib/dead_end/around_block_scan.rb

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,20 @@ def initialize(code_lines:, block:)
3737
@after_array = []
3838
@before_array = []
3939
@stop_after_kw = false
40+
41+
@skip_hidden = false
42+
@skip_empty = false
4043
end
4144

4245
def skip(name)
43-
@skip_array << name
46+
case name
47+
when :hidden?
48+
@skip_hidden = true
49+
when :empty?
50+
@skip_empty = true
51+
else
52+
raise "Unsupported skip #{name}"
53+
end
4454
self
4555
end
4656

@@ -49,39 +59,49 @@ def stop_after_kw
4959
self
5060
end
5161

52-
def scan_while(&block)
62+
def scan_while
5363
stop_next = false
5464

5565
kw_count = 0
5666
end_count = 0
57-
@before_index = before_lines.reverse_each.take_while do |line|
67+
index = before_lines.reverse_each.take_while do |line|
5868
next false if stop_next
59-
next true if @skip_array.detect { |meth| line.send(meth) }
69+
next true if @skip_hidden && line.hidden?
70+
next true if @skip_empty && line.empty?
6071

6172
kw_count += 1 if line.is_kw?
6273
end_count += 1 if line.is_end?
6374
if @stop_after_kw && kw_count > end_count
6475
stop_next = true
6576
end
6677

67-
block.call(line)
78+
yield line
6879
end.last&.index
6980

81+
if index && index < before_index
82+
@before_index = index
83+
end
84+
7085
stop_next = false
7186
kw_count = 0
7287
end_count = 0
73-
@after_index = after_lines.take_while do |line|
88+
index = after_lines.take_while do |line|
7489
next false if stop_next
75-
next true if @skip_array.detect { |meth| line.send(meth) }
90+
next true if @skip_hidden && line.hidden?
91+
next true if @skip_empty && line.empty?
7692

7793
kw_count += 1 if line.is_kw?
7894
end_count += 1 if line.is_end?
7995
if @stop_after_kw && end_count > kw_count
8096
stop_next = true
8197
end
8298

83-
block.call(line)
99+
yield line
84100
end.last&.index
101+
102+
if index && index > after_index
103+
@after_index = index
104+
end
85105
self
86106
end
87107

@@ -178,7 +198,11 @@ def start_at_next_line
178198
end
179199

180200
def code_block
181-
CodeBlock.new(lines: @code_lines[before_index..after_index])
201+
CodeBlock.new(lines: lines)
202+
end
203+
204+
def lines
205+
@code_lines[before_index..after_index]
182206
end
183207

184208
def before_index
@@ -190,7 +214,7 @@ def after_index
190214
end
191215

192216
private def before_lines
193-
@code_lines[0...@orig_before_index] || []
217+
@code_lines[0...before_index] || []
194218
end
195219

196220
private def after_lines

lib/dead_end/block_expand.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def initialize(code_lines:)
3636
end
3737

3838
def call(block)
39-
if (next_block = expand_neighbors(block, grab_empty: true))
39+
if (next_block = expand_neighbors(block))
4040
return next_block
4141
end
4242

@@ -51,25 +51,24 @@ def expand_indent(block)
5151
.code_block
5252
end
5353

54-
def expand_neighbors(block, grab_empty: true)
55-
scan = AroundBlockScan.new(code_lines: @code_lines, block: block)
54+
def expand_neighbors(block)
55+
expanded_lines = AroundBlockScan.new(code_lines: @code_lines, block: block)
5656
.skip(:hidden?)
5757
.stop_after_kw
5858
.scan_neighbors
59+
.scan_while { |line| line.empty? } # Slurp up empties
60+
.lines
5961

60-
# Slurp up empties
61-
if grab_empty
62-
scan = AroundBlockScan.new(code_lines: @code_lines, block: scan.code_block)
63-
.scan_while { |line| line.empty? || line.hidden? }
64-
end
65-
66-
new_block = scan.code_block
67-
68-
if block.lines == new_block.lines
62+
if block.lines == expanded_lines
6963
nil
7064
else
71-
new_block
65+
CodeBlock.new(lines: expanded_lines)
7266
end
7367
end
68+
69+
# Managable rspec errors
70+
def inspect
71+
"#<DeadEnd::CodeBlock:0x0000123843lol >"
72+
end
7473
end
7574
end

spec/integration/dead_end_spec.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ module DeadEnd
2929
6 class SyntaxTree < Ripper
3030
170 def self.parse(source)
3131
174 end
32-
176 private
3332
❯ 754 def on_args_add(arguments, argument)
3433
❯ 776 class ArgsAddBlock
3534
❯ 810 end
@@ -119,7 +118,6 @@ module DeadEnd
119118
7 REQUIRED_BY = {}
120119
9 attr_reader :name
121120
10 attr_writer :cost
122-
11 attr_accessor :parent
123121
❯ 13 def initialize(name)
124122
❯ 18 def self.reset!
125123
❯ 25 end

spec/unit/around_block_scan_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,23 @@
44

55
module DeadEnd
66
RSpec.describe AroundBlockScan do
7+
it "continues scan from last location even if scan is false" do
8+
source = <<~'EOM'
9+
print 'omg'
10+
print 'lol'
11+
print 'haha'
12+
EOM
13+
code_lines = CodeLine.from_source(source)
14+
block = CodeBlock.new(lines: code_lines[1])
15+
expand = AroundBlockScan.new(code_lines: code_lines, block: block)
16+
.scan_neighbors
17+
18+
expect(expand.code_block.to_s).to eq(source)
19+
expand.scan_while { |line| false }
20+
21+
expect(expand.code_block.to_s).to eq(source)
22+
end
23+
724
it "scan_adjacent_indent works on first or last line" do
825
source_string = <<~EOM
926
def foo

spec/unit/capture_code_context_spec.rb

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@
44

55
module DeadEnd
66
RSpec.describe CaptureCodeContext do
7+
it "capture_before_after_kws" do
8+
source = <<~'EOM'
9+
def sit
10+
end
11+
12+
def bark
13+
14+
def eat
15+
end
16+
EOM
17+
18+
code_lines = CodeLine.from_source(source)
19+
20+
block = CodeBlock.new(lines: code_lines[0])
21+
22+
display = CaptureCodeContext.new(
23+
blocks: [block],
24+
code_lines: code_lines
25+
)
26+
lines = display.call
27+
expect(lines.join).to eq(<<~EOM)
28+
def sit
29+
end
30+
def bark
31+
def eat
32+
end
33+
EOM
34+
end
35+
736
it "handles ambiguous end" do
837
source = <<~'EOM'
938
def call # 1
@@ -94,7 +123,6 @@ def call
94123
expect(lines.join).to eq(<<~EOM)
95124
class Rexe
96125
VERSION = '1.5.1'
97-
PROJECT_URL = 'https://github.com/keithrbennett/rexe'
98126
class Lookups
99127
def format_requires
100128
end

0 commit comments

Comments
 (0)