Class: Canon::Diff::DiffCharRange
- Inherits:
-
Object
- Object
- Canon::Diff::DiffCharRange
- Defined in:
- lib/canon/diff/diff_char_range.rb
Overview
Represents a character range within a source line, linked to a DiffNode.
DiffCharRange is the core abstraction for character-level diff rendering. Each DiffNode produces one or more DiffCharRanges (before-text, changed-text, after-text) that tell the formatter exactly which characters to highlight.
The formatter reads DiffCharRanges and applies colors — no computation needed.
Instance Attribute Summary collapse
-
#diff_node ⇒ DiffNode?
readonly
The originating DiffNode.
-
#end_col ⇒ Integer
readonly
Exclusive end (half-open [start_col, end_col)).
-
#line_number ⇒ Integer
readonly
0-based line index in text1 or text2.
-
#role ⇒ Symbol
readonly
:before, :changed, :after.
-
#side ⇒ Symbol
readonly
:old (text1) or :new (text2).
-
#start_col ⇒ Integer
readonly
0-based column offset (inclusive start).
-
#status ⇒ Symbol
readonly
:unchanged, :removed, :added, :changed_old, :changed_new.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#changed_new? ⇒ Boolean
True if this range represents a change on the new side.
-
#changed_old? ⇒ Boolean
True if this range represents a change on the old side.
-
#covers_entire_line?(line_length) ⇒ Boolean
True if this range covers the entire line.
-
#empty? ⇒ Boolean
True if this range has zero length.
-
#extract_from(line_text) ⇒ String
Extract the substring this range covers from a line.
-
#highlighted? ⇒ Boolean
True if this range should be highlighted.
-
#initialize(line_number:, start_col:, end_col:, side:, status:, role: nil, diff_node: nil) ⇒ DiffCharRange
constructor
A new instance of DiffCharRange.
-
#length ⇒ Integer
Number of characters in this range.
-
#new_side? ⇒ Boolean
True if this is a new-side (text2) range.
-
#old_side? ⇒ Boolean
True if this is an old-side (text1) range.
- #to_h ⇒ Object
-
#unchanged? ⇒ Boolean
True if this range represents unchanged content.
Constructor Details
#initialize(line_number:, start_col:, end_col:, side:, status:, role: nil, diff_node: nil) ⇒ DiffCharRange
Returns a new instance of DiffCharRange.
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/canon/diff/diff_char_range.rb', line 52 def initialize(line_number:, start_col:, end_col:, side:, status:, role: nil, diff_node: nil) @line_number = line_number @start_col = start_col @end_col = end_col @side = side @status = status @role = role @diff_node = diff_node end |
Instance Attribute Details
#diff_node ⇒ DiffNode? (readonly)
Returns the originating DiffNode.
43 44 45 |
# File 'lib/canon/diff/diff_char_range.rb', line 43 def diff_node @diff_node end |
#end_col ⇒ Integer (readonly)
Returns exclusive end (half-open [start_col, end_col)).
31 32 33 |
# File 'lib/canon/diff/diff_char_range.rb', line 31 def end_col @end_col end |
#line_number ⇒ Integer (readonly)
Returns 0-based line index in text1 or text2.
25 26 27 |
# File 'lib/canon/diff/diff_char_range.rb', line 25 def line_number @line_number end |
#role ⇒ Symbol (readonly)
Returns :before, :changed, :after.
40 41 42 |
# File 'lib/canon/diff/diff_char_range.rb', line 40 def role @role end |
#side ⇒ Symbol (readonly)
Returns :old (text1) or :new (text2).
34 35 36 |
# File 'lib/canon/diff/diff_char_range.rb', line 34 def side @side end |
#start_col ⇒ Integer (readonly)
Returns 0-based column offset (inclusive start).
28 29 30 |
# File 'lib/canon/diff/diff_char_range.rb', line 28 def start_col @start_col end |
#status ⇒ Symbol (readonly)
Returns :unchanged, :removed, :added, :changed_old, :changed_new.
37 38 39 |
# File 'lib/canon/diff/diff_char_range.rb', line 37 def status @status end |
Instance Method Details
#==(other) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/canon/diff/diff_char_range.rb', line 129 def ==(other) other.is_a?(DiffCharRange) && line_number == other.line_number && start_col == other.start_col && end_col == other.end_col && side == other.side && status == other.status && role == other.role end |
#changed_new? ⇒ Boolean
Returns true if this range represents a change on the new side.
109 110 111 |
# File 'lib/canon/diff/diff_char_range.rb', line 109 def changed_new? status == :changed_new end |
#changed_old? ⇒ Boolean
Returns true if this range represents a change on the old side.
104 105 106 |
# File 'lib/canon/diff/diff_char_range.rb', line 104 def changed_old? status == :changed_old end |
#covers_entire_line?(line_length) ⇒ Boolean
Returns true if this range covers the entire line.
75 76 77 |
# File 'lib/canon/diff/diff_char_range.rb', line 75 def covers_entire_line?(line_length) start_col.zero? && end_col >= line_length end |
#empty? ⇒ Boolean
Returns true if this range has zero length.
64 65 66 |
# File 'lib/canon/diff/diff_char_range.rb', line 64 def empty? start_col >= end_col end |
#extract_from(line_text) ⇒ String
Extract the substring this range covers from a line
82 83 84 85 86 |
# File 'lib/canon/diff/diff_char_range.rb', line 82 def extract_from(line_text) return "" if line_text.nil? || empty? line_text[start_col...end_col] || "" end |
#highlighted? ⇒ Boolean
Returns true if this range should be highlighted.
114 115 116 |
# File 'lib/canon/diff/diff_char_range.rb', line 114 def highlighted? %i[changed_old changed_new removed added].include?(status) end |
#length ⇒ Integer
Returns number of characters in this range.
69 70 71 |
# File 'lib/canon/diff/diff_char_range.rb', line 69 def length end_col - start_col end |
#new_side? ⇒ Boolean
Returns true if this is a new-side (text2) range.
94 95 96 |
# File 'lib/canon/diff/diff_char_range.rb', line 94 def new_side? side == :new end |
#old_side? ⇒ Boolean
Returns true if this is an old-side (text1) range.
89 90 91 |
# File 'lib/canon/diff/diff_char_range.rb', line 89 def old_side? side == :old end |
#to_h ⇒ Object
118 119 120 121 122 123 124 125 126 127 |
# File 'lib/canon/diff/diff_char_range.rb', line 118 def to_h { line_number: line_number, start_col: start_col, end_col: end_col, side: side, status: status, role: role, } end |
#unchanged? ⇒ Boolean
Returns true if this range represents unchanged content.
99 100 101 |
# File 'lib/canon/diff/diff_char_range.rb', line 99 def unchanged? status == :unchanged end |