Class: Solargraph::Source::Cursor
- Inherits:
-
Object
- Object
- Solargraph::Source::Cursor
- Defined in:
- lib/solargraph/source/cursor.rb
Overview
Information about a position in a source, including the word located there.
Instance Attribute Summary collapse
- #position ⇒ Position readonly
- #source ⇒ Source readonly
Instance Method Summary collapse
- #argument? ⇒ Boolean
- #chain ⇒ Chain
- #comment? ⇒ Boolean
-
#end_of_word ⇒ String
The part of the word after the current position.
- #filename ⇒ String
-
#initialize(source, position) ⇒ Cursor
constructor
A new instance of Cursor.
- #node_position ⇒ Object
-
#range ⇒ Range
The range of the word at the current position.
- #recipient ⇒ Cursor?
- #start_of_constant? ⇒ Boolean
-
#start_of_word ⇒ String
The part of the word before the current position.
- #string? ⇒ Boolean
-
#word ⇒ String
The whole word at the current position.
Constructor Details
Instance Attribute Details
#position ⇒ Position (readonly)
8 9 10 |
# File 'lib/solargraph/source/cursor.rb', line 8 def position @position end |
#source ⇒ Source (readonly)
11 12 13 |
# File 'lib/solargraph/source/cursor.rb', line 11 def source @source end |
Instance Method Details
#argument? ⇒ Boolean
79 80 81 |
# File 'lib/solargraph/source/cursor.rb', line 79 def argument? @argument ||= !signature_position.nil? end |
#chain ⇒ Chain
74 75 76 |
# File 'lib/solargraph/source/cursor.rb', line 74 def chain @chain ||= SourceChainer.chain(source, position) end |
#comment? ⇒ Boolean
84 85 86 |
# File 'lib/solargraph/source/cursor.rb', line 84 def comment? @comment ||= source.comment_at?(position) end |
#end_of_word ⇒ String
The part of the word after the current position. Given the text ‘foo.bar`, the end_of_word at position (0,6) is `r`.
51 52 53 54 55 56 |
# File 'lib/solargraph/source/cursor.rb', line 51 def end_of_word @end_of_word ||= begin match = source.code[offset..-1].to_s.match(end_word_pattern) match ? match[0] : '' end end |
#filename ⇒ String
21 22 23 |
# File 'lib/solargraph/source/cursor.rb', line 21 def filename source.filename end |
#node_position ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/solargraph/source/cursor.rb', line 99 def node_position @node_position ||= begin if start_of_word.empty? match = source.code[0, offset].match(/[\s]*(\.|:+)[\s]*$/) if match Position.from_offset(source.code, offset - match[0].length) else position end else position end end end |
#range ⇒ Range
The range of the word at the current position.
65 66 67 68 69 70 71 |
# File 'lib/solargraph/source/cursor.rb', line 65 def range @range ||= begin s = Position.from_offset(source.code, offset - start_of_word.length) e = Position.from_offset(source.code, offset + end_of_word.length) Solargraph::Range.new(s, e) end end |
#recipient ⇒ Cursor?
94 95 96 97 |
# File 'lib/solargraph/source/cursor.rb', line 94 def recipient return nil unless argument? @recipient ||= Cursor.new(source, signature_position) end |
#start_of_constant? ⇒ Boolean
58 59 60 |
# File 'lib/solargraph/source/cursor.rb', line 58 def start_of_constant? source.code[offset-2, 2] == '::' end |
#start_of_word ⇒ String
The part of the word before the current position. Given the text ‘foo.bar`, the start_of_word at position(0, 6) is `ba`.
37 38 39 40 41 42 43 44 45 |
# File 'lib/solargraph/source/cursor.rb', line 37 def start_of_word @start_of_word ||= begin match = source.code[0..offset-1].to_s.match(start_word_pattern) result = (match ? match[0] : '') # Including the preceding colon if the word appears to be a symbol result = ":#{result}" if source.code[0..offset-result.length-1].end_with?(':') and !source.code[0..offset-result.length-1].end_with?('::') result end end |
#string? ⇒ Boolean
89 90 91 |
# File 'lib/solargraph/source/cursor.rb', line 89 def string? @string ||= source.string_at?(position) end |
#word ⇒ String
The whole word at the current position. Given the text ‘foo.bar`, the word at position(0,6) is `bar`.
29 30 31 |
# File 'lib/solargraph/source/cursor.rb', line 29 def word @word ||= start_of_word + end_of_word end |