Class: RubyLsp::ShouldaContext::CodeLens

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Sig
Includes:
Requests::Support::Common
Defined in:
lib/ruby_lsp/ruby-lsp-shoulda-context/code_lens.rb

Constant Summary collapse

BASE_COMMAND =
T.let(
  begin
    cmd = File.exist?(File.join(Dir.pwd, "bin", "rails")) ? "bin/rails test" : "ruby -ITest"
    Bundler.with_original_env { Bundler.default_lockfile }
    "bundle exec #{cmd}"
  rescue Bundler::GemfileNotFound
    cmd
  end,
  String,
)
REQUIRED_LIBRARY =
T.let("shoulda-context", String)
ResponseType =
type_member { { fixed: T::Array[::RubyLsp::Interface::CodeLens] } }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response_builder, uri, dispatcher, global_state) ⇒ CodeLens

Returns a new instance of CodeLens.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ruby_lsp/ruby-lsp-shoulda-context/code_lens.rb', line 38

def initialize(response_builder, uri, dispatcher, global_state)
  @response_builder = response_builder
  @global_state = global_state
  return if ENV["RUBY_LSP_SHOULDA_CONTEXT"] == "false"

  # Listener is only initialized if uri.to_standardized_path is valid
  @path = T.let(T.must(uri.to_standardized_path), String)
  @class_name = T.let("", String)
  @group_id = T.let(1, Integer)
  @group_id_stack = T.let([], T::Array[Integer])
  @pattern = T.let("test_: ", String)
  dispatcher.register(self, :on_call_node_enter, :on_call_node_leave, :on_class_node_enter, :on_class_node_leave)

  @base_command = BASE_COMMAND
end

Instance Attribute Details

#_responseObject (readonly)

Returns the value of attribute _response.



28
29
30
# File 'lib/ruby_lsp/ruby-lsp-shoulda-context/code_lens.rb', line 28

def _response
  @_response
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby_lsp/ruby-lsp-shoulda-context/code_lens.rb', line 55

def on_call_node_enter(node)
  case node.message
  when "should"
    name = generate_name(node)

    # If is top level should without context the DSL is different
    if @group_id_stack.length == 1
      @pattern += "#{@class_name} "
    end

    @pattern += "should #{name} "
    add_test_code_lens(node, name: name, kind: :example)
  when "context"
    return unless valid_group?(node)

    name = generate_name(node)
    @pattern += "#{name} "
    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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby_lsp/ruby-lsp-shoulda-context/code_lens.rb', line 80

def on_call_node_leave(node)
  case node.message
  when "should"
    name = generate_name(node)

    @pattern = remove_last_pattern_in_string(@pattern, "should #{name} ")

    # If is top level should without context the DSL is different
    if @group_id_stack.length == 1
      @pattern = remove_last_pattern_in_string(@pattern, "#{@class_name} ")
    end
  when "context"
    return unless valid_group?(node)

    name = generate_name(node)
    @pattern = remove_last_pattern_in_string(@pattern, "#{name} ")
    @group_id_stack.pop
  end
end

#on_class_node_enter(node) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ruby_lsp/ruby-lsp-shoulda-context/code_lens.rb', line 101

def on_class_node_enter(node)
  class_name = node.constant_path.slice
  @class_name = remove_last_pattern_in_string(class_name, "Test")

  if @path && class_name.end_with?("Test")
    add_test_code_lens(
      node,
      name: class_name,
      kind: :class,
    )
  end

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

#on_class_node_leave(node) ⇒ Object



118
119
120
# File 'lib/ruby_lsp/ruby-lsp-shoulda-context/code_lens.rb', line 118

def on_class_node_leave(node)
  @group_id_stack.pop
end