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
# 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, dispatcher, message_queue) ⇒ 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, #markdown_from_index_entries, #range_from_location, #range_from_node, #visible?
Constructor Details
#initialize(uri, dispatcher, message_queue) ⇒ CodeLens
Returns a new instance of CodeLens.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 51 def initialize(uri, 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]) 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.
48 49 50 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 48 def _response @_response end |
Instance Method Details
#initialize_external_listener(addon) ⇒ Object
148 149 150 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 148 def initialize_external_listener(addon) addon.create_code_lens_listener(@uri, @dispatcher, @message_queue) end |
#merge_response!(other) ⇒ Object
153 154 155 156 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 153 def merge_response!(other) @_response.concat(other.response) self end |
#on_call_node_enter(node) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 113 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 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
142 143 144 145 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 142 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 72 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 end |
#on_class_node_leave(node) ⇒ Object
88 89 90 91 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 88 def on_class_node_leave(node) @visibility_stack.pop @class_stack.pop end |
#on_def_node_enter(node) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 94 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 |