Class: Spoom::Location
- Inherits:
-
Object
- Object
- Spoom::Location
- Includes:
- Comparable
- Defined in:
- lib/spoom/location.rb
Defined Under Namespace
Classes: LocationError
Instance Attribute Summary collapse
-
#end_column ⇒ Object
readonly
: Integer?.
-
#end_line ⇒ Object
readonly
: Integer?.
-
#file ⇒ Object
readonly
: String.
-
#start_column ⇒ Object
readonly
: Integer?.
-
#start_line ⇒ Object
readonly
: Integer?.
Class Method Summary collapse
-
.from_prism(file, location) ⇒ Object
: (String file, Prism::Location location) -> Location.
-
.from_string(location_string) ⇒ Object
: (String location_string) -> Location.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
: (BasicObject other) -> Integer?.
-
#include?(other) ⇒ Boolean
: (Location other) -> bool.
-
#initialize(file, start_line: nil, start_column: nil, end_line: nil, end_column: nil) ⇒ Location
constructor
: (String file, ?start_line: Integer?, ?start_column: Integer?, ?end_line: Integer?, ?end_column: Integer?) -> void.
-
#to_s ⇒ Object
: -> String.
Constructor Details
#initialize(file, start_line: nil, start_column: nil, end_line: nil, end_column: nil) ⇒ Location
: (String file, ?start_line: Integer?, ?start_column: Integer?, ?end_line: Integer?, ?end_column: Integer?) -> void
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/spoom/location.rb', line 61 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_column ⇒ Object (readonly)
: Integer?
58 59 60 |
# File 'lib/spoom/location.rb', line 58 def end_column @end_column end |
#end_line ⇒ Object (readonly)
: Integer?
58 59 60 |
# File 'lib/spoom/location.rb', line 58 def end_line @end_line end |
#file ⇒ Object (readonly)
: String
55 56 57 |
# File 'lib/spoom/location.rb', line 55 def file @file end |
#start_column ⇒ Object (readonly)
: Integer?
58 59 60 |
# File 'lib/spoom/location.rb', line 58 def start_column @start_column end |
#start_line ⇒ Object (readonly)
: Integer?
58 59 60 |
# File 'lib/spoom/location.rb', line 58 def start_line @start_line end |
Class Method Details
.from_prism(file, location) ⇒ Object
: (String file, Prism::Location location) -> Location
43 44 45 46 47 48 49 50 51 |
# File 'lib/spoom/location.rb', line 43 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
: (String location_string) -> Location
12 13 14 15 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 |
# File 'lib/spoom/location.rb', line 12 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
: (BasicObject other) -> Integer?
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/spoom/location.rb', line 95 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
: (Location other) -> bool
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/spoom/location.rb', line 81 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_s ⇒ Object
: -> String
118 119 120 121 122 123 124 125 126 |
# File 'lib/spoom/location.rb', line 118 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 |