Skip to content

Commit 4a54767

Browse files
committed
Clean up output
I previously left a comment stating I didn't know why a certain method existed. In investigating the code in `CaptureCodeContext#capture_before_after_kws` I found that it was added as to give a slightly less noisy output. The docs for AroundBlockScan#capture_neighbor_context only describe keywords as being a primary concern. I modified that code to only include lines that are keywords or ends. This reduces the output noise even more. This allows me to remove that `start_at_next_line` method. One weird side effect of the prior logic is it would cause this code to produce this output: ``` class OH def hello def hai end end ``` ``` 1 class OH > 2 def hello 4 def hai 5 end 6 end ``` But this code to produce this output: ``` class OH def hello def hai end end ``` ``` 1 class OH > 2 def hello 4 end 5 end ``` Note the missing `def hai`. The only difference between them is that space. With this change, they're now both consistent.
1 parent 46ec5b4 commit 4a54767

File tree

6 files changed

+9
-27
lines changed

6 files changed

+9
-27
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+
- Reduce line output for increased clarity (https://github.com/ruby/syntax_suggest/pull/190)
4+
35
## 1.0.4
46

57
- Fix rendering a file without a newline ending (https://github.com/ruby/syntax_suggest/pull/182)

lib/syntax_suggest/around_block_scan.rb

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ def capture_neighbor_context
176176
break
177177
end
178178

179-
lines << line
179+
lines << line if line.is_kw? || line.is_end?
180180
end
181181

182182
lines.reverse!
@@ -195,7 +195,7 @@ def capture_neighbor_context
195195
break
196196
end
197197

198-
lines << line
198+
lines << line if line.is_kw? || line.is_end?
199199
end
200200

201201
lines
@@ -319,24 +319,6 @@ def scan_adjacent_indent
319319
self
320320
end
321321

322-
# TODO: Doc or delete
323-
#
324-
# I don't remember why this is needed, but it's called in code_context.
325-
# It's related to the implementation of `capture_neighbor_context` somehow
326-
# and that display improvement is only triggered when there's one visible line
327-
#
328-
# I think the primary purpose is to not include the current line in the
329-
# logic evaluation of `capture_neighbor_context`. If that's true, then
330-
# we should fix that method to handle this logic instead of only using
331-
# it in one place and together.
332-
def start_at_next_line
333-
before_index
334-
after_index
335-
@before_index -= 1
336-
@after_index += 1
337-
self
338-
end
339-
340322
# Return the currently matched lines as a `CodeBlock`
341323
#
342324
# When a `CodeBlock` is created it will gather metadata about

lib/syntax_suggest/capture_code_context.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ def capture_before_after_kws(block)
116116
return unless block.visible_lines.count == 1
117117

118118
around_lines = AroundBlockScan.new(code_lines: @code_lines, block: block)
119-
.start_at_next_line
120119
.capture_neighbor_context
121120

122121
around_lines -= block.lines

spec/integration/syntax_suggest_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,6 @@ module SyntaxSuggest
115115
expect(io.string).to include(<<~'EOM')
116116
5 module DerailedBenchmarks
117117
6 class RequireTree
118-
7 REQUIRED_BY = {}
119-
9 attr_reader :name
120-
10 attr_writer :cost
121118
> 13 def initialize(name)
122119
> 18 def self.reset!
123120
> 25 end
@@ -160,7 +157,6 @@ module SyntaxSuggest
160157
out = io.string
161158
expect(out).to include(<<~EOM)
162159
16 class Rexe
163-
18 VERSION = '1.5.1'
164160
> 77 class Lookups
165161
> 140 def format_requires
166162
> 148 end

spec/unit/capture_code_context_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@ def eat
1616
EOM
1717

1818
code_lines = CleanDocument.new(source: source).call.lines
19-
block = CodeBlock.new(lines: code_lines[0])
19+
block = CodeBlock.new(lines: code_lines[3])
2020

2121
display = CaptureCodeContext.new(
2222
blocks: [block],
2323
code_lines: code_lines
2424
)
25-
lines = display.call
25+
26+
lines = display.capture_before_after_kws(block).sort
2627
expect(lines.join).to eq(<<~'EOM')
2728
def sit
2829
end

spec/unit/display_invalid_blocks_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def hai
144144
expect(io.string).to include([
145145
" 1 class OH",
146146
"> 2 def hello",
147+
" 3 def hai",
147148
" 4 end",
148149
" 5 end",
149150
""
@@ -162,6 +163,7 @@ def hai
162163
[
163164
" 1 class OH",
164165
["> 2 ", DisplayCodeWithLineNumbers::TERMINAL_HIGHLIGHT, " def hello"].join,
166+
" 3 def hai",
165167
" 4 end",
166168
" 5 end",
167169
""

0 commit comments

Comments
 (0)