diff options
author | schneems <[email protected]> | 2023-04-14 13:46:28 -0500 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2023-05-23 10:05:47 +0900 |
commit | aaf815c626816ccca227cb1f8f2a9b58f18f677a (patch) | |
tree | df1851edb5ed016a1b41124b851b3bf7a6daef40 /spec/syntax_suggest | |
parent | b8bf0a52723322e469e94a4cb96286fd48e754ab (diff) |
[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.
https://github.com/ruby/syntax_suggest/commit/ad8487d8aa
Diffstat (limited to 'spec/syntax_suggest')
-rw-r--r-- | spec/syntax_suggest/integration/syntax_suggest_spec.rb | 5 | ||||
-rw-r--r-- | spec/syntax_suggest/unit/around_block_scan_spec.rb | 41 | ||||
-rw-r--r-- | spec/syntax_suggest/unit/capture_code_context_spec.rb | 26 |
3 files changed, 68 insertions, 4 deletions
diff --git a/spec/syntax_suggest/integration/syntax_suggest_spec.rb b/spec/syntax_suggest/integration/syntax_suggest_spec.rb index f1ceac23c5..e701287985 100644 --- a/spec/syntax_suggest/integration/syntax_suggest_spec.rb +++ b/spec/syntax_suggest/integration/syntax_suggest_spec.rb @@ -21,10 +21,11 @@ module SyntaxSuggest filename: file ) end - debug_display(io.string) - debug_display(benchmark) end + debug_display(io.string) + debug_display(benchmark) + expect(io.string).to include(<<~'EOM') 6 class SyntaxTree < Ripper 170 def self.parse(source) diff --git a/spec/syntax_suggest/unit/around_block_scan_spec.rb b/spec/syntax_suggest/unit/around_block_scan_spec.rb index 88d973e151..c9241bc8ee 100644 --- a/spec/syntax_suggest/unit/around_block_scan_spec.rb +++ b/spec/syntax_suggest/unit/around_block_scan_spec.rb @@ -4,6 +4,43 @@ require_relative "../spec_helper" module SyntaxSuggest RSpec.describe AroundBlockScan do + it "on_falling_indent" do + source = <<~'EOM' + class OH + def lol + print 'lol + end + + def hello + it "foo" do + end + + def yolo + print 'haha' + end + end + EOM + + code_lines = CleanDocument.new(source: source).call.lines + block = CodeBlock.new(lines: code_lines[6]) + + lines = [] + AroundBlockScan.new( + block: block, + code_lines: code_lines + ).on_falling_indent do |line| + lines << line + end + lines.sort! + + expect(lines.join).to eq(<<~'EOM') + class OH + def hello + end + end + EOM + end + it "continues scan from last location even if scan is false" do source = <<~'EOM' print 'omg' @@ -104,8 +141,8 @@ module SyntaxSuggest expand = AroundBlockScan.new(code_lines: code_lines, block: block) expand.scan_while { true } - expect(expand.before_index).to eq(0) - expect(expand.after_index).to eq(6) + expect(expand.lines.first.index).to eq(0) + expect(expand.lines.last.index).to eq(6) expect(expand.code_block.to_s).to eq(source_string) end diff --git a/spec/syntax_suggest/unit/capture_code_context_spec.rb b/spec/syntax_suggest/unit/capture_code_context_spec.rb index 1563149b3c..46f13e8961 100644 --- a/spec/syntax_suggest/unit/capture_code_context_spec.rb +++ b/spec/syntax_suggest/unit/capture_code_context_spec.rb @@ -4,6 +4,32 @@ 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 |