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
![Code lens demo](../../code_lens.gif)
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])
- 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
- #after_call(node) ⇒ Object
- #after_class(node) ⇒ Object
-
#initialize(uri, emitter, message_queue) ⇒ CodeLens
constructor
A new instance of CodeLens.
- #initialize_external_listener(addon) ⇒ Object
- #merge_response!(other) ⇒ Object
- #on_call(node) ⇒ Object
- #on_class(node) ⇒ Object
- #on_def(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, emitter, message_queue) ⇒ CodeLens
Returns a new instance of CodeLens.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 35 def initialize(uri, emitter, ) @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(String)]]) @class_stack = T.let([], T::Array[String]) super(emitter, ) emitter.register( self, :on_class, :after_class, :on_def, :on_call, :after_call, ) end |
Instance Attribute Details
#_response ⇒ Object (readonly)
Returns the value of attribute _response.
32 33 34 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 32 def _response @_response end |
Instance Method Details
#after_call(node) ⇒ Object
126 127 128 129 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 126 def after_call(node) _, prev_visibility = @visibility_stack.pop @visibility_stack.push([prev_visibility, prev_visibility]) end |
#after_class(node) ⇒ Object
72 73 74 75 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 72 def after_class(node) @visibility_stack.pop @class_stack.pop end |
#initialize_external_listener(addon) ⇒ Object
132 133 134 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 132 def initialize_external_listener(addon) addon.create_code_lens_listener(@uri, @emitter, @message_queue) end |
#merge_response!(other) ⇒ Object
137 138 139 140 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 137 def merge_response!(other) @_response.concat(other.response) self end |
#on_call(node) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 97 def on_call(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?(YARP::DefNode) visibility, _ = @visibility_stack.pop @visibility_stack.push([name, visibility]) end return end if @path&.include?("Gemfile") && name == "gem" && arguments first_argument = arguments.arguments.first return unless first_argument.is_a?(YARP::StringNode) remote = resolve_gem_remote(first_argument) return unless remote add_open_gem_remote_code_lens(node, remote) end end |
#on_class(node) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 56 def on_class(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_def(node) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ruby_lsp/requests/code_lens.rb', line 78 def on_def(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 |