Class: RubyLsp::Requests::CodeActions
- Inherits:
-
BaseRequest
- Object
- SyntaxTree::Visitor
- BaseRequest
- RubyLsp::Requests::CodeActions
- Extended by:
- T::Sig
- Defined in:
- lib/ruby_lsp/requests/code_actions.rb
Overview

The [code actions](microsoft.github.io/language-server-protocol/specification#textDocument_codeAction) request informs the editor of RuboCop quick fixes that can be applied. These are accessible by hovering over a specific diagnostic.
# Example
“‘ruby def say_hello puts “Hello” # –> code action: quick fix indentation end “`
Instance Method Summary collapse
-
#initialize(document, range, context) ⇒ CodeActions
constructor
A new instance of CodeActions.
- #run ⇒ Object
Methods inherited from BaseRequest
Methods included from Support::Common
#create_code_lens, #full_constant_name, #range_from_syntax_tree_node, #visible?
Constructor Details
#initialize(document, range, context) ⇒ CodeActions
Returns a new instance of CodeActions.
29 30 31 32 33 34 35 |
# File 'lib/ruby_lsp/requests/code_actions.rb', line 29 def initialize(document, range, context) super(document) @uri = T.let(document.uri, String) @range = range @context = context end |
Instance Method Details
#run ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/ruby_lsp/requests/code_actions.rb', line 38 def run diagnostics = @context[:diagnostics] code_actions = diagnostics.filter_map do |diagnostic| code_action = diagnostic.dig(:data, :code_action) next if code_action.nil? # We want to return only code actions that are within range or that do not have any edits, such as refactor # code actions range = code_action.dig(:edit, :documentChanges, 0, :edits, 0, :range) code_action if diagnostic.dig(:data, :correctable) && cover?(range) end # Only add refactor actions if there's a non empty selection in the editor code_actions << refactor_code_action(@range, @uri) unless @range.dig(:start) == @range.dig(:end) code_actions end |