Skip to content

Commit a4f60c1

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 67d7874 commit a4f60c1

File tree

5 files changed

+78
-14
lines changed

5 files changed

+78
-14
lines changed

lib/syntax_search/around_block_scan.rb

Lines changed: 44 additions & 0 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|

lib/syntax_search/capture_code_context.rb

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

4644
@lines_to_output.concat(around_lines)
4745

@@ -53,7 +51,12 @@ def call
5351
end
5452
end
5553

56-
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
5760
end
5861
end
5962
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: 12 additions & 6 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
@@ -43,13 +49,13 @@ class Zerg
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)