Class: Ast::Merge::Region

Inherits:
Struct
  • Object
show all
Defined in:
lib/ast/merge/region.rb

Overview

Represents a detected region within a document.

Regions are portions of a document that can be handled by a specialized merger. For example, YAML frontmatter in a Markdown file, or a Ruby code block that should be merged using a Ruby-aware merger.

Examples:

Creating a region for YAML frontmatter

Region.new(
  type: :yaml_frontmatter,
  content: "title: My Doc\nversion: 1.0\n",
  start_line: 1,
  end_line: 4,
  delimiters: ["---", "---"],
  metadata: { format: :yaml }
)

Creating a region for a Ruby code block

Region.new(
  type: :ruby_code_block,
  content: "def hello\n  puts 'world'\nend\n",
  start_line: 5,
  end_line: 9,
  delimiters: ["```ruby", "```"],
  metadata: { language: "ruby" }
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject

Returns the value of attribute content

Returns:

  • (Object)

    the current value of content



32
33
34
# File 'lib/ast/merge/region.rb', line 32

def content
  @content
end

#delimitersObject

Returns the value of attribute delimiters

Returns:

  • (Object)

    the current value of delimiters



32
33
34
# File 'lib/ast/merge/region.rb', line 32

def delimiters
  @delimiters
end

#end_lineObject

Returns the value of attribute end_line

Returns:

  • (Object)

    the current value of end_line



32
33
34
# File 'lib/ast/merge/region.rb', line 32

def end_line
  @end_line
end

#metadataObject

Returns the value of attribute metadata

Returns:

  • (Object)

    the current value of metadata



32
33
34
# File 'lib/ast/merge/region.rb', line 32

def 
  
end

#start_lineObject

Returns the value of attribute start_line

Returns:

  • (Object)

    the current value of start_line



32
33
34
# File 'lib/ast/merge/region.rb', line 32

def start_line
  @start_line
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



32
33
34
# File 'lib/ast/merge/region.rb', line 32

def type
  @type
end

Instance Method Details

#contains_line?(line) ⇒ Boolean

Checks if this region overlaps with the given line number.

Parameters:

  • line (Integer)

    The line number to check (1-indexed)

Returns:

  • (Boolean)

    true if the line is within this region



90
91
92
# File 'lib/ast/merge/region.rb', line 90

def contains_line?(line)
  line_range.cover?(line)
end

#full_textString

Reconstructs the full region text including delimiters.

Examples:

region.full_text
# => "```ruby\ndef hello\n  puts 'world'\nend\n```"

Returns:

  • (String)

    The complete region with start and end delimiters



78
79
80
81
82
83
84
# File 'lib/ast/merge/region.rb', line 78

def full_text
  return content if delimiters.nil? || delimiters.empty?

  opening = delimiters[0] || ""
  closing = delimiters[1] || ""
  "#{opening}\n#{content}#{closing}"
end

#inspectString

Returns a detailed human-readable representation of the region.

Returns:

  • (String)

    A string describing the region with truncated content



114
115
116
117
118
119
120
121
# File 'lib/ast/merge/region.rb', line 114

def inspect
  truncated = if content && content.length > 30
    "#{content[0, 30]}..."
  else
    content.inspect
  end
  "#{self} #{truncated}"
end

#line_countInteger

Returns the number of lines this region spans.

Examples:

region.line_count # => 4

Returns:

  • (Integer)

    The number of lines



68
69
70
# File 'lib/ast/merge/region.rb', line 68

def line_count
  end_line - start_line + 1
end

#line_rangeRange

Returns the line range covered by this region.

Examples:

region.line_range # => 1..4

Returns:

  • (Range)

    The range from start_line to end_line (inclusive)



59
60
61
# File 'lib/ast/merge/region.rb', line 59

def line_range
  start_line..end_line
end

#overlaps?(other) ⇒ Boolean

Checks if this region overlaps with another region.

Parameters:

  • other (Region)

    Another region to check for overlap

Returns:

  • (Boolean)

    true if the regions overlap



98
99
100
101
102
# File 'lib/ast/merge/region.rb', line 98

def overlaps?(other)
  line_range.cover?(other.start_line) ||
    line_range.cover?(other.end_line) ||
    other.line_range.cover?(start_line)
end

#to_sString

Returns a short string representation of the region.

Returns:

  • (String)

    A concise string describing the region



107
108
109
# File 'lib/ast/merge/region.rb', line 107

def to_s
  "Region<#{type}:#{start_line}-#{end_line}>"
end