Class: RubyLsp::EventEmitter

Inherits:
SyntaxTree::Visitor
  • Object
show all
Extended by:
T::Sig
Includes:
SyntaxTree::WithScope
Defined in:
lib/ruby_lsp/event_emitter.rb

Overview

EventEmitter is an intermediary between our requests and Syntax Tree visitors. It’s used to visit the document’s AST and emit events that the requests can listen to for providing functionality. Usages:

  • For positional requests, locate the target node and use ‘emit_for_target` to fire events for each listener

  • For nonpositional requests, use ‘visit` to go through the AST, which will fire events for each listener as nodes

are found

# Example

“‘ruby target_node = document.locate_node(position) emitter = EventEmitter.new listener = Requests::Hover.new(emitter, @message_queue) emitter.emit_for_target(target_node) listener.response “`

Instance Method Summary collapse

Constructor Details

#initializeEventEmitter

Returns a new instance of EventEmitter.



26
27
28
29
# File 'lib/ruby_lsp/event_emitter.rb', line 26

def initialize
  @listeners = T.let(Hash.new { |h, k| h[k] = [] }, T::Hash[Symbol, T::Array[Listener[T.untyped]]])
  super()
end

Instance Method Details

#emit_for_target(node) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_lsp/event_emitter.rb', line 39

def emit_for_target(node)
  case node
  when SyntaxTree::Command
    @listeners[:on_command]&.each { |l| T.unsafe(l).on_command(node) }
  when SyntaxTree::CallNode
    @listeners[:on_call]&.each { |l| T.unsafe(l).on_call(node) }
  when SyntaxTree::TStringContent
    @listeners[:on_tstring_content]&.each { |l| T.unsafe(l).on_tstring_content(node) }
  when SyntaxTree::ConstPathRef
    @listeners[:on_const_path_ref]&.each { |l| T.unsafe(l).on_const_path_ref(node) }
  when SyntaxTree::Const
    @listeners[:on_const]&.each { |l| T.unsafe(l).on_const(node) }
  end
end

#register(listener, *events) ⇒ Object



32
33
34
# File 'lib/ruby_lsp/event_emitter.rb', line 32

def register(listener, *events)
  events.each { |event| T.must(@listeners[event]) << listener }
end

#visit_binary(node) ⇒ Object



170
171
172
173
# File 'lib/ruby_lsp/event_emitter.rb', line 170

def visit_binary(node)
  super
  @listeners[:after_binary]&.each { |l| T.unsafe(l).after_binary(node) }
end

#visit_block_var(node) ⇒ Object



158
159
160
161
# File 'lib/ruby_lsp/event_emitter.rb', line 158

def visit_block_var(node)
  @listeners[:on_block_var]&.each { |l| T.unsafe(l).on_block_var(node) }
  super
end

#visit_call(node) ⇒ Object



84
85
86
87
88
# File 'lib/ruby_lsp/event_emitter.rb', line 84

def visit_call(node)
  @listeners[:on_call]&.each { |l| T.unsafe(l).on_call(node) }
  super
  @listeners[:after_call]&.each { |l| T.unsafe(l).after_call(node) }
end

#visit_class(node) ⇒ Object



57
58
59
60
61
# File 'lib/ruby_lsp/event_emitter.rb', line 57

def visit_class(node)
  @listeners[:on_class]&.each { |l| T.unsafe(l).on_class(node) }
  super
  @listeners[:after_class]&.each { |l| T.unsafe(l).after_class(node) }
end

#visit_command(node) ⇒ Object



71
72
73
74
75
# File 'lib/ruby_lsp/event_emitter.rb', line 71

def visit_command(node)
  @listeners[:on_command]&.each { |l| T.unsafe(l).on_command(node) }
  super
  @listeners[:after_command]&.each { |l| T.unsafe(l).after_command(node) }
end

#visit_command_call(node) ⇒ Object



78
79
80
81
# File 'lib/ruby_lsp/event_emitter.rb', line 78

def visit_command_call(node)
  @listeners[:on_command_call]&.each { |l| T.unsafe(l).on_command_call(node) }
  super
end

#visit_comment(node) ⇒ Object



122
123
124
125
# File 'lib/ruby_lsp/event_emitter.rb', line 122

def visit_comment(node)
  @listeners[:on_comment]&.each { |l| T.unsafe(l).on_comment(node) }
  super
end

#visit_const(node) ⇒ Object



176
177
178
179
# File 'lib/ruby_lsp/event_emitter.rb', line 176

def visit_const(node)
  @listeners[:on_const]&.each { |l| T.unsafe(l).on_const(node) }
  super
end

#visit_const_path_field(node) ⇒ Object



97
98
99
100
# File 'lib/ruby_lsp/event_emitter.rb', line 97

def visit_const_path_field(node)
  @listeners[:on_const_path_field]&.each { |l| T.unsafe(l).on_const_path_field(node) }
  super
end

#visit_def(node) ⇒ Object



109
110
111
112
113
# File 'lib/ruby_lsp/event_emitter.rb', line 109

def visit_def(node)
  @listeners[:on_def]&.each { |l| T.unsafe(l).on_def(node) }
  super
  @listeners[:after_def]&.each { |l| T.unsafe(l).after_def(node) }
end

#visit_field(node) ⇒ Object



146
147
148
149
# File 'lib/ruby_lsp/event_emitter.rb', line 146

def visit_field(node)
  @listeners[:on_field]&.each { |l| T.unsafe(l).on_field(node) }
  super
end

#visit_kw(node) ⇒ Object



134
135
136
137
# File 'lib/ruby_lsp/event_emitter.rb', line 134

def visit_kw(node)
  @listeners[:on_kw]&.each { |l| T.unsafe(l).on_kw(node) }
  super
end

#visit_lambda_var(node) ⇒ Object



164
165
166
167
# File 'lib/ruby_lsp/event_emitter.rb', line 164

def visit_lambda_var(node)
  @listeners[:on_lambda_var]&.each { |l| T.unsafe(l).on_lambda_var(node) }
  super
end

#visit_module(node) ⇒ Object



64
65
66
67
68
# File 'lib/ruby_lsp/event_emitter.rb', line 64

def visit_module(node)
  @listeners[:on_module]&.each { |l| T.unsafe(l).on_module(node) }
  super
  @listeners[:after_module]&.each { |l| T.unsafe(l).after_module(node) }
end

#visit_params(node) ⇒ Object



140
141
142
143
# File 'lib/ruby_lsp/event_emitter.rb', line 140

def visit_params(node)
  @listeners[:on_params]&.each { |l| T.unsafe(l).on_params(node) }
  super
end

#visit_rescue(node) ⇒ Object



128
129
130
131
# File 'lib/ruby_lsp/event_emitter.rb', line 128

def visit_rescue(node)
  @listeners[:on_rescue]&.each { |l| T.unsafe(l).on_rescue(node) }
  super
end

#visit_top_const_field(node) ⇒ Object



103
104
105
106
# File 'lib/ruby_lsp/event_emitter.rb', line 103

def visit_top_const_field(node)
  @listeners[:on_top_const_field]&.each { |l| T.unsafe(l).on_top_const_field(node) }
  super
end

#visit_var_field(node) ⇒ Object



116
117
118
119
# File 'lib/ruby_lsp/event_emitter.rb', line 116

def visit_var_field(node)
  @listeners[:on_var_field]&.each { |l| T.unsafe(l).on_var_field(node) }
  super
end

#visit_var_ref(node) ⇒ Object



152
153
154
155
# File 'lib/ruby_lsp/event_emitter.rb', line 152

def visit_var_ref(node)
  @listeners[:on_var_ref]&.each { |l| T.unsafe(l).on_var_ref(node) }
  super
end

#visit_vcall(node) ⇒ Object



91
92
93
94
# File 'lib/ruby_lsp/event_emitter.rb', line 91

def visit_vcall(node)
  @listeners[:on_vcall]&.each { |l| T.unsafe(l).on_vcall(node) }
  super
end