Class: Canon::Diff::DiffLine

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/diff/diff_line.rb

Overview

Represents a single line in the diff output Links textual representation to semantic DiffNode

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_number:, content:, type:, diff_node: nil, formatting: false) ⇒ DiffLine

Returns a new instance of DiffLine.

Parameters:

  • line_number (Integer)

    The line number in the original text

  • content (String)

    The text content of the line

  • type (Symbol)

    The type of line (:unchanged, :added, :removed, :changed)

  • diff_node (DiffNode, nil) (defaults to: nil)

    The semantic diff node this line belongs to

  • formatting (Boolean) (defaults to: false)

    Whether this is a formatting-only difference



15
16
17
18
19
20
21
22
# File 'lib/canon/diff/diff_line.rb', line 15

def initialize(line_number:, content:, type:, diff_node: nil,
formatting: false)
  @line_number = line_number
  @content = content
  @type = type
  @diff_node = diff_node
  @formatting = formatting
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



8
9
10
# File 'lib/canon/diff/diff_line.rb', line 8

def content
  @content
end

#diff_nodeObject (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

#line_numberObject (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

#typeObject (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



83
84
85
86
87
88
89
90
# File 'lib/canon/diff/diff_line.rb', line 83

def ==(other)
  other.is_a?(DiffLine) &&
    line_number == other.line_number &&
    content == other.content &&
    type == other.type &&
    diff_node == other.diff_node &&
    @formatting == other.instance_variable_get(:@formatting)
end

#added?Boolean

Returns true if this line was added.

Returns:

  • (Boolean)

    true if this line was added



57
58
59
# File 'lib/canon/diff/diff_line.rb', line 57

def added?
  type == :added
end

#changed?Boolean

Returns true if this line was changed.

Returns:

  • (Boolean)

    true if this line was changed



67
68
69
# File 'lib/canon/diff/diff_line.rb', line 67

def changed?
  type == :changed
end

#formatting?Boolean

Formatting diffs are purely cosmetic (whitespace, line breaks) with no semantic meaning

Returns:

  • (Boolean)

    true if this line represents a formatting-only difference



47
48
49
# File 'lib/canon/diff/diff_line.rb', line 47

def formatting?
  @formatting == true
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

Returns:

  • (Boolean)

    true if this line represents an informative-only difference



38
39
40
41
42
43
# File 'lib/canon/diff/diff_line.rb', line 38

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

Returns:

  • (Boolean)

    true if this line represents a normative difference



28
29
30
31
32
33
# File 'lib/canon/diff/diff_line.rb', line 28

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.

Returns:

  • (Boolean)

    true if this line was removed



62
63
64
# File 'lib/canon/diff/diff_line.rb', line 62

def removed?
  type == :removed
end

#to_hObject



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/canon/diff/diff_line.rb', line 71

def to_h
  {
    line_number: line_number,
    content: content,
    type: type,
    diff_node: diff_node&.to_h,
    normative: normative?,
    informative: informative?,
    formatting: formatting?,
  }
end

#unchanged?Boolean

Returns true if this line is unchanged.

Returns:

  • (Boolean)

    true if this line is unchanged



52
53
54
# File 'lib/canon/diff/diff_line.rb', line 52

def unchanged?
  type == :unchanged
end