Module: I18n::Tasks::Scanners::OccurrenceFromPosition

Included in:
PatternMapper, PatternScanner
Defined in:
lib/i18n/tasks/scanners/occurrence_from_position.rb

Instance Method Summary collapse

Instance Method Details

#occurrence_from_position(path, contents, position, raw_key: nil) ⇒ Results::Occurrence

Given a path to a file, its contents and a position in the file, return a Results::Occurrence at the position until the end of the line.

Parameters:

  • path (String)
  • contents (String)

    contents of the file at the path.

  • position (Integer)

    position just before the beginning of the match.

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/i18n/tasks/scanners/occurrence_from_position.rb', line 14

def occurrence_from_position(path, contents, position, raw_key: nil)
  line_begin = contents.rindex(/^/, position - 1)
  line_end   = contents.index(/.(?=\r?\n|$)/, position)
  Results::Occurrence.new(
    path: path,
    pos: position,
    line_num: contents[0..position].count("\n") + 1,
    line_pos: position - line_begin + 1,
    line: contents[line_begin..line_end],
    raw_key: raw_key
  )
end