Class: Rsense::Server::Listeners::FindDefinitionEventListener

Inherits:
Object
  • Object
show all
Includes:
Java::org.cx4a.rsense::Project::EventListener
Defined in:
lib/rsense/server/listeners/find_definition_event_listener.rb

Constant Summary collapse

EventType =
Rsense::Typing::Graph::EventListener::EventType
SourceLocation =
Rsense::Util::SourceLocation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ FindDefinitionEventListener

Returns a new instance of FindDefinitionEventListener.



19
20
21
22
23
# File 'lib/rsense/server/listeners/find_definition_event_listener.rb', line 19

def initialize(context)
  @context = context
  @@counter = 0
  @locations = Set.new
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



17
18
19
# File 'lib/rsense/server/listeners/find_definition_event_listener.rb', line 17

def context
  @context
end

#locationsObject

Returns the value of attribute locations.



17
18
19
# File 'lib/rsense/server/listeners/find_definition_event_listener.rb', line 17

def locations
  @locations
end

#prefixObject

Returns the value of attribute prefix.



17
18
19
# File 'lib/rsense/server/listeners/find_definition_event_listener.rb', line 17

def prefix
  @prefix
end

Class Method Details

.counterObject



30
31
32
33
34
# File 'lib/rsense/server/listeners/find_definition_event_listener.rb', line 30

def self.counter
  old = @@counter
  @@counter += 1
  return old
end

Instance Method Details

#check_vertex(vertex) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/rsense/server/listeners/find_definition_event_listener.rb', line 83

def check_vertex(vertex)
  if @context.main && event.type == EventType::METHOD_MISSING
    if vertex && vertex.getName().startsWith(@prefix) && vertex.getReceiverVertex()
      return true
    end
  end
end

#getLocationsObject



36
37
38
# File 'lib/rsense/server/listeners/find_definition_event_listener.rb', line 36

def getLocations
  @locations
end

#setupObject



25
26
27
28
# File 'lib/rsense/server/listeners/find_definition_event_listener.rb', line 25

def setup
  @locations.clear
  @prefix = Rsense::Server::Command::FIND_DEFINITION_METHOD_NAME_PREFIX + @@counter
end

#update(event) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rsense/server/listeners/find_definition_event_listener.rb', line 40

def update(event)
  vertex = event.vertex
  if check_vertex(vertex)
    realName = vertex.getName().substring(@prefix.length)
    receivers = vertex.getReceiverVertex().getTypeSet()
    receivers.each do |receiver|

      receiver_type = receiver.getMetaClass()
      if receiver_type
        location = nil
        method = receiver_type.search_method(realName)
        if method
          @locations.add(method.getLocation) if method.getLocation
        else
          klass = nil
          if receiver_type instance_of? Rsense::Ruby::MetaClass
            metaClass = receiver_type
            if metaClass.getAttached instance_of? Rsense::Ruby::RubyModule
              klass = metaClass.getAttached
            end
          else
            klass = @context.project.graph().getRuntime().getContext().getCurrentScope().getModule()
          end
          if klass
            constant = klass.getConstant(realName)
            if constant instance_of? Rsense::Typing::Runtime::VertexHolder
              location = SourceLocation.of(constant.getVertex().getNode())
            elsif constant instance_of? Rsense::Ruby::RubyModule
                location = constant.getLocation()
            end
          end
        end

        if location
          @locations.add(location)
        end

      end
    end
    vertex.cutout()
  end
end