Class: Rubydex::Location

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/rubydex/location.rb,
ext/rubydex/location.c

Overview

A zero based internal location. Intended to be used for tool-to-tool communication, such as a language server communicating with an editor.

Direct Known Subclasses

DisplayLocation

Defined Under Namespace

Classes: NotFileUriError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri:, start_line:, end_line:, start_column:, end_column:) ⇒ Location

: (?uri: String, ?start_line: Integer, ?end_line: Integer, ?start_column: Integer, ?end_column: Integer) -> void



18
19
20
21
22
23
24
# File 'lib/rubydex/location.rb', line 18

def initialize(uri:, start_line:, end_line:, start_column:, end_column:)
  @uri = uri
  @start_line = start_line
  @end_line = end_line
  @start_column = start_column
  @end_column = end_column
end

Instance Attribute Details

#end_columnObject (readonly)

: Integer



15
16
17
# File 'lib/rubydex/location.rb', line 15

def end_column
  @end_column
end

#end_lineObject (readonly)

: Integer



15
16
17
# File 'lib/rubydex/location.rb', line 15

def end_line
  @end_line
end

#start_columnObject (readonly)

: Integer



15
16
17
# File 'lib/rubydex/location.rb', line 15

def start_column
  @start_column
end

#start_lineObject (readonly)

: Integer



15
16
17
# File 'lib/rubydex/location.rb', line 15

def start_line
  @start_line
end

#uriObject (readonly)

: String



12
13
14
# File 'lib/rubydex/location.rb', line 12

def uri
  @uri
end

Instance Method Details

#<=>(other) ⇒ Object

: (other: BasicObject) -> Integer



38
39
40
41
42
# File 'lib/rubydex/location.rb', line 38

def <=>(other)
  return -1 unless other.is_a?(Location)

  comparable_values <=> other.comparable_values
end

#comparable_valuesObject

: () -> [String, Integer, Integer, Integer, Integer]



45
46
47
# File 'lib/rubydex/location.rb', line 45

def comparable_values
  [@uri, @start_line, @start_column, @end_line, @end_column]
end

#to_displayObject

Turns this zero based location into a one based location for display purposes.

: () -> DisplayLocation



52
53
54
55
56
57
58
59
60
# File 'lib/rubydex/location.rb', line 52

def to_display
  DisplayLocation.new(
    uri: @uri,
    start_line: @start_line + 1,
    end_line: @end_line + 1,
    start_column: @start_column + 1,
    end_column: @end_column + 1,
  )
end

#to_file_pathObject

: () -> String

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/rubydex/location.rb', line 27

def to_file_path
  uri = URI(@uri)
  raise NotFileUriError, "URI is not a file:// URI: #{@uri}" unless uri.scheme == "file"

  path = uri.path
  # TODO: This has to go away once we have a proper URI abstraction
  path.delete_prefix!("/") if Gem.win_platform?
  path
end

#to_sObject

: -> String



63
64
65
# File 'lib/rubydex/location.rb', line 63

def to_s
  "#{to_file_path}:#{@start_line + 1}:#{@start_column + 1}-#{@end_line + 1}:#{@end_column + 1}"
end