Class: Rucoa::Range

Inherits:
Object
  • Object
show all
Defined in:
lib/rucoa/range.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(beginning, ending, including_ending: true) ⇒ Range

Returns a new instance of Range.

Parameters:

  • beginning (Rucoa::Position)
  • ending (Ruoca::Position)
  • including_ending (Boolean) (defaults to: true)


34
35
36
37
38
39
40
41
42
# File 'lib/rucoa/range.rb', line 34

def initialize(
  beginning,
  ending,
  including_ending: true
)
  @beginning = beginning
  @ending = ending
  @including_ending = including_ending
end

Instance Attribute Details

#beginningRucoa::Position (readonly)

Returns:



26
27
28
# File 'lib/rucoa/range.rb', line 26

def beginning
  @beginning
end

#endingRucoa::Position (readonly)

Returns:



29
30
31
# File 'lib/rucoa/range.rb', line 29

def ending
  @ending
end

Class Method Details

.from_parser_range(range) ⇒ Rucoa::Range

Parameters:

  • range (Parser::Source::Range)

Returns:



8
9
10
11
12
13
# File 'lib/rucoa/range.rb', line 8

def from_parser_range(range)
  new(
    Position.from_parser_range_beginning(range),
    Position.from_parser_range_ending(range)
  )
end

.from_vscode_range(hash) ⇒ Rucoa::Range

Parameters:

  • hash (Hash)

Returns:



17
18
19
20
21
22
# File 'lib/rucoa/range.rb', line 17

def from_vscode_range(hash)
  new(
    Position.from_vscode_position(hash['start']),
    Position.from_vscode_position(hash['end'])
  )
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


46
47
48
# File 'lib/rucoa/range.rb', line 46

def ==(other)
  beginning == other.beginning && ending == other.ending
end

#contain?(range) ⇒ Boolean

Examples:

returns true when the range is contained in self

range = Rucoa::Range.new(
  Rucoa::Position.new(
    column: 0,
    line: 0
  ),
  Rucoa::Position.new(
    column: 0,
    line: 2
  )
)
expect(range).to contain(
  Rucoa::Range.new(
    Rucoa::Position.new(
      column: 0,
      line: 0
    ),
    Rucoa::Position.new(
      column: 0,
      line: 0
    )
  )
)

Parameters:

Returns:

  • (Boolean)


75
76
77
# File 'lib/rucoa/range.rb', line 75

def contain?(range)
  include?(range.beginning) && include?(range.ending)
end

#include?(position) ⇒ Boolean

Examples:

returns true when the position is included in self

range = Rucoa::Range.new(
  Rucoa::Position.new(
    column: 0,
    line: 0
  ),
  Rucoa::Position.new(
    column: 0,
    line: 2
  )
)
expect(range).to include(
  Rucoa::Position.new(
    column: 0,
    line: 0
  )
)
expect(range).to include(
  Rucoa::Position.new(
    column: 0,
    line: 1
  )
)
expect(range).to include(
  Rucoa::Position.new(
    column: 1,
    line: 1
  )
)
expect(range).to include(
  Rucoa::Position.new(
    column: 0,
    line: 2
  )
)
expect(range).not_to include(
  Rucoa::Position.new(
    column: 0,
    line: 3
  )
)

Parameters:

Returns:

  • (Boolean)


122
123
124
125
126
127
128
129
130
# File 'lib/rucoa/range.rb', line 122

def include?(position)
  return false if position.line > @ending.line
  return false if position.line < @beginning.line
  return false if position.line == @beginning.line && position.column < @beginning.column
  return false if position.line == @ending.line && position.column > @ending.column
  return false if position.line == @ending.line && position.column == @ending.column && !@including_ending

  true
end

#to_vscode_rangeHash

Returns:

  • (Hash)


133
134
135
136
137
138
# File 'lib/rucoa/range.rb', line 133

def to_vscode_range
  {
    end: @ending.to_vscode_position,
    start: @beginning.to_vscode_position
  }
end