Class: RBI::Loc

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
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

Returns a new instance of Loc.



38
39
40
41
42
43
44
# File 'lib/rbi/loc.rb', line 38

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)

Returns the value of attribute begin_column.



27
28
29
# File 'lib/rbi/loc.rb', line 27

def begin_column
  @begin_column
end

#begin_lineObject (readonly)

Returns the value of attribute begin_line.



27
28
29
# File 'lib/rbi/loc.rb', line 27

def begin_line
  @begin_line
end

#end_columnObject (readonly)

Returns the value of attribute end_column.



27
28
29
# File 'lib/rbi/loc.rb', line 27

def end_column
  @end_column
end

#end_lineObject (readonly)

Returns the value of attribute end_line.



27
28
29
# File 'lib/rbi/loc.rb', line 27

def end_line
  @end_line
end

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

Class Method Details

.from_prism(file, prism_location) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/rbi/loc.rb', line 12

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



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rbi/loc.rb', line 56

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



47
48
49
50
51
52
53
# File 'lib/rbi/loc.rb', line 47

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