ruby/spec/syntax_suggest/unit/capture_code_context_spec.rb
schneems aaf815c626
[ruby/syntax_suggest] Refactor Scanner logic out of AroundBlockScan introduce history
AroundBlockScan started as a utility class that was meant to be used as a DSL for scanning and making new blocks. As logic got added to this class it became hard to reason about what exactly is being mutated when. I pulled the scanning logic out into it's own class which gives us a clean separation of concerns. This allowed me to remove a lot of accessors that aren't core to the logic provided by AroundBlockScan.

In addition to this refactor the ScanHistory class can snapshot a scan. This allows us to be more aggressive with scans in the future as we can now snapshot and rollback if it didn't turn out the way we wanted.

The change comes with a minor perf impact:

before: 5.092678   0.104299   5.196977 (  5.226494)
after: 5.128536   0.099871   5.228407 (  5.249542)

This represents a 0.996x change in speed (where 1x would be no change and 2x would be twice as fast). This is a 0.38% decrease in performance which is negligible. It's likely coming from the extra blocks being created while scanning. This is negligible and if the history feature works well we might be able to make better block decisions which is means fewer calls to ripper which is the biggest bottleneck.

While this doesn't fix https://github.com/ruby/syntax_suggest/issues/187 it's a good intermediate step that will hopefully make working on that issue easier.

ad8487d8aa
2023-05-23 10:05:47 +09:00

229 lines
4.8 KiB
Ruby

# frozen_string_literal: true
require_relative "../spec_helper"
module SyntaxSuggest
RSpec.describe CaptureCodeContext do
it "capture_before_after_kws two" do
source = <<~'EOM'
class OH
def hello
def hai
end
end
EOM
code_lines = CleanDocument.new(source: source).call.lines
block = CodeBlock.new(lines: code_lines[2])
display = CaptureCodeContext.new(
blocks: [block],
code_lines: code_lines
)
display.capture_before_after_kws(block)
expect(display.sorted_lines.join).to eq(<<~'EOM'.indent(2))
def hello
def hai
end
EOM
end
it "capture_before_after_kws" do
source = <<~'EOM'
def sit
end
def bark
def eat
end
EOM
code_lines = CleanDocument.new(source: source).call.lines
block = CodeBlock.new(lines: code_lines[3])
display = CaptureCodeContext.new(
blocks: [block],
code_lines: code_lines
)
lines = display.capture_before_after_kws(block).sort
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 # 0
print "lol" # 1
end # one # 2
end # two # 3
EOM
code_lines = CleanDocument.new(source: source).call.lines
code_lines[0..2].each(&:mark_invisible)
block = CodeBlock.new(lines: code_lines)
display = CaptureCodeContext.new(
blocks: [block],
code_lines: code_lines
)
lines = display.call
lines = lines.sort.map(&:original)
expect(lines.join).to eq(<<~'EOM')
def call # 0
end # one # 2
end # two # 3
EOM
end
it "shows ends of captured block" do
lines = fixtures_dir.join("rexe.rb.txt").read.lines
lines.delete_at(148 - 1)
source = lines.join
code_lines = CleanDocument.new(source: source).call.lines
code_lines[0..75].each(&:mark_invisible)
code_lines[77..-1].each(&:mark_invisible)
expect(code_lines.join.strip).to eq("class Lookups")
block = CodeBlock.new(lines: code_lines[76..149])
display = CaptureCodeContext.new(
blocks: [block],
code_lines: code_lines
)
lines = display.call
lines = lines.sort.map(&:original)
expect(lines.join).to include(<<~'EOM'.indent(2))
class Lookups
def format_requires
end
EOM
end
it "shows ends of captured block" do
source = <<~'EOM'
class Dog
def bark
puts "woof"
end
EOM
code_lines = CleanDocument.new(source: source).call.lines
block = CodeBlock.new(lines: code_lines)
code_lines[1..-1].each(&:mark_invisible)
expect(block.to_s.strip).to eq("class Dog")
display = CaptureCodeContext.new(
blocks: [block],
code_lines: code_lines
)
lines = display.call.sort.map(&:original)
expect(lines.join).to eq(<<~'EOM')
class Dog
def bark
end
EOM
end
it "captures surrounding context on falling indent" do
source = <<~'EOM'
class Blerg
end
class OH
def hello
it "foo" do
end
end
class Zerg
end
EOM
code_lines = CleanDocument.new(source: source).call.lines
block = CodeBlock.new(lines: code_lines[6])
expect(block.to_s.strip).to eq('it "foo" do')
display = CaptureCodeContext.new(
blocks: [block],
code_lines: code_lines
)
lines = display.call.sort.map(&:original)
expect(lines.join).to eq(<<~'EOM')
class OH
def hello
it "foo" do
end
end
EOM
end
it "captures surrounding context on same indent" do
source = <<~'EOM'
class Blerg
end
class OH
def nope
end
def lol
end
end # here
def haha
end
def nope
end
end
class Zerg
end
EOM
code_lines = CleanDocument.new(source: source).call.lines
block = CodeBlock.new(lines: code_lines[7..10])
expect(block.to_s).to eq(<<~'EOM'.indent(2))
def lol
end
end # here
EOM
code_context = CaptureCodeContext.new(
blocks: [block],
code_lines: code_lines
)
lines = code_context.call
out = DisplayCodeWithLineNumbers.new(
lines: lines
).call
expect(out).to eq(<<~'EOM'.indent(2))
3 class OH
8 def lol
9 end
11 end # here
18 end
EOM
end
end
end