Sha256: 4c633caa0383382e23e499340c8cbc7cd173ecd14a6172b55c0c4a76c160303c

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

# typed: strict
# frozen_string_literal: true

module RBI
  class Loc
    extend T::Sig

    sig { returns(T.nilable(String)) }
    attr_reader :file

    sig { returns(T.nilable(Integer)) }
    attr_reader :begin_line, :end_line, :begin_column, :end_column

    sig do
      params(
        file: T.nilable(String),
        begin_line: T.nilable(Integer),
        end_line: T.nilable(Integer),
        begin_column: T.nilable(Integer),
        end_column: T.nilable(Integer)
      ).void
    end
    def initialize(file: nil, begin_line: nil, end_line: nil, begin_column: nil, end_column: nil)
      @file = file
      @begin_line = begin_line
      @end_line = end_line
      @begin_column = begin_column
      @end_column = end_column
    end

    sig { returns(String) }
    def to_s
      "#{file}:#{begin_line}:#{begin_column}-#{end_line}:#{end_column}"
    end

    sig { returns(T.nilable(String)) }
    def source
      file = self.file
      return nil unless file
      return nil unless ::File.file?(file)

      return ::File.read(file) unless begin_line && end_line

      string = String.new
      ::File.foreach(file).with_index do |line, line_number|
        string << line if line_number + 1 >= begin_line && line_number + 1 <= end_line
      end
      string
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/rbi-0.0.16/lib/rbi/loc.rb
rbi-0.0.16 lib/rbi/loc.rb
rbi-0.0.15 lib/rbi/loc.rb
rbi-0.0.14 lib/rbi/loc.rb