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
1-indexed column number.
Class Method Summary collapse
- .first_column ⇒ Object
- .first_row ⇒ Object
- .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 1-indexed column number.
8 9 10 |
# File 'lib/yoda/parsing/location.rb', line 8 def row @row end |
Class Method Details
.first_column ⇒ Object
24 25 26 |
# File 'lib/yoda/parsing/location.rb', line 24 def self.first_column 0 end |
.first_row ⇒ Object
20 21 22 |
# File 'lib/yoda/parsing/location.rb', line 20 def self.first_row 1 end |
.of_ast_location(ast_location) ⇒ Location?
31 32 33 34 |
# File 'lib/yoda/parsing/location.rb', line 31 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
39 40 41 |
# File 'lib/yoda/parsing/location.rb', line 39 def self.of_language_server_protocol_position(line:, character:) new(row: line + 1, column: character) end |
.valid_location?(location) ⇒ Boolean
44 45 46 47 48 |
# File 'lib/yoda/parsing/location.rb', line 44 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
103 104 105 106 107 |
# File 'lib/yoda/parsing/location.rb', line 103 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
67 68 69 70 |
# File 'lib/yoda/parsing/location.rb', line 67 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
73 74 75 76 |
# File 'lib/yoda/parsing/location.rb', line 73 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
56 57 58 59 |
# File 'lib/yoda/parsing/location.rb', line 56 def included?(location) return false unless self.class.valid_location?(location) after_begin(location) && before_last(location) end |
#index_of(source) ⇒ Object
51 52 53 |
# File 'lib/yoda/parsing/location.rb', line 51 def index_of(source) (source.split("\n").slice(0, row - 1) || []).map(&:length).reduce(0, &:+) + column end |
#later_than?(location) ⇒ Boolean
62 63 64 |
# File 'lib/yoda/parsing/location.rb', line 62 def later_than?(location) move(row: 0, column: -1).after_begin(location) end |
#move(row:, column:) ⇒ Location
88 89 90 |
# File 'lib/yoda/parsing/location.rb', line 88 def move(row:, column:) self.class.new(row: @row + row, column: @column + column) end |
#offset_from_begin(location) ⇒ {Symbol => Numerical}
80 81 82 83 |
# File 'lib/yoda/parsing/location.rb', line 80 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}
93 94 95 |
# File 'lib/yoda/parsing/location.rb', line 93 def to_language_server_protocol_range { line: row - 1, character: column } end |
#to_s ⇒ Object
97 98 99 |
# File 'lib/yoda/parsing/location.rb', line 97 def to_s "(#{row}, #{column})" end |