Class: RubyLsp::Requests::CodeActionResolve

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

Overview

Code action resolve demo

The code action 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.

Example: Extract to variable

# Before:
1 + 1 # Select the text and use Refactor: Extract Variable

# After:
new_variable = 1 + 1
new_variable

Defined Under Namespace

Classes: CodeActionError, Error

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?, #sorbet_level_true_or_higher?, #visible?

Constructor Details

#initialize(document, code_action) ⇒ CodeActionResolve

Returns a new instance of CodeActionResolve.



42
43
44
45
46
# File 'lib/ruby_lsp/requests/code_action_resolve.rb', line 42

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

Instance Method Details

#performObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ruby_lsp/requests/code_action_resolve.rb', line 49

def perform
  return Error::EmptySelection 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
  else
    Error::UnknownCodeAction
  end
end