Module: Ast::Merge::StructuralEdit::BoundarySupport

Extended by:
BoundarySupport
Includes:
LineRangeSupport
Included in:
BoundarySupport
Defined in:
lib/ast/merge/structural_edit/boundary_support.rb

Overview

Shared helpers for building structural-edit boundaries and removed attachment payloads from statement-like objects.

This module stays parser-agnostic: it only depends on the shared analysis attachment hooks (layout_attachment_for, comment_attachment_for) plus statement-like line-range interfaces (start_line / end_line or source_position).

Instance Method Summary collapse

Instance Method Details

#attachment_preserves_fragments?(attachment) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
# File 'lib/ast/merge/structural_edit/boundary_support.rb', line 57

def attachment_preserves_fragments?(attachment)
  return false unless attachment
  return !attachment.empty? if attachment.respond_to?(:empty?)

  gaps = attachment.respond_to?(:gaps) ? attachment.gaps : []
  regions = attachment.respond_to?(:regions) ? attachment.regions : []
  gaps.any? || regions.any?
end

#build_splice_boundary(analysis, statement, edge:, source: nil) ⇒ Boundary?

Build a boundary descriptor for a surviving statement adjacent to a splice.

Parameters:

  • analysis (Object)

    analysis exposing shared attachment hooks

  • statement (Object, nil)

    adjacent surviving statement

  • edge (Symbol)

    boundary edge, :leading or :trailing

  • source (Symbol, String, nil) (defaults to: nil)

    optional metadata source tag

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ast/merge/structural_edit/boundary_support.rb', line 24

def build_splice_boundary(analysis, statement, edge:, source: nil)
  return unless statement

  owner = statement_owner(statement)

  Boundary.new(
    edge: edge,
    owner: owner,
    layout_attachment: analysis.respond_to?(:layout_attachment_for) ? analysis.layout_attachment_for(owner) : nil,
    comment_attachment: analysis.respond_to?(:comment_attachment_for) ? analysis.comment_attachment_for(owner) : nil,
    metadata: source ? { source: source } : {}
  )
end

#removed_statement_attachments_for(analysis, statements) ⇒ Array<Object>

Collect comment/layout attachments from removed statements when they preserve fragments.

Parameters:

  • analysis (Object)

    analysis exposing attachment hooks

  • statements (Array<Object>)

    removed statements

Returns:

  • (Array<Object>)


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ast/merge/structural_edit/boundary_support.rb', line 43

def removed_statement_attachments_for(analysis, statements)
  Array(statements).filter_map do |statement|
    owner = statement_owner(statement)

    comment_attachment = if analysis.respond_to?(:comment_attachment_for)
                           analysis.comment_attachment_for(owner)
                         end
    next comment_attachment if attachment_preserves_fragments?(comment_attachment)

    layout_attachment = (analysis.layout_attachment_for(owner) if analysis.respond_to?(:layout_attachment_for))
    next layout_attachment if attachment_preserves_fragments?(layout_attachment)
  end
end

#statement_end_line(statement) ⇒ Integer?

Return the ending line for a statement-like object.

Parameters:

  • statement (Object)

Returns:

  • (Integer, nil)


78
79
80
# File 'lib/ast/merge/structural_edit/boundary_support.rb', line 78

def statement_end_line(statement)
  object_end_line(statement)
end

#statement_start_line(statement) ⇒ Integer?

Return the starting line for a statement-like object.

Parameters:

  • statement (Object)

Returns:

  • (Integer, nil)


70
71
72
# File 'lib/ast/merge/structural_edit/boundary_support.rb', line 70

def statement_start_line(statement)
  object_start_line(statement)
end