Sha256: 472c08f5ac8893503ee038631fc31dfcb9a5a3f5e7bc504dee9549ff572204a3

Contents?: true

Size: 1.02 KB

Versions: 6

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true
module I18n
  module Tasks
    module Scanners
      module OccurrenceFromPosition
        # 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.
        #
        # @param path [String]
        # @param contents [String] contents of the file at the path.
        # @param position [Integer] position just before the beginning of the match.
        # @return [Results::Occurrence]
        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
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
i18n-tasks-0.9.12 lib/i18n/tasks/scanners/occurrence_from_position.rb
i18n-tasks-0.9.11 lib/i18n/tasks/scanners/occurrence_from_position.rb
i18n-tasks-0.9.10 lib/i18n/tasks/scanners/occurrence_from_position.rb
i18n-tasks-0.9.9 lib/i18n/tasks/scanners/occurrence_from_position.rb
i18n-tasks-0.9.8 lib/i18n/tasks/scanners/occurrence_from_position.rb
i18n-tasks-0.9.7 lib/i18n/tasks/scanners/occurrence_from_position.rb