-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract shared behaviour of test disco listeners
- Loading branch information
1 parent
a205912
commit a28112a
Showing
4 changed files
with
103 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
module RubyLsp | ||
module Listeners | ||
class DiscoverTests | ||
extend T::Sig | ||
include Requests::Support::Common | ||
|
||
DYNAMIC_REFERENCE_MARKER = "<dynamic_reference>" | ||
|
||
#: (ResponseBuilders::TestCollection response_builder, GlobalState global_state, Prism::Dispatcher dispatcher, URI::Generic uri) -> void | ||
def initialize(response_builder, global_state, dispatcher, uri) | ||
@response_builder = response_builder | ||
@uri = uri | ||
@index = T.let(global_state.index, RubyIndexer::Index) | ||
@visibility_stack = T.let([:public], T::Array[Symbol]) | ||
@nesting = T.let([], T::Array[String]) | ||
|
||
@fully_qualified_name = T.let("", String) | ||
@attached_ancestors = T.let([], T::Array[String]) | ||
end | ||
|
||
#: (node: Prism::ClassNode) -> void | ||
def on_class_node_enter(node) | ||
@visibility_stack << :public | ||
name = constant_name(node.constant_path) | ||
name ||= name_with_dynamic_reference(node.constant_path) | ||
|
||
@fully_qualified_name = calc_fully_qualified_name(name) | ||
@attached_ancestors = calc_attached_ancestors(node, @fully_qualified_name) | ||
|
||
@nesting << name | ||
end | ||
|
||
#: (node: Prism::ModuleNode) -> void | ||
def on_module_node_enter(node) | ||
@visibility_stack << :public | ||
|
||
name = constant_name(node.constant_path) | ||
name ||= name_with_dynamic_reference(node.constant_path) | ||
|
||
@nesting << name | ||
end | ||
|
||
#: (node: Prism::ModuleNode) -> void | ||
def on_module_node_leave(node) | ||
@visibility_stack.pop | ||
@nesting.pop | ||
end | ||
|
||
#: (node: Prism::ClassNode) -> void | ||
def on_class_node_leave(node) | ||
@visibility_stack.pop | ||
@nesting.pop | ||
end | ||
|
||
#: (node: Prism::CallNode) -> void | ||
def on_call_node_enter(node); end | ||
|
||
#: (node: Prism::CallNode) -> void | ||
def on_call_node_leave(node); end | ||
|
||
private | ||
|
||
#: (name: String?) -> String } | ||
def calc_fully_qualified_name(name) | ||
RubyIndexer::Index.actual_nesting(@nesting, name).join("::") | ||
end | ||
|
||
#: (node: Prism::ClassNode, fully_qualified_name: String) -> Array[String] | ||
def calc_attached_ancestors(node, fully_qualified_name) | ||
@index.linearized_ancestors_of(fully_qualified_name) | ||
rescue RubyIndexer::Index::NonExistingNamespaceError | ||
# When there are dynamic parts in the constant path, we will not have indexed the namespace. We can still | ||
# provide test functionality if the class inherits directly from Test::Unit::TestCase or Minitest::Test | ||
[node.superclass&.slice].compact | ||
end | ||
|
||
#: (node: Prism::ConstantPathNode | Prism::ConstantReadNode | Prism::ConstantPathTargetNode | Prism::CallNode | Prism::MissingNode) -> String | ||
def name_with_dynamic_reference(node) | ||
slice = node.slice | ||
slice.gsub(/((?<=::)|^)[a-z]\w*/, DYNAMIC_REFERENCE_MARKER) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters