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

The [code lens](microsoft.github.io/language-server-protocol/specification#textDocument_codeLens) request informs the editor of runnable commands such as tests
# Configuration
To disable gem code lenses, set rubyLsp.featuresConfiguration.codeLens.gemfileLinks to false.
# Example
“‘ruby # Run class Test < Minitest::Test end “`
Constant Summary collapse
- ResponseType =
type_member { { fixed: T::Array[Interface::CodeLens] } }
- BASE_COMMAND =
T.let( begin Bundler.with_original_env { Bundler.default_lockfile } "bundle exec ruby" rescue Bundler::GemfileNotFound "ruby" end + " -Itest ", String, )
- GEMFILE_NAME =
T.let( begin Bundler.with_original_env { Bundler.default_gemfile.basename.to_s } rescue Bundler::GemfileNotFound "Gemfile" end, String, )
- ACCESS_MODIFIERS =
T.let([:public, :private, :protected], T::Array[Symbol])
- SUPPORTED_TEST_LIBRARIES =
T.let(["minitest", "test-unit"], T::Array[String])
Instance Attribute Summary collapse
-
#_response ⇒ Object
readonly
Returns the value of attribute _response.
Instance Method Summary collapse
-
#initialize(uri, lenses_configuration, dispatcher) ⇒ CodeLens
constructor
A new instance of CodeLens.
- #initialize_external_listener(addon) ⇒ Object
- #merge_response!(other) ⇒ Object
- #on_call_node_enter(node) ⇒ Object
- #on_call_node_leave(node) ⇒ Object
- #on_class_node_enter(node) ⇒ Object
- #on_class_node_leave(node) ⇒ Object
- #on_def_node_enter(node) ⇒ Object
Methods inherited from Listener
Methods included from Support::Common
#create_code_lens, #defined_in_gem?, #markdown_from_index_entries, #range_from_location, #range_from_node, #self_receiver?, #visible?
Constructor Details
#initialize(uri, lenses_configuration, dispatcher) ⇒ CodeLens
Returns a new instance of CodeLens.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 61 def initialize(uri, lenses_configuration, dispatcher) @uri = T.let(uri, URI::Generic) @_response = T.let([], ResponseType) @path = T.let(uri.to_standardized_path, T.nilable(String)) # visibility_stack is a stack of [current_visibility, previous_visibility] @visibility_stack = T.let([[:public, :public]], T::Array[T::Array[T.nilable(Symbol)]]) @class_stack = T.let([], T::Array[String]) @group_id = T.let(1, Integer) @group_id_stack = T.let([], T::Array[Integer]) @lenses_configuration = lenses_configuration super(dispatcher) dispatcher.register( self, :on_class_node_enter, :on_class_node_leave, :on_def_node_enter, :on_call_node_enter, :on_call_node_leave, ) end |
Instance Attribute Details
#_response ⇒ Object (readonly)
Returns the value of attribute _response.
52 53 54 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 52 def _response @_response end |
Instance Method Details
#initialize_external_listener(addon) ⇒ Object
167 168 169 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 167 def initialize_external_listener(addon) addon.create_code_lens_listener(@uri, @dispatcher) end |
#merge_response!(other) ⇒ Object
172 173 174 175 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 172 def merge_response!(other) @_response.concat(other.response) self end |
#on_call_node_enter(node) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 130 def on_call_node_enter(node) name = node.name arguments = node.arguments # If we found `private` by itself or `private def foo` if ACCESS_MODIFIERS.include?(name) if arguments.nil? @visibility_stack.pop @visibility_stack.push([name, name]) elsif arguments.arguments.first.is_a?(Prism::DefNode) visibility, _ = @visibility_stack.pop @visibility_stack.push([name, visibility]) end return end if @path&.include?(GEMFILE_NAME) && name == :gem && arguments return unless @lenses_configuration.enabled?(:gemfileLinks) first_argument = arguments.arguments.first return unless first_argument.is_a?(Prism::StringNode) remote = resolve_gem_remote(first_argument) return unless remote add_open_gem_remote_code_lens(node, remote) end end |
#on_call_node_leave(node) ⇒ Object
161 162 163 164 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 161 def on_call_node_leave(node) _, prev_visibility = @visibility_stack.pop @visibility_stack.push([prev_visibility, prev_visibility]) end |
#on_class_node_enter(node) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 85 def on_class_node_enter(node) @visibility_stack.push([:public, :public]) class_name = node.constant_path.slice @class_stack.push(class_name) if @path && class_name.end_with?("Test") add_test_code_lens( node, name: class_name, command: generate_test_command(class_name: class_name), kind: :group, ) end @group_id_stack.push(@group_id) @group_id += 1 end |
#on_class_node_leave(node) ⇒ Object
104 105 106 107 108 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 104 def on_class_node_leave(node) @visibility_stack.pop @class_stack.pop @group_id_stack.pop end |
#on_def_node_enter(node) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 111 def on_def_node_enter(node) class_name = @class_stack.last return unless class_name&.end_with?("Test") visibility, _ = @visibility_stack.last if visibility == :public method_name = node.name.to_s if @path && method_name.start_with?("test_") 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 |