Class: Solargraph::Position

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

Overview

The zero-based line and column numbers of a position in a string.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, character) ⇒ Position

Returns a new instance of Position.

Parameters:

  • line (Integer)
  • character (Integer)


15
16
17
18
# File 'lib/solargraph/position.rb', line 15

def initialize line, character
  @line = line
  @character = character
end

Instance Attribute Details

#characterInteger (readonly) Also known as: column

Returns:

  • (Integer)


9
10
11
# File 'lib/solargraph/position.rb', line 9

def character
  @character
end

#lineInteger (readonly)

Returns:

  • (Integer)


6
7
8
# File 'lib/solargraph/position.rb', line 6

def line
  @line
end

Class Method Details

.from_offset(text, offset) ⇒ Position

Get a position for the specified text and offset.

Parameters:

  • text (String)
  • offset (Integer)

Returns:

Raises:



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

def self.from_offset text, offset
  cursor = 0
  line = 0
  character = nil
  text.lines.each do |l|
    line_length = l.length
    char_length = l.chomp.length
    if cursor + char_length >= offset
      character = offset - cursor
      break
    end
    cursor += line_length
    line += 1
  end
  character = 0 if character.nil? and offset == cursor
  raise InvalidOffsetError if character.nil?
  Position.new(line, character)
end

.line_char_to_offset(text, line, character) ⇒ Integer

Get a numeric offset for the specified text and a position identified by its line and character.

Parameters:

  • text (String)
  • line (Integer)
  • character (Integer)

Returns:

  • (Integer)


65
66
67
# File 'lib/solargraph/position.rb', line 65

def self.line_char_to_offset text, line, character
  to_offset(text, Position.new(line, character))
end

.normalize(object) ⇒ Position

A helper method for generating positions from arrays of integers. The original parameter is returned if it is already a position.

Parameters:

  • object (Position, Array(Integer, Integer))

Returns:

Raises:

  • (ArgumentError)

    if the object cannot be converted to a position.



100
101
102
103
104
# File 'lib/solargraph/position.rb', line 100

def self.normalize object
  return object if object.is_a?(Position)
  return Position.new(object[0], object[1]) if object.is_a?(Array)
  raise ArgumentError, "Unable to convert #{object.class} to Position"
end

.to_offset(text, position) ⇒ Integer

Get a numeric offset for the specified text and position.

Parameters:

Returns:

  • (Integer)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/solargraph/position.rb', line 40

def self.to_offset text, position
  result = 0
  feed = 0
  line = position.line
  column = position.character
  text.lines.each do |l|
    line_length = l.length
    char_length = l.chomp.length
    if feed == line
      result += column
      break
    end
    result += line_length
    feed += 1
  end
  result
end

Instance Method Details

#==(other) ⇒ Object



106
107
108
109
# File 'lib/solargraph/position.rb', line 106

def == other
  return false unless other.is_a?(Position)
  line == other.line and character == other.character
end

#inspectObject



31
32
33
# File 'lib/solargraph/position.rb', line 31

def inspect
  "<Position #{line}, #{character}>"
end

#to_hashHash

Get a hash of the position. This representation is suitable for use in the language server protocol.

Returns:

  • (Hash)


24
25
26
27
28
29
# File 'lib/solargraph/position.rb', line 24

def to_hash
  {
    line: line,
    character: character
  }
end