Class: Solargraph::Source::Cursor

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(source, position) ⇒ Cursor

Returns a new instance of Cursor.

Parameters:



17
18
19
20
# File 'lib/solargraph/source/cursor.rb', line 17

def initialize source, position
  @source = source
  @position = Position.normalize(position)
end

Instance Attribute Details

#positionPosition (readonly)

Returns:



10
11
12
# File 'lib/solargraph/source/cursor.rb', line 10

def position
  @position
end

#sourceSource (readonly)

Returns:



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.

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/solargraph/source/cursor.rb', line 96

def argument?
  # @argument ||= !signature_position.nil?
  @argument ||= !recipient.nil?
end

#chainChain

Returns:



82
83
84
# File 'lib/solargraph/source/cursor.rb', line 82

def chain
  @chain ||= SourceChainer.chain(source, position)
end

#comment?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/solargraph/source/cursor.rb', line 102

def comment?
  @comment ||= source.comment_at?(position)
end

#end_of_wordString

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

@sg-ignore Need to add nil check here

Returns:

  • (String)


58
59
60
61
62
63
# File 'lib/solargraph/source/cursor.rb', line 58

def end_of_word
  @end_of_word ||= begin
    match = source.code[offset..].to_s.match(end_word_pattern)
    match ? match[0] : ''
  end
end

#filenameString?

Returns:

  • (String, nil)


23
24
25
# File 'lib/solargraph/source/cursor.rb', line 23

def filename
  source.filename
end

#nodeAST::Node

Returns:

  • (AST::Node)


125
126
127
# File 'lib/solargraph/source/cursor.rb', line 125

def node
  @node ||= source.node_at(position.line, position.column)
end

#node_positionPosition

Returns:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/solargraph/source/cursor.rb', line 130

def node_position
  @node_position ||= if start_of_word.empty?
                       # @sg-ignore Need to add nil check here
                       match = source.code[0, offset].match(/\s*(\.|:+)\s*$/)
                       if match
                         # @sg-ignore Need to add nil check here
                         Position.from_offset(source.code, offset - match[0].length)
                       else
                         position
                       end
                     else
                       position
                     end
end

#offsetInteger

Returns:

  • (Integer)


151
152
153
# File 'lib/solargraph/source/cursor.rb', line 151

def offset
  @offset ||= Position.to_offset(source.code, position)
end

#rangeRange

The range of the word at the current position.

Returns:



73
74
75
76
77
78
79
# File 'lib/solargraph/source/cursor.rb', line 73

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

#recipientCursor? Also known as: receiver

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

Returns:



115
116
117
118
119
120
121
# File 'lib/solargraph/source/cursor.rb', line 115

def recipient
  @recipient ||= begin
    node = recipient_node
    # @sg-ignore Need to add nil check here
    node ? Cursor.new(source, Range.from_node(node).ending) : nil
  end
end

#recipient_nodeParser::AST::Node?

Returns:

  • (Parser::AST::Node, nil)


146
147
148
# File 'lib/solargraph/source/cursor.rb', line 146

def recipient_node
  @recipient_node ||= Solargraph::Parser::NodeMethods.find_recipient_node(self)
end

#start_of_constant?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/solargraph/source/cursor.rb', line 66

def start_of_constant?
  source.code[offset - 2, 2] == '::'
end

#start_of_wordString

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 Need to add nil check here

Returns:

  • (String)


40
41
42
43
44
45
46
47
48
49
50
51
# 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
    # @sg-ignore Need to add nil check here
    if source.code[0..(offset - result.length - 1)].end_with?(':') && !source.code[0..(offset - result.length - 1)].end_with?('::')
      result = ":#{result}"
    end
    result
  end
end

#string?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/solargraph/source/cursor.rb', line 107

def string?
  @string ||= source.string_at?(position)
end

#wordString

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

Returns:

  • (String)


31
32
33
# File 'lib/solargraph/source/cursor.rb', line 31

def word
  @word ||= start_of_word + end_of_word
end