Class: RubyLsp::Requests::CodeActions

Inherits:
BaseRequest
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_lsp/requests/code_actions.rb

Overview

![Code actions demo](../../misc/code_actions.gif)

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

Methods inherited from BaseRequest

#full_constant_name, #locate, #range_from_syntax_tree_node, #visible?

Constructor Details

#initialize(uri, document, range, context) ⇒ CodeActions

Returns a new instance of CodeActions.



30
31
32
33
34
35
36
# File 'lib/ruby_lsp/requests/code_actions.rb', line 30

def initialize(uri, document, range, context)
  super(document)

  @uri = uri
  @range = range
  @context = context
end

Instance Method Details

#runObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_lsp/requests/code_actions.rb', line 39

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

  code_actions << refactor_code_action(@range, @uri)
end