class Solargraph::Source::Cursor

Information about a position in a source, including the word located there.

Attributes

position[R]

@return [Position]

source[R]

@return [Source]

Public Class Methods

new(source, position) click to toggle source

@param source [Source] @param position [Position, Array(Integer, Integer)]

# File lib/solargraph/source/cursor.rb, line 16
def initialize source, position
  @source = source
  @position = Position.normalize(position)
end

Public Instance Methods

argument?() click to toggle source

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.

@return [Boolean]

# File lib/solargraph/source/cursor.rb, line 90
def argument?
  # @argument ||= !signature_position.nil?
  @argument ||= !recipient.nil?
end
chain() click to toggle source

@return [Chain]

# File lib/solargraph/source/cursor.rb, line 76
def chain
  @chain ||= SourceChainer.chain(source, position)
end
comment?() click to toggle source

@return [Boolean]

# File lib/solargraph/source/cursor.rb, line 96
def comment?
  @comment ||= source.comment_at?(position)
end
end_of_word() click to toggle source

The part of the word after the current position. Given the text `foo.bar`, the #end_of_word at position (0,6) is `r`.

@return [String]

# File lib/solargraph/source/cursor.rb, line 52
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() click to toggle source

@return [String]

# File lib/solargraph/source/cursor.rb, line 22
def filename
  source.filename
end
node() click to toggle source
# File lib/solargraph/source/cursor.rb, line 117
def node
  @node ||= source.node_at(position.line, position.column)
end
node_position() click to toggle source

@return [Position]

# File lib/solargraph/source/cursor.rb, line 122
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() click to toggle source

@return [Integer]

# File lib/solargraph/source/cursor.rb, line 142
def offset
  @offset ||= Position.to_offset(source.code, position)
end
range() click to toggle source

The range of the word at the current position.

@return [Range]

# File lib/solargraph/source/cursor.rb, line 67
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
receiver()
Alias for: recipient
recipient() click to toggle source

Get a cursor pointing to the method that receives the current statement as an argument.

@return [Cursor, nil]

# File lib/solargraph/source/cursor.rb, line 109
def recipient
  @recipient ||= begin
    node = recipient_node
    node ? Cursor.new(source, Range.from_node(node).ending) : nil
  end
end
Also aliased as: receiver
recipient_node() click to toggle source
# File lib/solargraph/source/cursor.rb, line 137
def recipient_node
  @recipient_node ||= Solargraph::Parser::NodeMethods.find_recipient_node(self)
end
start_of_constant?() click to toggle source

@return [Boolean]

# File lib/solargraph/source/cursor.rb, line 60
def start_of_constant?
  source.code[offset-2, 2] == '::'
end
start_of_word() click to toggle source

The part of the word before the current position. Given the text `foo.bar`, the #start_of_word at position(0, 6) is `ba`.

@return [String]

# File lib/solargraph/source/cursor.rb, line 38
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?() click to toggle source

@return [Boolean]

# File lib/solargraph/source/cursor.rb, line 101
def string?
  @string ||= source.string_at?(position)
end
word() click to toggle source

The whole word at the current position. Given the text `foo.bar`, the word at position(0,6) is `bar`.

@return [String]

# File lib/solargraph/source/cursor.rb, line 30
def word
  @word ||= start_of_word + end_of_word
end

Private Instance Methods

end_word_pattern() click to toggle source

A regular expression to find the end of a word from an offset.

@return [Regexp]

# File lib/solargraph/source/cursor.rb, line 158
def end_word_pattern
  /^([a-z0-9_]|[^\u0000-\u007F])*[\?\!]?/i
end
start_word_pattern() click to toggle source

A regular expression to find the start of a word from an offset.

@return [Regexp]

# File lib/solargraph/source/cursor.rb, line 151
def start_word_pattern
  /(@{1,2}|\$)?([a-z0-9_]|[^\u0000-\u007F])*\z/i
end