Class: RubyLsp::RSpec::CodeLens

Inherits:
Listener
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
RubyLsp::Requests::Support::Common
Defined in:
lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb

Constant Summary collapse

ResponseType =
type_member { { fixed: T::Array[::RubyLsp::Interface::CodeLens] } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, dispatcher, message_queue) ⇒ CodeLens

Returns a new instance of CodeLens.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb', line 18

def initialize(uri, dispatcher, message_queue)
  @_response = T.let([], ResponseType)
  # Listener is only initialized if uri.to_standardized_path is valid
  @path = T.let(T.must(uri.to_standardized_path), String)
  @group_id = T.let(1, Integer)
  @group_id_stack = T.let([], T::Array[Integer])
  dispatcher.register(self, :on_call_node_enter, :on_call_node_leave)

  @base_command = T.let(
    begin
      cmd = if File.exist?(File.join(Dir.pwd, "bin", "rspec"))
        "bin/rspec"
      else
        "rspec"
      end

      if File.exist?("Gemfile.lock")
        "bundle exec #{cmd}"
      else
        cmd
      end
    end,
    String,
  )

  super(dispatcher, message_queue)
end

Instance Attribute Details

#_responseObject (readonly)

Returns the value of attribute _response.



15
16
17
# File 'lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb', line 15

def _response
  @_response
end

Instance Method Details

#generate_name(node) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb', line 74

def generate_name(node)
  if node.arguments
    argument = node.arguments.arguments.first

    case argument
    when Prism::StringNode
      argument.content
    when Prism::CallNode
      "<#{argument.name}>"
    when nil
      ""
    else
      argument.slice
    end
  else
    "<unnamed>"
  end
end

#on_call_node_enter(node) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb', line 47

def on_call_node_enter(node)
  case node.message
  when "example", "it", "specify"
    name = generate_name(node)
    add_test_code_lens(node, name: name, kind: :example)
  when "context", "describe"
    return if node.receiver && node.receiver.name.to_s != "RSpec"

    name = generate_name(node)
    add_test_code_lens(node, name: name, kind: :group)

    @group_id_stack.push(@group_id)
    @group_id += 1
  end
end

#on_call_node_leave(node) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/ruby_lsp/ruby_lsp_rspec/code_lens.rb', line 64

def on_call_node_leave(node)
  case node.message
  when "context", "describe"
    return if node.receiver && node.receiver.name.to_s != "RSpec"

    @group_id_stack.pop
  end
end