Class: RubyLsp::EventEmitter
- Inherits:
-
SyntaxTree::Visitor
- Object
- SyntaxTree::Visitor
- RubyLsp::EventEmitter
- Extended by:
- T::Sig
- 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_targetto fire events for each listener -
For nonpositional requests, use
visitto 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
- #emit_for_target(node) ⇒ Object
-
#initialize ⇒ EventEmitter
constructor
A new instance of EventEmitter.
- #register(listener, *events) ⇒ Object
- #visit_call(node) ⇒ Object
- #visit_class(node) ⇒ Object
- #visit_command(node) ⇒ Object
- #visit_comment(node) ⇒ Object
- #visit_const_path_field(node) ⇒ Object
- #visit_def(node) ⇒ Object
- #visit_module(node) ⇒ Object
- #visit_top_const_field(node) ⇒ Object
- #visit_var_field(node) ⇒ Object
- #visit_vcall(node) ⇒ Object
Constructor Details
#initialize ⇒ EventEmitter
25 26 27 28 |
# File 'lib/ruby_lsp/event_emitter.rb', line 25 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
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ruby_lsp/event_emitter.rb', line 38 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
31 32 33 |
# File 'lib/ruby_lsp/event_emitter.rb', line 31 def register(listener, *events) events.each { |event| T.must(@listeners[event]) << listener } end |
#visit_call(node) ⇒ Object
77 78 79 80 81 |
# File 'lib/ruby_lsp/event_emitter.rb', line 77 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
56 57 58 59 60 |
# File 'lib/ruby_lsp/event_emitter.rb', line 56 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
70 71 72 73 74 |
# File 'lib/ruby_lsp/event_emitter.rb', line 70 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_comment(node) ⇒ Object
115 116 117 118 |
# File 'lib/ruby_lsp/event_emitter.rb', line 115 def visit_comment(node) @listeners[:on_comment]&.each { |l| T.unsafe(l).on_comment(node) } super end |
#visit_const_path_field(node) ⇒ Object
90 91 92 93 |
# File 'lib/ruby_lsp/event_emitter.rb', line 90 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
102 103 104 105 106 |
# File 'lib/ruby_lsp/event_emitter.rb', line 102 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_module(node) ⇒ Object
63 64 65 66 67 |
# File 'lib/ruby_lsp/event_emitter.rb', line 63 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_top_const_field(node) ⇒ Object
96 97 98 99 |
# File 'lib/ruby_lsp/event_emitter.rb', line 96 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
109 110 111 112 |
# File 'lib/ruby_lsp/event_emitter.rb', line 109 def visit_var_field(node) @listeners[:on_var_field]&.each { |l| T.unsafe(l).on_var_field(node) } super end |
#visit_vcall(node) ⇒ Object
84 85 86 87 |
# File 'lib/ruby_lsp/event_emitter.rb', line 84 def visit_vcall(node) @listeners[:on_vcall]&.each { |l| T.unsafe(l).on_vcall(node) } super end |