Skip to content

Commit 9036905

Browse files
committed
Fix code context capturing
Previously when we were trying to capture the "neighbors" context, we were using the same logic as when trying to "expand" a block. This logic looks for unmatched keyword pairs, so when it looks up it will stop at `def foo` but not at `def foo; end`. For grabbing context we want to stop after the first contextual pair so we cannot use that same method. Instead a new method that is specialized to finding context is introduced to the AroundBlockScan class. When trying to capture code context, we don't want to look for a dangling keyword, we want the first matched pair
1 parent d4ef283 commit 9036905

File tree

6 files changed

+90
-17
lines changed

6 files changed

+90
-17
lines changed

CHANGELOG.md

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

3+
- Simplify large file output so minimal context around the invalid section is shown (https://github.com/zombocom/syntax_search/pull/26)
34
- Block expansion is now lexically aware of keywords (def/do/end etc.) (https://github.com/zombocom/syntax_search/pull/24)
45
- Fix bug where not all of a source is lexed which is used in heredoc detection/removal (https://github.com/zombocom/syntax_search/pull/23)
56

lib/syntax_search/around_block_scan.rb

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,50 @@ def scan_while(&block)
8585
self
8686
end
8787

88+
def capture_neighbor_context
89+
lines = []
90+
kw_count = 0
91+
end_count = 0
92+
before_lines.reverse.each do |line|
93+
next if line.empty?
94+
break if line.indent < @orig_indent
95+
next if line.indent != @orig_indent
96+
97+
kw_count += 1 if line.is_kw?
98+
end_count += 1 if line.is_end?
99+
if kw_count != 0 && kw_count == end_count
100+
lines << line
101+
break
102+
end
103+
104+
lines << line
105+
end
106+
107+
lines.reverse!
108+
109+
kw_count = 0
110+
end_count = 0
111+
after_lines.each do |line|
112+
# puts "line: #{line.number} #{line.original_line}, indent: #{line.indent}, #{line.empty?} #{line.indent == @orig_indent}"
113+
114+
next if line.empty?
115+
break if line.indent < @orig_indent
116+
next if line.indent != @orig_indent
117+
118+
kw_count += 1 if line.is_kw?
119+
end_count += 1 if line.is_end?
120+
if kw_count != 0 && kw_count == end_count
121+
lines << line
122+
break
123+
end
124+
125+
lines << line
126+
end
127+
lines.select! {|line| !line.is_comment? }
128+
129+
lines
130+
end
131+
88132
def on_falling_indent
89133
last_indent = @orig_indent
90134
before_lines.reverse.each do |line|
@@ -119,16 +163,23 @@ def scan_adjacent_indent
119163
self
120164
end
121165

166+
def start_at_next_line
167+
before_index; after_index
168+
@before_index -= 1
169+
@after_index += 1
170+
self
171+
end
172+
122173
def code_block
123174
CodeBlock.new(lines: @code_lines[before_index..after_index])
124175
end
125176

126177
def before_index
127-
@before_index || @orig_before_index
178+
@before_index ||= @orig_before_index
128179
end
129180

130181
def after_index
131-
@after_index || @orig_after_index
182+
@after_index ||= @orig_after_index
132183
end
133184

134185
private def before_lines

lib/syntax_search/capture_code_context.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,10 @@ def initialize(blocks: , code_lines:)
3636
def call
3737
@blocks.each do |block|
3838
around_lines = AroundBlockScan.new(code_lines: @code_lines, block: block)
39-
.stop_after_kw
40-
.scan_while {|line| line.empty? || line.indent >= block.current_indent }
41-
.code_block
42-
.lines
43-
.select {|l| l.indent == block.current_indent && ! block.lines.include?(l) }
39+
.start_at_next_line
40+
.capture_neighbor_context
41+
42+
around_lines -= block.lines
4443

4544
@lines_to_output.concat(around_lines)
4645

@@ -52,7 +51,12 @@ def call
5251
end
5352
end
5453

55-
return @lines_to_output.uniq.select(&:not_empty?).sort
54+
@lines_to_output.select!(&:not_empty?)
55+
@lines_to_output.select!(&:not_comment?)
56+
@lines_to_output.uniq!
57+
@lines_to_output.sort!
58+
59+
return @lines_to_output
5660
end
5761
end
5862
end

lib/syntax_search/code_line.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def is_comment?
6868
@is_comment
6969
end
7070

71+
def not_comment?
72+
!is_comment?
73+
end
74+
7175
def is_kw?
7276
@is_kw
7377
end
@@ -107,6 +111,8 @@ def line_number
107111
index + 1
108112
end
109113

114+
alias :number :line_number
115+
110116
def not_empty?
111117
!empty?
112118
end

spec/unit/display_invalid_blocks_spec.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,21 @@ class Blerg
1010
end
1111
class OH
1212
13+
def nope
14+
end
15+
1316
def lol
1417
end
1518
1619
it "foo"
20+
puts "here"
1721
end
1822
1923
def haha
2024
end
2125
26+
def nope
27+
end
2228
end
2329
2430
class Zerg
@@ -35,21 +41,21 @@ class Zerg
3541

3642
# Finds lines previously hidden
3743
lines = code_context.call
38-
expect(lines.select(&:hidden?).map(&:line_number)).to eq([11, 12])
44+
# expect(lines.select(&:hidden?).map(&:line_number)).to eq([11, 12])
3945

4046
out = DisplayCodeWithLineNumbers.new(
4147
lines: lines,
4248
).call
4349

4450
expect(out).to eq(<<~EOM.indent(2))
4551
3 class OH
46-
5 def lol
47-
6 end
48-
8 it "foo"
52+
8 def lol
4953
9 end
50-
11 def haha
51-
12 end
52-
14 end
54+
11 it "foo"
55+
13 end
56+
15 def haha
57+
16 end
58+
20 end
5359
EOM
5460
end
5561

spec/unit/exe_spec.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,14 @@ def exe(cmd)
3838
out = exe("#{file.path} --no-terminal")
3939

4040
expect(out).to include(<<~EOM.indent(4))
41-
77 class Lookups
41+
16 class Rexe
42+
40 class Options < Struct.new(
43+
71 end
44+
❯ 77 class Lookups
4245
❯ 78 def input_modes
43-
148 end
46+
❯ 148 end
47+
152 class CommandLineParser
48+
418 end
4449
551 end
4550
EOM
4651
end

0 commit comments

Comments
 (0)