Class: Canon::Diff::DiffContext
- Inherits:
-
Object
- Object
- Canon::Diff::DiffContext
- 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
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#end_idx ⇒ Object
readonly
Returns the value of attribute end_idx.
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#normative ⇒ Object
Returns the value of attribute normative.
-
#start_idx ⇒ Object
readonly
Returns the value of attribute start_idx.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#block_count ⇒ Object
Number of diff blocks in this context.
-
#gap_to(other_context) ⇒ Object
Calculate gap to another context.
-
#includes_type?(type) ⇒ Boolean
Check if this context contains changes of a specific type.
-
#informative? ⇒ Boolean
True if this context contains only informative diffs.
-
#initialize(start_line: nil, end_line: nil, start_idx: nil, end_idx: nil, blocks: [], lines: [], normative: nil) ⇒ DiffContext
constructor
A new instance of DiffContext.
-
#normative? ⇒ Boolean
True if this context contains normative diffs.
-
#overlaps?(other_context) ⇒ Boolean
Check if this context overlaps with another.
-
#size ⇒ Object
Number of lines in this context (including context lines).
- #to_h ⇒ Object
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.
13 14 15 16 17 18 19 20 21 |
# File 'lib/canon/diff/diff_context.rb', line 13 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
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
10 11 12 |
# File 'lib/canon/diff/diff_context.rb', line 10 def blocks @blocks end |
#end_idx ⇒ Object (readonly)
Returns the value of attribute end_idx.
10 11 12 |
# File 'lib/canon/diff/diff_context.rb', line 10 def end_idx @end_idx end |
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
10 11 12 |
# File 'lib/canon/diff/diff_context.rb', line 10 def lines @lines end |
#normative ⇒ Object
Returns the value of attribute normative.
11 12 13 |
# File 'lib/canon/diff/diff_context.rb', line 11 def normative @normative end |
#start_idx ⇒ Object (readonly)
Returns the value of attribute start_idx.
10 11 12 |
# File 'lib/canon/diff/diff_context.rb', line 10 def start_idx @start_idx end |
Instance Method Details
#==(other) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/canon/diff/diff_context.rb', line 77 def ==(other) other.is_a?(DiffContext) && start_idx == other.start_idx && end_idx == other.end_idx && blocks == other.blocks end |
#block_count ⇒ Object
Number of diff blocks in this context
39 40 41 |
# File 'lib/canon/diff/diff_context.rb', line 39 def block_count blocks.length end |
#gap_to(other_context) ⇒ Object
Calculate gap to another context
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/canon/diff/diff_context.rb', line 49 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
44 45 46 |
# File 'lib/canon/diff/diff_context.rb', line 44 def includes_type?(type) blocks.any? { |block| block.includes_type?(type) } end |
#informative? ⇒ Boolean
Returns true if this context contains only informative diffs.
29 30 31 |
# File 'lib/canon/diff/diff_context.rb', line 29 def informative? @normative == false end |
#normative? ⇒ Boolean
Returns true if this context contains normative diffs.
24 25 26 |
# File 'lib/canon/diff/diff_context.rb', line 24 def normative? @normative == true end |
#overlaps?(other_context) ⇒ Boolean
Check if this context overlaps with another
63 64 65 66 67 |
# File 'lib/canon/diff/diff_context.rb', line 63 def overlaps?(other_context) return false if other_context.nil? !(end_idx < other_context.start_idx || start_idx > other_context.end_idx) end |
#size ⇒ Object
Number of lines in this context (including context lines)
34 35 36 |
# File 'lib/canon/diff/diff_context.rb', line 34 def size end_idx - start_idx + 1 end |
#to_h ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/canon/diff/diff_context.rb', line 69 def to_h { start_idx: start_idx, end_idx: end_idx, blocks: blocks.map(&:to_h), } end |