Class: Prism::Merge::BlockDirectiveDetector
- Inherits:
-
Ruby::Merge::BlockDirectiveDetector
- Object
- Ruby::Merge::BlockDirectiveDetector
- Prism::Merge::BlockDirectiveDetector
- Defined in:
- lib/prism/merge/block_directive_detector.rb
Overview
Prism-specific projection for Ruby block directive spans.
Ruby directive token detection lives in Ruby::Merge::BlockDirectiveDetector. This class only promotes detected spans into Prism merge nodes.
Instance Method Summary collapse
Instance Method Details
#promote_spans_to_nodes(statements, spans, analysis:) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/prism/merge/block_directive_detector.rb', line 10 def promote_spans_to_nodes(statements, spans, analysis:) return statements if spans.empty? validate_same_syntactic_level!(spans, statements) top_level = top_level_spans_only(spans).reject do |span| statements.any? do |stmt| start_line = stmt_start_line(stmt) end_line = stmt_end_line(stmt) start_line && end_line && start_line <= span.start_line && end_line >= span.end_line end end return statements if top_level.empty? result = [] used_indices = Set.new top_level.each do |span| inner_stmts = statements.each_with_index.select do |stmt, index| next false if used_indices.include?(index) start_line = stmt_start_line(stmt) end_line = stmt_end_line(stmt) start_line && end_line && start_line >= span.start_line && end_line <= span.end_line end.map { |stmt, index| [index, stmt] } inner_indices = inner_stmts.map(&:first) inner_nodes = inner_stmts.map(&:last) nested = spans.select do |candidate| candidate != span && candidate.start_line >= span.start_line && candidate.end_line <= span.end_line end inner_nodes = promote_spans_to_nodes(inner_nodes, nested, analysis: analysis) unless nested.empty? insert_at = if inner_indices.any? inner_indices.min else after_span = statements.each_with_index.find do |stmt, _| stmt_start_line(stmt).to_i > span.start_line end after_span ? after_span.last : statements.length end result << [insert_at, build_directive_node(span, inner_nodes, analysis), inner_indices] used_indices.merge(inner_indices) end rebuild_statements(statements, result, used_indices) end |