Class: Solargraph::Source::Cursor
- Inherits:
-
Object
- Object
- Solargraph::Source::Cursor
- Defined in:
- lib/solargraph/source/cursor.rb
Overview
Information about a single 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
True if the statement at the cursor is an argument to a previous method.
- #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 ⇒ AST::Node
- #node_position ⇒ Position
- #offset ⇒ Integer
-
#range ⇒ Range
The range of the word at the current position.
-
#recipient ⇒ Cursor?
(also: #receiver)
Get a cursor pointing to the method that receives the current statement as an argument.
- #recipient_node ⇒ Parser::AST::Node?
- #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)
10 11 12 |
# File 'lib/solargraph/source/cursor.rb', line 10 def position @position end |
#source ⇒ Source (readonly)
13 14 15 |
# File 'lib/solargraph/source/cursor.rb', line 13 def source @source end |
Instance Method Details
#argument? ⇒ Boolean
True if the statement at the cursor is an argument to a previous method.
Given the code ‘process(foo)`, a cursor pointing at `foo` would identify it as an argument being passed to the `process` method.
If #argument? is true, the #recipient method will return a cursor that points to the method receiving the argument.
92 93 94 95 |
# File 'lib/solargraph/source/cursor.rb', line 92 def argument? # @argument ||= !signature_position.nil? @argument ||= !recipient.nil? end |
#chain ⇒ Chain
78 79 80 |
# File 'lib/solargraph/source/cursor.rb', line 78 def chain @chain ||= SourceChainer.chain(source, position) end |
#comment? ⇒ Boolean
98 99 100 |
# File 'lib/solargraph/source/cursor.rb', line 98 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`.
54 55 56 57 58 59 |
# File 'lib/solargraph/source/cursor.rb', line 54 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
23 24 25 |
# File 'lib/solargraph/source/cursor.rb', line 23 def filename source.filename end |
#node ⇒ AST::Node
120 121 122 |
# File 'lib/solargraph/source/cursor.rb', line 120 def node @node ||= source.node_at(position.line, position.column) end |
#node_position ⇒ Position
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/solargraph/source/cursor.rb', line 125 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 |
#offset ⇒ Integer
146 147 148 |
# File 'lib/solargraph/source/cursor.rb', line 146 def offset @offset ||= Position.to_offset(source.code, position) end |
#range ⇒ Range
The range of the word at the current position.
69 70 71 72 73 74 75 |
# File 'lib/solargraph/source/cursor.rb', line 69 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? Also known as: receiver
Get a cursor pointing to the method that receives the current statement as an argument.
111 112 113 114 115 116 |
# File 'lib/solargraph/source/cursor.rb', line 111 def recipient @recipient ||= begin node = recipient_node node ? Cursor.new(source, Range.from_node(node).ending) : nil end end |
#recipient_node ⇒ Parser::AST::Node?
141 142 143 |
# File 'lib/solargraph/source/cursor.rb', line 141 def recipient_node @recipient_node ||= Solargraph::Parser::NodeMethods.find_recipient_node(self) end |
#start_of_constant? ⇒ Boolean
62 63 64 |
# File 'lib/solargraph/source/cursor.rb', line 62 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`.
@sg-ignore Improve resolution of String#match below
40 41 42 43 44 45 46 47 48 |
# File 'lib/solargraph/source/cursor.rb', line 40 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
103 104 105 |
# File 'lib/solargraph/source/cursor.rb', line 103 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`.
31 32 33 |
# File 'lib/solargraph/source/cursor.rb', line 31 def word @word ||= start_of_word + end_of_word end |