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

Class Method 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



31
32
33
34
35
36
37
# File 'lib/rubydex/location.rb', line 31

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

Class Method Details

.from_prism(prism_location, uri:) ⇒ Object

: (Prism::Location prism_location, uri: String) -> Location



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

def from_prism(prism_location, uri:)
  Location.new(
    uri: uri,
    start_line: prism_location.start_line - 1,
    start_column: prism_location.start_column,
    end_line: prism_location.end_line - 1,
    end_column: prism_location.end_column,
  )
end

Instance Method Details

#<=>(other) ⇒ Object

: (other: BasicObject) -> Integer



51
52
53
54
55
# File 'lib/rubydex/location.rb', line 51

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

  comparable_values <=> other.comparable_values
end

#comparable_valuesObject

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



58
59
60
# File 'lib/rubydex/location.rb', line 58

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



65
66
67
68
69
70
71
72
73
# File 'lib/rubydex/location.rb', line 65

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:



40
41
42
43
44
45
46
47
48
# File 'lib/rubydex/location.rb', line 40

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



76
77
78
# File 'lib/rubydex/location.rb', line 76

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