Class: Canon::Diff::DiffContext

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

Overview

Represents a context - a group of diff blocks with surrounding context lines A context is created by grouping nearby diff blocks and expanding with context lines

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_line: nil, end_line: nil, start_idx: nil, end_idx: nil, blocks: [], lines: [], normative: nil) ⇒ DiffContext

Returns a new instance of DiffContext.



11
12
13
14
15
16
17
18
19
# File 'lib/canon/diff/diff_context.rb', line 11

def initialize(start_line: nil, end_line: nil, start_idx: nil,
               end_idx: nil, blocks: [], lines: [], normative: nil)
  # Support both old (start_idx/end_idx) and new (start_line/end_line) signatures
  @start_idx = start_line || start_idx
  @end_idx = end_line || end_idx
  @blocks = blocks
  @lines = lines
  @normative = normative
end

Instance Attribute Details

#blocksObject (readonly)

Returns the value of attribute blocks.



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

def blocks
  @blocks
end

#end_idxObject (readonly)

Returns the value of attribute end_idx.



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

def end_idx
  @end_idx
end

#linesObject (readonly)

Returns the value of attribute lines.



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

def lines
  @lines
end

#normativeObject

Returns the value of attribute normative.



9
10
11
# File 'lib/canon/diff/diff_context.rb', line 9

def normative
  @normative
end

#start_idxObject (readonly)

Returns the value of attribute start_idx.



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

def start_idx
  @start_idx
end

Instance Method Details

#==(other) ⇒ Object



75
76
77
78
79
80
# File 'lib/canon/diff/diff_context.rb', line 75

def ==(other)
  other.is_a?(DiffContext) &&
    start_idx == other.start_idx &&
    end_idx == other.end_idx &&
    blocks == other.blocks
end

#block_countObject

Number of diff blocks in this context



37
38
39
# File 'lib/canon/diff/diff_context.rb', line 37

def block_count
  blocks.length
end

#gap_to(other_context) ⇒ Object

Calculate gap to another context



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/canon/diff/diff_context.rb', line 47

def gap_to(other_context)
  return Float::INFINITY if other_context.nil?
  return 0 if overlaps?(other_context)

  if other_context.start_idx > end_idx
    other_context.start_idx - end_idx - 1
  elsif start_idx > other_context.end_idx
    start_idx - other_context.end_idx - 1
  else
    0
  end
end

#includes_type?(type) ⇒ Boolean

Check if this context contains changes of a specific type

Returns:

  • (Boolean)


42
43
44
# File 'lib/canon/diff/diff_context.rb', line 42

def includes_type?(type)
  blocks.any? { |block| block.includes_type?(type) }
end

#informative?Boolean

Returns true if this context contains only informative diffs.

Returns:

  • (Boolean)

    true if this context contains only informative diffs



27
28
29
# File 'lib/canon/diff/diff_context.rb', line 27

def informative?
  @normative == false
end

#normative?Boolean

Returns true if this context contains normative diffs.

Returns:

  • (Boolean)

    true if this context contains normative diffs



22
23
24
# File 'lib/canon/diff/diff_context.rb', line 22

def normative?
  @normative == true
end

#overlaps?(other_context) ⇒ Boolean

Check if this context overlaps with another

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/canon/diff/diff_context.rb', line 61

def overlaps?(other_context)
  return false if other_context.nil?

  !(end_idx < other_context.start_idx || start_idx > other_context.end_idx)
end

#sizeObject

Number of lines in this context (including context lines)



32
33
34
# File 'lib/canon/diff/diff_context.rb', line 32

def size
  end_idx - start_idx + 1
end

#to_hObject



67
68
69
70
71
72
73
# File 'lib/canon/diff/diff_context.rb', line 67

def to_h
  {
    start_idx: start_idx,
    end_idx: end_idx,
    blocks: blocks.map(&:to_h),
  }
end