Class: Ast::Merge::Detector::Region

Inherits:
Struct
  • Object
show all
Defined in:
lib/ast/merge/detector/base.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 }
)

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



40
41
42
# File 'lib/ast/merge/detector/base.rb', line 40

def content
  @content
end

#delimitersObject

Returns the value of attribute delimiters

Returns:

  • (Object)

    the current value of delimiters



40
41
42
# File 'lib/ast/merge/detector/base.rb', line 40

def delimiters
  @delimiters
end

#end_lineObject

Returns the value of attribute end_line

Returns:

  • (Object)

    the current value of end_line



40
41
42
# File 'lib/ast/merge/detector/base.rb', line 40

def end_line
  @end_line
end

#metadataObject

Returns the value of attribute metadata

Returns:

  • (Object)

    the current value of metadata



40
41
42
# File 'lib/ast/merge/detector/base.rb', line 40

def 
  @metadata
end

#start_lineObject

Returns the value of attribute start_line

Returns:

  • (Object)

    the current value of start_line



40
41
42
# File 'lib/ast/merge/detector/base.rb', line 40

def start_line
  @start_line
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



40
41
42
# File 'lib/ast/merge/detector/base.rb', line 40

def type
  @type
end

Instance Method Details

#contains_line?(line) ⇒ Boolean

Checks if this region contains the given line number.

Parameters:

  • line (Integer)

    The line number to check (1-indexed)

Returns:

  • (Boolean)


85
86
87
# File 'lib/ast/merge/detector/base.rb', line 85

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

#full_textString

Reconstructs the full region text including delimiters.

Returns:

  • (String)


74
75
76
77
78
79
80
# File 'lib/ast/merge/detector/base.rb', line 74

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

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

#inspectString

Returns:

  • (String)


104
105
106
107
108
109
110
111
# File 'lib/ast/merge/detector/base.rb', line 104

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.

Returns:

  • (Integer)


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

def line_count
  end_line - start_line + 1
end

#line_rangeRange

Returns the line range covered by this region.

Returns:

  • (Range)


62
63
64
# File 'lib/ast/merge/detector/base.rb', line 62

def line_range
  start_line..end_line
end

#overlaps?(other) ⇒ Boolean

Checks if this region overlaps with another region.

Parameters:

  • other (Region)

    Another region

Returns:

  • (Boolean)


92
93
94
95
96
# File 'lib/ast/merge/detector/base.rb', line 92

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:

  • (String)


99
100
101
# File 'lib/ast/merge/detector/base.rb', line 99

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