Class: Rucoa::Position

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column: 0, line: 1) ⇒ Position

Returns a new instance of Position.

Parameters:

  • column (Integer) (defaults to: 0)

    0-origin column number

  • line (Integer) (defaults to: 1)

    1-origin line number



42
43
44
45
46
47
48
# File 'lib/rucoa/position.rb', line 42

def initialize(
  column: 0,
  line: 1
)
  @column = column
  @line = line
end

Instance Attribute Details

#columnInteger (readonly)

Returns:

  • (Integer)


35
36
37
# File 'lib/rucoa/position.rb', line 35

def column
  @column
end

#lineInteger (readonly)

Returns:

  • (Integer)


38
39
40
# File 'lib/rucoa/position.rb', line 38

def line
  @line
end

Class Method Details

.from_parser_range_beginning(range) ⇒ Rucoa::Position

Parameters:

  • range (Parser::Source::Range)

Returns:



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

def from_parser_range_beginning(range)
  new(
    column: range.column,
    line: range.line
  )
end

.from_parser_range_ending(range) ⇒ Rucoa::Position

Parameters:

  • range (Parser::Source::Range)

Returns:



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

def from_parser_range_ending(range)
  new(
    column: range.last_column,
    line: range.last_line
  )
end

.from_vscode_position(hash) ⇒ Rucoa::Position

Parameters:

  • hash (Hash{String => Integer})

Returns:



26
27
28
29
30
31
# File 'lib/rucoa/position.rb', line 26

def from_vscode_position(hash)
  new(
    column: hash['character'],
    line: hash['line'] + 1
  )
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


52
53
54
# File 'lib/rucoa/position.rb', line 52

def ==(other)
  column == other.column && line == other.line
end

#to_index_of(text) ⇒ Integer

Parameters:

  • text (String)

Returns:

  • (Integer)


58
59
60
# File 'lib/rucoa/position.rb', line 58

def to_index_of(text)
  text.each_line.take(@line - 1).sum(&:length) + @column
end

#to_rangeRucoa::Range

Returns:



63
64
65
# File 'lib/rucoa/position.rb', line 63

def to_range
  Range.new(self, self)
end

#to_vscode_positionHash

Returns:

  • (Hash)


68
69
70
71
72
73
# File 'lib/rucoa/position.rb', line 68

def to_vscode_position
  {
    'character' => @column,
    'line' => @line - 1
  }
end