Class: RubyLsp::Requests::CodeLens

Inherits:
Listener
  • Object
show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/requests/code_lens.rb

Overview

![Code lens demo](../../code_lens.gif)

This feature is currently experimental. Clients will need to pass ‘experimentalFeaturesEnabled` in the initialization options to enable it.

The [code lens](microsoft.github.io/language-server-protocol/specification#textDocument_codeLens) request informs the editor of runnable commands such as tests

# Example

“‘ruby # Run class Test < Minitest::Test end “`

Constant Summary collapse

ResponseType =
type_member { { fixed: T::Array[Interface::CodeLens] } }
BASE_COMMAND =
T.let((File.exist?("Gemfile.lock") ? "bundle exec ruby" : "ruby") + " -Itest ", String)
ACCESS_MODIFIERS =
T.let(["public", "private", "protected"], T::Array[String])

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Listener

add_listener, listeners

Methods included from Support::Common

#create_code_lens, #full_constant_name, #range_from_syntax_tree_node, #visible?

Constructor Details

#initialize(uri, emitter, message_queue, test_library) ⇒ CodeLens

Returns a new instance of CodeLens.

[View source]

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_lsp/requests/code_lens.rb', line 37

def initialize(uri, emitter, message_queue, test_library)
  super(emitter, message_queue)

  @test_library = T.let(test_library, String)
  @response = T.let([], ResponseType)
  @path = T.let(T.must(URI(uri).path), String)
  # visibility_stack is a stack of [current_visibility, previous_visibility]
  @visibility_stack = T.let([["public", "public"]], T::Array[T::Array[T.nilable(String)]])
  @class_stack = T.let([], T::Array[String])

  emitter.register(
    self,
    :on_class,
    :after_class,
    :on_def,
    :on_command,
    :after_command,
    :on_call,
    :after_call,
    :on_vcall,
  )
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.


34
35
36
# File 'lib/ruby_lsp/requests/code_lens.rb', line 34

def response
  @response
end

Instance Method Details

#after_call(node) ⇒ Object

[View source]

132
133
134
135
# File 'lib/ruby_lsp/requests/code_lens.rb', line 132

def after_call(node)
  _, prev_visibility = @visibility_stack.pop
  @visibility_stack.push([prev_visibility, prev_visibility])
end

#after_class(node) ⇒ Object

[View source]

76
77
78
79
# File 'lib/ruby_lsp/requests/code_lens.rb', line 76

def after_class(node)
  @visibility_stack.pop
  @class_stack.pop
end

#after_command(node) ⇒ Object

[View source]

113
114
115
116
# File 'lib/ruby_lsp/requests/code_lens.rb', line 113

def after_command(node)
  _, prev_visibility = @visibility_stack.pop
  @visibility_stack.push([prev_visibility, prev_visibility])
end

#merge_response!(other) ⇒ Object

[View source]

148
149
150
151
# File 'lib/ruby_lsp/requests/code_lens.rb', line 148

def merge_response!(other)
  @response.concat(other.response)
  self
end

#on_call(node) ⇒ Object

[View source]

119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ruby_lsp/requests/code_lens.rb', line 119

def on_call(node)
  ident = node.message if node.message.is_a?(SyntaxTree::Ident)

  if ident
    ident_value = T.cast(ident, SyntaxTree::Ident).value
    if ACCESS_MODIFIERS.include?(ident_value)
      visibility, _ = @visibility_stack.pop
      @visibility_stack.push([ident_value, visibility])
    end
  end
end

#on_class(node) ⇒ Object

[View source]

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

def on_class(node)
  @visibility_stack.push(["public", "public"])
  class_name = node.constant.constant.value
  if class_name.end_with?("Test")
    @class_stack.push(class_name)
    add_test_code_lens(
      node,
      name: class_name,
      command: generate_test_command(class_name: class_name),
      kind: :group,
    )
  end
end

#on_command(node) ⇒ Object

[View source]

99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ruby_lsp/requests/code_lens.rb', line 99

def on_command(node)
  node_message = node.message.value
  if ACCESS_MODIFIERS.include?(node_message) && node.arguments.parts.any?
    visibility, _ = @visibility_stack.pop
    @visibility_stack.push([node_message, visibility])
  elsif @path.include?("Gemfile") && node_message.include?("gem") && node.arguments.parts.any?
    remote = resolve_gem_remote(node)
    return unless remote

    add_open_gem_remote_code_lens(node, remote)
  end
end

#on_def(node) ⇒ Object

[View source]

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ruby_lsp/requests/code_lens.rb', line 82

def on_def(node)
  visibility, _ = @visibility_stack.last
  if visibility == "public"
    method_name = node.name.value
    if method_name.start_with?("test_")
      class_name = T.must(@class_stack.last)
      add_test_code_lens(
        node,
        name: method_name,
        command: generate_test_command(method_name: method_name, class_name: class_name),
        kind: :example,
      )
    end
  end
end

#on_vcall(node) ⇒ Object

[View source]

138
139
140
141
142
143
144
145
# File 'lib/ruby_lsp/requests/code_lens.rb', line 138

def on_vcall(node)
  vcall_value = node.value.value

  if ACCESS_MODIFIERS.include?(vcall_value)
    @visibility_stack.pop
    @visibility_stack.push([vcall_value, vcall_value])
  end
end