Class: RubyLsp::RSpec::Addon

Inherits:
Addon
  • Object
show all
Defined in:
lib/ruby_lsp/ruby_lsp_rspec/addon.rb

Constant Summary collapse

FORMATTER_PATH =

: String

File.expand_path("rspec_formatter.rb", __dir__)
FORMATTER_NAME =

: String

"RubyLsp::RSpec::RSpecFormatter"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Addon

: -> void



24
25
26
27
28
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 24

def initialize
  super
  @debug = false #: bool
  @rspec_command = nil #: String?
end

Instance Attribute Details

#debug ⇒ Object (readonly)

: bool



21
22
23
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 21

def debug
  @debug
end

Instance Method Details

#activate(global_state, message_queue) ⇒ Object

: (GlobalState, Thread::Queue) -> void



32
33
34
35
36
37
38
39
40
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 32

def activate(global_state, message_queue)
  @index = global_state.index #: RubyIndexer::Index?
  @global_state = global_state #: GlobalState?

  settings = global_state.settings_for_addon(name)
  @rspec_command = rspec_command(settings)
  @workspace_path = global_state.workspace_path #: String?
  @debug = settings&.dig(:debug) || false
end

#create_code_lens_listener(response_builder, uri, dispatcher) ⇒ Object

Creates a new CodeLens listener. This method is invoked on every CodeLens request : (ResponseBuilders::CollectionResponseBuilder, URI::Generic, Prism::Dispatcher) -> void



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 61

def create_code_lens_listener(response_builder, uri, dispatcher)
  return unless uri.to_standardized_path&.end_with?("_test.rb") || uri.to_standardized_path&.end_with?("_spec.rb")
  return if @global_state&.enabled_feature?(:fullTestDiscovery)

  CodeLens.new(
    response_builder,
    uri,
    dispatcher,
    @rspec_command, #: as !nil
    debug: debug,
  )
end

#create_definition_listener(response_builder, uri, node_context, dispatcher) ⇒ Object

: (ResponseBuilders::CollectionResponseBuilder[Interface::Location | Interface::LocationLink], URI::Generic, NodeContext, Prism::Dispatcher) -> void



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 144

def create_definition_listener(response_builder, uri, node_context, dispatcher)
  return unless uri.to_standardized_path&.end_with?("_test.rb") || uri.to_standardized_path&.end_with?("_spec.rb")

  Definition.new(
    response_builder,
    uri,
    node_context,
    @index, #: as !nil
    dispatcher,
  )
end

#create_discover_tests_listener(response_builder, dispatcher, uri) ⇒ Object

Creates a new Discover Tests listener. This method is invoked on every DiscoverTests request : (ResponseBuilders::TestCollection, Prism::Dispatcher, URI::Generic) -> void



77
78
79
80
81
82
83
84
85
86
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 77

def create_discover_tests_listener(response_builder, dispatcher, uri)
  return unless uri.to_standardized_path&.end_with?("_spec.rb")

  TestDiscovery.new(
    response_builder,
    dispatcher,
    uri,
    @workspace_path, #: as !nil
  )
end

#create_document_symbol_listener(response_builder, dispatcher) ⇒ Object

: (ResponseBuilders::DocumentSymbol, Prism::Dispatcher) -> void



138
139
140
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 138

def create_document_symbol_listener(response_builder, dispatcher)
  DocumentSymbol.new(response_builder, dispatcher)
end

#deactivate ⇒ Object

: -> void



44
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 44

def deactivate; end

#name ⇒ Object

: -> String



48
49
50
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 48

def name
  "Ruby LSP RSpec"
end

#resolve_test_commands(items) ⇒ Object

Resolves the minimal set of commands required to execute the requested tests : (Array[Hash[Symbol, untyped]]) -> Array



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 91

def resolve_test_commands(items)
  commands = []
  queue = items.dup

  full_files = []

  until queue.empty?
    item = queue.shift #: as !nil
    tags = Set.new(item[:tags])
    next unless tags.include?("framework:rspec")

    children = item[:children]
    uri = URI(item[:uri])
    path = uri.full_path
    next unless path

    if tags.include?("test_dir")
      if children.empty?
        full_files.concat(Dir.glob(
          "#{path}/**/*_spec.rb",
          File::Constants::FNM_EXTGLOB | File::Constants::FNM_PATHNAME,
        ))
      end
    elsif tags.include?("test_file")
      full_files << path if children.empty?
    elsif tags.include?("test_group")
      start_line = item.dig(:range, :start, :line)
      commands << "#{@rspec_command} -r #{FORMATTER_PATH} -f #{FORMATTER_NAME} #{path}:#{start_line + 1}"
    elsif tags.include?("test_case")
      full_files << "#{path}:#{item.dig(:range, :start, :line) + 1}"
    else
      # whole project
      full_files << path
    end

    queue.concat(children)
  end

  unless full_files.empty?
    commands << "#{@rspec_command} -r #{FORMATTER_PATH} -f #{FORMATTER_NAME} #{full_files.join(" ")}"
  end

  commands
end

#version ⇒ Object

: -> String



54
55
56
# File 'lib/ruby_lsp/ruby_lsp_rspec/addon.rb', line 54

def version
  VERSION
end