Class: RubyLsp::Requests::CodeActionResolve

Inherits:
Request
  • Object
show all
Includes:
Support::Common
Defined in:
lib/ruby_lsp/requests/code_action_resolve.rb

Overview

The [code action resolve](microsoft.github.io/language-server-protocol/specification#codeAction_resolve) request is used to to resolve the edit field for a given code action, if it is not already provided in the textDocument/codeAction response. We can use it for scenarios that require more computation such as refactoring.

Defined Under Namespace

Classes: CodeActionError, EmptySelectionError, InvalidTargetRangeError, UnknownCodeActionError

Constant Summary collapse

NEW_VARIABLE_NAME =
"new_variable"
NEW_METHOD_NAME =
"new_method"

Instance Method Summary collapse

Methods included from Support::Common

#categorized_markdown_from_index_entries, #constant_name, #create_code_lens, #each_constant_path_part, #kind_for_entry, #markdown_from_index_entries, #namespace_constant_name, #not_in_dependencies?, #range_from_location, #range_from_node, #self_receiver?

Constructor Details

#initialize(document, global_state, code_action) ⇒ CodeActionResolve

: (RubyDocument document, GlobalState global_state, Hash[Symbol, untyped] code_action) -> void



21
22
23
24
25
26
# File 'lib/ruby_lsp/requests/code_action_resolve.rb', line 21

def initialize(document, global_state, code_action)
  super()
  @document = document
  @global_state = global_state
  @code_action = code_action
end

Instance Method Details

#performObject

: -> (Interface::CodeAction)



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_lsp/requests/code_action_resolve.rb', line 30

def perform
  raise EmptySelectionError, "Invalid selection for refactor" if @document.source.empty?

  case @code_action[:title]
  when CodeActions::EXTRACT_TO_VARIABLE_TITLE
    refactor_variable
  when CodeActions::EXTRACT_TO_METHOD_TITLE
    refactor_method
  when CodeActions::TOGGLE_BLOCK_STYLE_TITLE
    switch_block_style
  when CodeActions::CREATE_ATTRIBUTE_READER,
       CodeActions::CREATE_ATTRIBUTE_WRITER,
       CodeActions::CREATE_ATTRIBUTE_ACCESSOR
    create_attribute_accessor
  else
    raise UnknownCodeActionError, "Unknown code action: #{@code_action[:title]}"
  end
end