Class: Canon::Diff::SourceLocator

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

Overview

Locates serialized content within source text and maps character offsets to line/column positions. Used during DiffNode enrichment (Phase 1).

The SourceLocator uses String#index on the full source text (not LCS on lines) to find where a DiffNode's serialized content appears. It then maps the character offset to a line number and column position using a pre-built line offset map.

Examples:

line_map = SourceLocator.build_line_map("line1\nline2\nline3")
SourceLocator.locate("line2", "line1\nline2\nline3", line_map)
# => { char_offset: 6, line_number: 1, col: 0 }

Defined Under Namespace

Classes: LineMap

Class Method Summary collapse

Class Method Details

.build_line_map(text) ⇒ LineMap

Build a line offset map from source text.

Parameters:

  • text (String)

    the full source text

Returns:

  • (LineMap)

    flat offset arrays, one entry per line (0-indexed)



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/canon/diff/source_locator.rb', line 51

def self.build_line_map(text)
  return LineMap.new([], []) if text.nil? || text.empty?

  starts = []
  offset = 0
  text.each_line do |line|
    starts << offset
    offset += line.length
  end
  ends = starts[1..] || []
  ends << text.length
  LineMap.new(starts, ends)
end

.find_line_for_offset(char_offset, line_map) ⇒ Integer?

Binary search for the line containing a character offset.

Parameters:

  • char_offset (Integer)

    the character offset

  • line_map (Array<Hash>)

    the line offset map

Returns:

  • (Integer, nil)

    the 0-based line index, or nil



122
123
124
125
126
# File 'lib/canon/diff/source_locator.rb', line 122

def find_line_for_offset(char_offset, line_map)
  line_map.ends.bsearch_index do |end_offset|
    end_offset > char_offset
  end
end

.locate(substring, text, line_map, start_from: nil) ⇒ Hash?

Locate a substring within source text and return its position.

Parameters:

  • substring (String)

    the content to find (e.g., serialized_before)

  • text (String)

    the full source text

  • line_map (Array<Hash>)

    pre-built line offset map

  • start_from (Integer, nil) (defaults to: nil)

    character offset to start searching from

Returns:

  • (Hash, nil)

    { char_offset:, line_number:, col: } or nil if not found



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/canon/diff/source_locator.rb', line 72

def self.locate(substring, text, line_map, start_from: nil)
  return nil if substring.nil? || substring.empty?
  return nil if text.nil? || line_map.empty?

  char_offset = if start_from
                  text.index(substring, start_from)
                else
                  text.index(substring)
                end
  return nil if char_offset.nil?

  line_idx = find_line_for_offset(char_offset, line_map)
  return nil if line_idx.nil?

  col = char_offset - line_map.start_at(line_idx)

  { char_offset: char_offset, line_number: line_idx, col: col }
end

.locate_all(substring, text, line_map) ⇒ Array<Hash>

Locate ALL occurrences of a substring within source text.

Parameters:

  • substring (String)

    the content to find

  • text (String)

    the full source text

  • line_map (Array<Hash>)

    pre-built line offset map

Returns:

  • (Array<Hash>)

    array of { char_offset:, line_number:, col: } hashes



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/canon/diff/source_locator.rb', line 97

def self.locate_all(substring, text, line_map)
  return [] if substring.nil? || substring.empty?
  return [] if text.nil? || line_map.empty?

  results = []
  offset = 0

  while (pos = text.index(substring, offset))
    line_idx = find_line_for_offset(pos, line_map)
    break if line_idx.nil?

    col = pos - line_map.start_at(line_idx)
    results << { char_offset: pos, line_number: line_idx, col: col }
    offset = pos + 1
  end

  results
end