Class: Canon::Diff::DiffLine
- Inherits:
-
Object
- Object
- Canon::Diff::DiffLine
- Defined in:
- lib/canon/diff/diff_line.rb
Overview
Represents a single line in the diff output Links textual representation to semantic DiffNode and DiffCharRanges
Instance Attribute Summary collapse
-
#char_ranges ⇒ Object
readonly
Returns the value of attribute char_ranges.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#diff_node ⇒ Object
readonly
Returns the value of attribute diff_node.
-
#formatting ⇒ Object
writeonly
Sets the attribute formatting.
-
#line_number ⇒ Object
readonly
Returns the value of attribute line_number.
-
#new_char_ranges ⇒ Object
readonly
Returns the value of attribute new_char_ranges.
-
#new_content ⇒ Object
readonly
Returns the value of attribute new_content.
-
#new_position ⇒ Object
readonly
Returns the value of attribute new_position.
-
#old_content ⇒ Object
Returns the value of attribute old_content.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#add_char_range(char_range) ⇒ Object
Add a character range for the text1 (old) side.
-
#add_new_char_range(char_range) ⇒ Object
Add a character range for the text2 (new) side.
-
#added? ⇒ Boolean
True if this line was added.
-
#changed? ⇒ Boolean
True if this line was changed.
-
#char_ranges_for_side(side) ⇒ Array<DiffCharRange>
Get character ranges for a specific side.
-
#formatting? ⇒ Boolean
Formatting diffs are purely cosmetic (whitespace, line breaks) with no semantic meaning.
-
#has_char_ranges? ⇒ Boolean
True if this line has any character ranges.
-
#informative? ⇒ Boolean
If diff_node is nil (not linked), it’s not informative either (it’s unchanged/cosmetic) Formatting-only diffs are never informative.
-
#initialize(line_number:, content:, type:, diff_node: nil, formatting: false, new_position: nil, old_content: nil, char_ranges: nil, new_char_ranges: nil, new_content: nil) ⇒ DiffLine
constructor
A new instance of DiffLine.
-
#normative? ⇒ Boolean
If diff_node is nil (not linked to any semantic difference), the line is considered informative (cosmetic/unchanged) Formatting-only diffs are never normative.
-
#removed? ⇒ Boolean
True if this line was removed.
- #to_h ⇒ Object
-
#unchanged? ⇒ Boolean
True if this line is unchanged.
Constructor Details
#initialize(line_number:, content:, type:, diff_node: nil, formatting: false, new_position: nil, old_content: nil, char_ranges: nil, new_char_ranges: nil, new_content: nil) ⇒ DiffLine
Returns a new instance of DiffLine.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/canon/diff/diff_line.rb', line 24 def initialize(line_number:, content:, type:, diff_node: nil, formatting: false, new_position: nil, old_content: nil, char_ranges: nil, new_char_ranges: nil, new_content: nil) @line_number = line_number @new_position = new_position @content = content @type = type @diff_node = diff_node @formatting = formatting @old_content = old_content @char_ranges = char_ranges || [] @new_char_ranges = new_char_ranges || [] @new_content = new_content end |
Instance Attribute Details
#char_ranges ⇒ Object (readonly)
Returns the value of attribute char_ranges.
8 9 10 |
# File 'lib/canon/diff/diff_line.rb', line 8 def char_ranges @char_ranges end |
#content ⇒ Object (readonly)
Returns the value of attribute content.
8 9 10 |
# File 'lib/canon/diff/diff_line.rb', line 8 def content @content end |
#diff_node ⇒ Object (readonly)
Returns the value of attribute diff_node.
8 9 10 |
# File 'lib/canon/diff/diff_line.rb', line 8 def diff_node @diff_node end |
#formatting=(value) ⇒ Object (writeonly)
Sets the attribute formatting
11 12 13 |
# File 'lib/canon/diff/diff_line.rb', line 11 def formatting=(value) @formatting = value end |
#line_number ⇒ Object (readonly)
Returns the value of attribute line_number.
8 9 10 |
# File 'lib/canon/diff/diff_line.rb', line 8 def line_number @line_number end |
#new_char_ranges ⇒ Object (readonly)
Returns the value of attribute new_char_ranges.
8 9 10 |
# File 'lib/canon/diff/diff_line.rb', line 8 def new_char_ranges @new_char_ranges end |
#new_content ⇒ Object (readonly)
Returns the value of attribute new_content.
8 9 10 |
# File 'lib/canon/diff/diff_line.rb', line 8 def new_content @new_content end |
#new_position ⇒ Object (readonly)
Returns the value of attribute new_position.
8 9 10 |
# File 'lib/canon/diff/diff_line.rb', line 8 def new_position @new_position end |
#old_content ⇒ Object
Returns the value of attribute old_content.
10 11 12 |
# File 'lib/canon/diff/diff_line.rb', line 10 def old_content @old_content end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
8 9 10 |
# File 'lib/canon/diff/diff_line.rb', line 8 def type @type end |
Instance Method Details
#==(other) ⇒ Object
126 127 128 129 130 131 132 133 134 |
# File 'lib/canon/diff/diff_line.rb', line 126 def ==(other) other.is_a?(DiffLine) && line_number == other.line_number && new_position == other.new_position && content == other.content && type == other.type && diff_node == other.diff_node && @formatting == other.instance_variable_get(:@formatting) end |
#add_char_range(char_range) ⇒ Object
Add a character range for the text1 (old) side
41 42 43 |
# File 'lib/canon/diff/diff_line.rb', line 41 def add_char_range(char_range) @char_ranges << char_range end |
#add_new_char_range(char_range) ⇒ Object
Add a character range for the text2 (new) side
47 48 49 |
# File 'lib/canon/diff/diff_line.rb', line 47 def add_new_char_range(char_range) @new_char_ranges << char_range end |
#added? ⇒ Boolean
Returns true if this line was added.
96 97 98 |
# File 'lib/canon/diff/diff_line.rb', line 96 def added? type == :added end |
#changed? ⇒ Boolean
Returns true if this line was changed.
106 107 108 |
# File 'lib/canon/diff/diff_line.rb', line 106 def changed? type == :changed end |
#char_ranges_for_side(side) ⇒ Array<DiffCharRange>
Get character ranges for a specific side
59 60 61 |
# File 'lib/canon/diff/diff_line.rb', line 59 def char_ranges_for_side(side) side == :old ? @char_ranges : @new_char_ranges end |
#formatting? ⇒ Boolean
Formatting diffs are purely cosmetic (whitespace, line breaks) with no semantic meaning
86 87 88 |
# File 'lib/canon/diff/diff_line.rb', line 86 def formatting? @formatting == true end |
#has_char_ranges? ⇒ Boolean
Returns true if this line has any character ranges.
52 53 54 |
# File 'lib/canon/diff/diff_line.rb', line 52 def has_char_ranges? !@char_ranges.empty? || !@new_char_ranges.empty? end |
#informative? ⇒ Boolean
If diff_node is nil (not linked), it’s not informative either (it’s unchanged/cosmetic) Formatting-only diffs are never informative
77 78 79 80 81 82 |
# File 'lib/canon/diff/diff_line.rb', line 77 def informative? return false if formatting? return false if diff_node.nil? diff_node.informative? end |
#normative? ⇒ Boolean
If diff_node is nil (not linked to any semantic difference), the line is considered informative (cosmetic/unchanged) Formatting-only diffs are never normative
67 68 69 70 71 72 |
# File 'lib/canon/diff/diff_line.rb', line 67 def normative? return false if formatting? return false if diff_node.nil? diff_node.normative? end |
#removed? ⇒ Boolean
Returns true if this line was removed.
101 102 103 |
# File 'lib/canon/diff/diff_line.rb', line 101 def removed? type == :removed end |
#to_h ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/canon/diff/diff_line.rb', line 110 def to_h { line_number: line_number, new_position: new_position, content: content, new_content: new_content, type: type, diff_node: diff_node&.to_h, normative: normative?, informative: informative?, formatting: formatting?, char_ranges: @char_ranges.map(&:to_h), new_char_ranges: @new_char_ranges.map(&:to_h), } end |
#unchanged? ⇒ Boolean
Returns true if this line is unchanged.
91 92 93 |
# File 'lib/canon/diff/diff_line.rb', line 91 def unchanged? type == :unchanged end |