Class: Spoom::Location

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Comparable
Defined in:
lib/spoom/location.rb

Defined Under Namespace

Classes: LocationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, start_line: nil, start_column: nil, end_line: nil, end_column: nil) ⇒ Location

Returns a new instance of Location.

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/spoom/location.rb', line 73

def initialize(file, start_line: nil, start_column: nil, end_line: nil, end_column: nil)
  raise LocationError,
    "Invalid location: end line is required if start line is provided" if start_line && !end_line
  raise LocationError,
    "Invalid location: start line is required if end line is provided" if !start_line && end_line
  raise LocationError,
    "Invalid location: end column is required if start column is provided" if start_column && !end_column
  raise LocationError,
    "Invalid location: start column is required if end column is provided" if !start_column && end_column
  raise LocationError,
    "Invalid location: lines are required if columns are provided" if start_column && !start_line

  @file = file
  @start_line = start_line
  @start_column = start_column
  @end_line = end_line
  @end_column = end_column
end

Instance Attribute Details

#end_columnObject (readonly)

Returns the value of attribute end_column.



62
63
64
# File 'lib/spoom/location.rb', line 62

def end_column
  @end_column
end

#end_lineObject (readonly)

Returns the value of attribute end_line.



62
63
64
# File 'lib/spoom/location.rb', line 62

def end_line
  @end_line
end

#fileObject (readonly)

Returns the value of attribute file.



59
60
61
# File 'lib/spoom/location.rb', line 59

def file
  @file
end

#start_columnObject (readonly)

Returns the value of attribute start_column.



62
63
64
# File 'lib/spoom/location.rb', line 62

def start_column
  @start_column
end

#start_lineObject (readonly)

Returns the value of attribute start_line.



62
63
64
# File 'lib/spoom/location.rb', line 62

def start_line
  @start_line
end

Class Method Details

.from_prism(file, location) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/spoom/location.rb', line 47

def from_prism(file, location)
  new(
    file,
    start_line: location.start_line,
    start_column: location.start_column,
    end_line: location.end_line,
    end_column: location.end_column,
  )
end

.from_string(location_string) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spoom/location.rb', line 16

def from_string(location_string)
  file, rest = location_string.split(":", 2)
  raise LocationError, "Invalid location string `#{location_string}`: missing file name" unless file

  return new(file) if rest.nil?

  start_line, rest = rest.split(":", 2)
  if rest.nil?
    start_line, end_line = T.must(start_line).split("-", 2)
    raise LocationError, "Invalid location string `#{location_string}`: missing end line" unless end_line

    return new(file, start_line: start_line.to_i, end_line: end_line.to_i) if end_line
  end

  start_column, rest = rest.split("-", 2)
  raise LocationError, "Invalid location string `#{location_string}`: missing end line and column" if rest.nil?

  end_line, end_column = rest.split(":", 2)
  raise LocationError,
    "Invalid location string `#{location_string}`: missing end column" unless end_line && end_column

  new(
    file,
    start_line: start_line.to_i,
    start_column: start_column.to_i,
    end_line: end_line.to_i,
    end_column: end_column.to_i,
  )
end

Instance Method Details

#<=>(other) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/spoom/location.rb', line 106

def <=>(other)
  return unless Location === other

  comparison_array_self = [
    @file,
    @start_line || -Float::INFINITY,
    @start_column || -Float::INFINITY,
    @end_line || Float::INFINITY,
    @end_column || Float::INFINITY,
  ]

  comparison_array_other = [
    other.file,
    other.start_line || -Float::INFINITY,
    other.start_column || -Float::INFINITY,
    other.end_line || Float::INFINITY,
    other.end_column || Float::INFINITY,
  ]

  comparison_array_self <=> comparison_array_other
end

#include?(other) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
99
100
101
102
103
# File 'lib/spoom/location.rb', line 93

def include?(other)
  return false unless @file == other.file
  return false if (@start_line || -Float::INFINITY) > (other.start_line || -Float::INFINITY)
  return false if @start_line == other.start_line &&
    (@start_column || -Float::INFINITY) > (other.start_column || -Float::INFINITY)
  return false if (@end_line || Float::INFINITY) < (other.end_line || Float::INFINITY)
  return false if @end_line == other.end_line &&
    (@end_column || Float::INFINITY) < (other.end_column || Float::INFINITY)

  true
end

#to_sObject



129
130
131
132
133
134
135
136
137
# File 'lib/spoom/location.rb', line 129

def to_s
  if @start_line && @start_column
    "#{@file}:#{@start_line}:#{@start_column}-#{@end_line}:#{@end_column}"
  elsif @start_line
    "#{@file}:#{@start_line}-#{@end_line}"
  else
    @file
  end
end