Class: RubyLsp::Requests::CodeActions

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

Overview

![Code actions demo](../../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 “`

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, range, context) ⇒ CodeActions

Returns a new instance of CodeActions.



38
39
40
41
42
43
44
# File 'lib/ruby_lsp/requests/code_actions.rb', line 38

def initialize(document, range, context)
  super()
  @document = document
  @uri = T.let(document.uri, URI::Generic)
  @range = range
  @context = context
end

Class Method Details

.providerObject



26
27
28
# File 'lib/ruby_lsp/requests/code_actions.rb', line 26

def provider
  Interface::CodeActionOptions.new(resolve_provider: true)
end

Instance Method Details

#performObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_lsp/requests/code_actions.rb', line 47

def perform
  diagnostics = @context[:diagnostics]

  code_actions = diagnostics.flat_map do |diagnostic|
    diagnostic.dig(:data, :code_actions) || []
  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