Class: RubyLsp::Requests::DiscoverTests

Inherits:
Request
  • Object
show all
Includes:
Support::Common
Defined in:
lib/ruby_lsp/requests/discover_tests.rb

Overview

This is a custom request to ask the server to parse a test file and discover all available examples in it. Add-ons can augment the behavior through listeners, allowing them to handle discovery for different frameworks

Instance Method Summary collapse

Methods included from Support::Common

#categorized_markdown_from_index_entries, #constant_name, #create_code_lens, #each_constant_path_part, #kind_for_entry, #markdown_from_index_entries, #namespace_constant_name, #not_in_dependencies?, #range_from_location, #range_from_node, #self_receiver?

Constructor Details

#initialize(global_state, document, dispatcher) ⇒ DiscoverTests

: (GlobalState global_state, RubyDocument document, Prism::Dispatcher dispatcher) -> void



16
17
18
19
20
21
22
23
# File 'lib/ruby_lsp/requests/discover_tests.rb', line 16

def initialize(global_state, document, dispatcher)
  super()
  @global_state = global_state
  @document = document
  @dispatcher = dispatcher
  @response_builder = ResponseBuilders::TestCollection.new #: ResponseBuilders::TestCollection
  @index = global_state.index #: RubyIndexer::Index
end

Instance Method Details

#performObject

: -> Array



27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/ruby_lsp/requests/discover_tests.rb', line 27

def perform
  uri = @document.uri

  # We normally only index test files once they are opened in the editor to save memory and avoid doing
  # unnecessary work. If the file is already opened and we already indexed it, then we can just discover the tests
  # straight away.
  #
  # However, if the user navigates to a specific test file from the explorer with nothing opened in the UI, then
  # we will not have indexed the test file yet and trying to linearize the ancestor of the class will fail. In
  # this case, we have to instantiate the indexer listener first, so that we insert classes, modules and methods
  # in the index first and then discover the tests, all in the same traversal.
  if @index.entries_for(uri.to_s)
    Listeners::TestStyle.new(@response_builder, @global_state, @dispatcher, @document.uri)
    Listeners::SpecStyle.new(@response_builder, @global_state, @dispatcher, @document.uri)

    Addon.addons.each do |addon|
      addon.create_discover_tests_listener(@response_builder, @dispatcher, @document.uri)
    end

    @dispatcher.visit(@document.ast)
  else
    @global_state.synchronize do
      RubyIndexer::DeclarationListener.new(
        @index,
        @dispatcher,
        @document.parse_result,
        uri,
        collect_comments: true,
      )

      Listeners::TestStyle.new(@response_builder, @global_state, @dispatcher, @document.uri)
      Listeners::SpecStyle.new(@response_builder, @global_state, @dispatcher, @document.uri)

      Addon.addons.each do |addon|
        addon.create_discover_tests_listener(@response_builder, @dispatcher, @document.uri)
      end

      # Dispatch the events both for indexing the test file and discovering the tests. The order here is
      # important because we need the index to be aware of the existing classes/modules/methods before the test
      # listeners can do their work
      @dispatcher.visit(@document.ast)
    end
  end

  @response_builder.response
end