Class: Yoda::Parsing::Location
- Inherits:
-
Object
- Object
- Yoda::Parsing::Location
- Includes:
- Comparable
- Defined in:
- lib/yoda/parsing/location.rb
Instance Attribute Summary collapse
-
#column ⇒ Integer
readonly
0-indexed column number.
-
#row ⇒ Integer
readonly
0-indexed column number.
Class Method Summary collapse
- .of_ast_location(ast_location) ⇒ Location?
- .of_language_server_protocol_position(line:, character:) ⇒ Location
- .valid_location?(location) ⇒ Boolean
Instance Method Summary collapse
- #<=>(another) ⇒ Integer
- #after_begin(location) ⇒ Object
- #before_last(location) ⇒ Object
- #included?(location) ⇒ Boolean
- #index_of(source) ⇒ Object
-
#initialize(row:, column:) ⇒ Location
constructor
A new instance of Location.
- #later_than?(location) ⇒ Boolean
- #move(row:, column:) ⇒ Location
- #offset_from_begin(location) ⇒ {Symbol => Numerical}
- #to_language_server_protocol_range ⇒ {Symbol => Integer}
- #to_s ⇒ Object
Constructor Details
#initialize(row:, column:) ⇒ Location
Returns a new instance of Location.
15 16 17 18 |
# File 'lib/yoda/parsing/location.rb', line 15 def initialize(row:, column:) @row = row @column = column end |
Instance Attribute Details
#column ⇒ Integer (readonly)
Returns 0-indexed column number.
11 12 13 |
# File 'lib/yoda/parsing/location.rb', line 11 def column @column end |
#row ⇒ Integer (readonly)
TODO:
Make this 0-indexed.
Returns 0-indexed column number.
8 9 10 |
# File 'lib/yoda/parsing/location.rb', line 8 def row @row end |
Class Method Details
.of_ast_location(ast_location) ⇒ Location?
22 23 24 25 |
# File 'lib/yoda/parsing/location.rb', line 22 def self.of_ast_location(ast_location) return nil unless valid_location?(ast_location) Location.new(row: ast_location.line, column: ast_location.column) end |
.of_language_server_protocol_position(line:, character:) ⇒ Location
30 31 32 |
# File 'lib/yoda/parsing/location.rb', line 30 def self.of_language_server_protocol_position(line:, character:) new(row: line + 1, column: character) end |
.valid_location?(location) ⇒ Boolean
35 36 37 38 39 |
# File 'lib/yoda/parsing/location.rb', line 35 def self.valid_location?(location) return false if !location.is_a?(::Parser::Source::Range) && !location.is_a?(::Parser::Source::Map) return false if location.is_a?(::Parser::Source::Map) && !location.expression true end |
Instance Method Details
#<=>(another) ⇒ Integer
94 95 96 97 98 |
# File 'lib/yoda/parsing/location.rb', line 94 def <=>(another) return 0 if row == another.row && column == another.column return 1 if (row == another.row && column >= another.column) || row > another.row -1 end |
#after_begin(location) ⇒ Object
58 59 60 61 |
# File 'lib/yoda/parsing/location.rb', line 58 def after_begin(location) return false unless self.class.valid_location?(location) (location.line == row && location.column <= column) || location.line < row end |
#before_last(location) ⇒ Object
64 65 66 67 |
# File 'lib/yoda/parsing/location.rb', line 64 def before_last(location) return false unless self.class.valid_location?(location) (location.last_line == row && column <= location.last_column ) || row < location.last_line end |
#included?(location) ⇒ Boolean
47 48 49 50 |
# File 'lib/yoda/parsing/location.rb', line 47 def included?(location) return false unless self.class.valid_location?(location) after_begin(location) && before_last(location) end |
#index_of(source) ⇒ Object
42 43 44 |
# File 'lib/yoda/parsing/location.rb', line 42 def index_of(source) (source.split("\n").slice(0, row - 1) || []).map(&:length).reduce(0, &:+) + column end |
#later_than?(location) ⇒ Boolean
53 54 55 |
# File 'lib/yoda/parsing/location.rb', line 53 def later_than?(location) move(row: 0, column: -1).after_begin(location) end |
#move(row:, column:) ⇒ Location
79 80 81 |
# File 'lib/yoda/parsing/location.rb', line 79 def move(row:, column:) self.class.new(row: @row + row, column: @column + column) end |
#offset_from_begin(location) ⇒ {Symbol => Numerical}
71 72 73 74 |
# File 'lib/yoda/parsing/location.rb', line 71 def offset_from_begin(location) fail ArgumentError, location unless self.class.valid_location?(location) { line: row - location.line, column: column - location.column } end |
#to_language_server_protocol_range ⇒ {Symbol => Integer}
84 85 86 |
# File 'lib/yoda/parsing/location.rb', line 84 def to_language_server_protocol_range { line: row - 1, character: column } end |
#to_s ⇒ Object
88 89 90 |
# File 'lib/yoda/parsing/location.rb', line 88 def to_s "(#{row}, #{column})" end |