Class: Solargraph::Source::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/source/cursor.rb

Overview

Information about a 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:



15
16
17
18
# File 'lib/solargraph/source/cursor.rb', line 15

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

Instance Attribute Details

#positionPosition (readonly)

Returns:



8
9
10
# File 'lib/solargraph/source/cursor.rb', line 8

def position
  @position
end

#sourceSource (readonly)

Returns:



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

def source
  @source
end

Instance Method Details

#argument?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/solargraph/source/cursor.rb', line 79

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

#chainChain

Returns:



74
75
76
# File 'lib/solargraph/source/cursor.rb', line 74

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

#comment?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/solargraph/source/cursor.rb', line 84

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`.

Returns:

  • (String)


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

#filenameString

Returns:

  • (String)


21
22
23
# File 'lib/solargraph/source/cursor.rb', line 21

def filename
  source.filename
end

#node_positionObject



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

#rangeRange

The range of the word at the current position.

Returns:



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

#recipientCursor?

Returns:



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

Returns:

  • (Boolean)


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

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`.

Returns:

  • (String)


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

Returns:

  • (Boolean)


89
90
91
# File 'lib/solargraph/source/cursor.rb', line 89

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)


29
30
31
# File 'lib/solargraph/source/cursor.rb', line 29

def word
  @word ||= start_of_word + end_of_word
end