Class: RBI::Loc

Inherits:
Object
  • Object
show all
Defined in:
lib/rbi/loc.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file: nil, begin_line: nil, end_line: nil, begin_column: nil, end_column: nil) ⇒ Loc

: (?file: String?, ?begin_line: Integer?, ?end_line: Integer?, ?begin_column: Integer?, ?end_column: Integer?) -> void



26
27
28
29
30
31
32
# File 'lib/rbi/loc.rb', line 26

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

Instance Attribute Details

#begin_columnObject (readonly)

: Integer?



23
24
25
# File 'lib/rbi/loc.rb', line 23

def begin_column
  @begin_column
end

#begin_lineObject (readonly)

: Integer?



23
24
25
# File 'lib/rbi/loc.rb', line 23

def begin_line
  @begin_line
end

#end_columnObject (readonly)

: Integer?



23
24
25
# File 'lib/rbi/loc.rb', line 23

def end_column
  @end_column
end

#end_lineObject (readonly)

: Integer?



23
24
25
# File 'lib/rbi/loc.rb', line 23

def end_line
  @end_line
end

#fileObject (readonly)

: String?



20
21
22
# File 'lib/rbi/loc.rb', line 20

def file
  @file
end

Class Method Details

.from_prism(file, prism_location) ⇒ Object

: (String file, Prism::Location prism_location) -> Loc



8
9
10
11
12
13
14
15
16
# File 'lib/rbi/loc.rb', line 8

def from_prism(file, prism_location)
  new(
    file: file,
    begin_line: prism_location.start_line,
    end_line: prism_location.end_line,
    begin_column: prism_location.start_column,
    end_column: prism_location.end_column,
  )
end

Instance Method Details

#sourceObject

: -> String?



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rbi/loc.rb', line 44

def source
  file = self.file
  return unless file
  return 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

#to_sObject

: -> String



35
36
37
38
39
40
41
# File 'lib/rbi/loc.rb', line 35

def to_s
  if end_line && end_column
    "#{file}:#{begin_line}:#{begin_column}-#{end_line}:#{end_column}"
  else
    "#{file}:#{begin_line}:#{begin_column}"
  end
end