Class: SourceMap::Offset

Inherits:
Object show all
Includes:
Comparable
Defined in:
opal/stdlib/source_map/offset.rb

Overview

Public: Offset is an immutable structure representing a position in a source file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, column) ⇒ Offset

Public: Initialize an Offset.

line - Integer line number column - Integer column number



25
26
27
# File 'opal/stdlib/source_map/offset.rb', line 25

def initialize(line, column)
  @line, @column = line, column
end

Instance Attribute Details

#columnObject (readonly)

Public: Get Integer column of offset



33
34
35
# File 'opal/stdlib/source_map/offset.rb', line 33

def column
  @column
end

#lineObject (readonly)

Public: Gets Integer line of offset



30
31
32
# File 'opal/stdlib/source_map/offset.rb', line 30

def line
  @line
end

Class Method Details

.new(*args) ⇒ Object

Public: Construct Offset value.

Returns Offset instance.



10
11
12
13
14
15
16
17
18
19
# File 'opal/stdlib/source_map/offset.rb', line 10

def self.new(*args)
  case args.first
  when Offset
    args.first
  when Array
    super(*args.first)
  else
    super(*args)
  end
end

Instance Method Details

#+(other) ⇒ Object

Public: Shift the offset by some value.

other - An Offset to add by its line and column Or an Integer to add by line

Returns a new Offset instance.



41
42
43
44
45
46
47
48
49
50
# File 'opal/stdlib/source_map/offset.rb', line 41

def +(other)
  case other
  when Offset
    Offset.new(self.line + other.line, self.column + other.column)
  when Integer
    Offset.new(self.line + other, self.column)
  else
    raise ArgumentError, "can't convert #{other} into #{self.class}"
  end
end

#<=>(other) ⇒ Object

Public: Compare Offset to another.

Useful for determining if a position in a few is between two offsets.

other - Another Offset

Returns a negative number when other is smaller and a positive number when its greater. Implements the Comparable#<=> protocol.



60
61
62
63
64
65
66
67
68
# File 'opal/stdlib/source_map/offset.rb', line 60

def <=>(other)
  case other
  when Offset
    diff = self.line - other.line
    diff.zero? ? self.column - other.column : diff
  else
    raise ArgumentError, "can't convert #{other.class} into #{self.class}"
  end
end

#inspectObject

Public: Get a pretty inspect output for debugging purposes.

Returns a String.



84
85
86
# File 'opal/stdlib/source_map/offset.rb', line 84

def inspect
  "#<#{self.class} line=#{line}, column=#{column}>"
end

#to_sObject

Public: Get a simple string representation of the offset

Returns a String.



73
74
75
76
77
78
79
# File 'opal/stdlib/source_map/offset.rb', line 73

def to_s
  if column == 0
    "#{line}"
  else
    "#{line}:#{column}"
  end
end