Class: Canon::Diff::SourceLocator
- Inherits:
-
Object
- Object
- Canon::Diff::SourceLocator
- 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.
Defined Under Namespace
Classes: LineMap
Class Method Summary collapse
-
.build_line_map(text) ⇒ LineMap
Build a line offset map from source text.
-
.find_line_for_offset(char_offset, line_map) ⇒ Integer?
Binary search for the line containing a character offset.
-
.locate(substring, text, line_map, start_from: nil) ⇒ Hash?
Locate a substring within source text and return its position.
-
.locate_all(substring, text, line_map) ⇒ Array<Hash>
Locate ALL occurrences of a substring within source text.
Class Method Details
.build_line_map(text) ⇒ LineMap
Build a line offset map from source text.
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.
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.
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.
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 |