Class: RubyLsp::Requests::CodeLens
- Extended by:
- T::Generic, T::Sig
- Defined in:
- lib/ruby_lsp/requests/code_lens.rb
Overview

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
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #after_call(node) ⇒ Object
- #after_class(node) ⇒ Object
- #after_command(node) ⇒ Object
-
#initialize(uri, emitter, message_queue, test_library) ⇒ CodeLens
constructor
A new instance of CodeLens.
- #merge_response!(other) ⇒ Object
- #on_call(node) ⇒ Object
- #on_class(node) ⇒ Object
- #on_command(node) ⇒ Object
- #on_def(node) ⇒ Object
- #on_vcall(node) ⇒ Object
Methods inherited from Listener
Methods included from Support::Common
#create_code_lens, #full_constant_name, #range_from_syntax_tree_node, #visible?
Constructor Details
permalink #initialize(uri, emitter, message_queue, test_library) ⇒ CodeLens
Returns a new instance of CodeLens.
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, , test_library) super(emitter, ) @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
permalink #response ⇒ Object (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
permalink #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 |
permalink #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 |
permalink #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 |
permalink #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 |
permalink #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. if node..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 |
permalink #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 |
permalink #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..value if ACCESS_MODIFIERS.include?() && node.arguments.parts.any? visibility, _ = @visibility_stack.pop @visibility_stack.push([, visibility]) elsif @path.include?("Gemfile") && .include?("gem") && node.arguments.parts.any? remote = resolve_gem_remote(node) return unless remote add_open_gem_remote_code_lens(node, remote) end end |
permalink #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 |
permalink #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 |