Class: RubyLsp::Requests::CodeActionResolve
- Inherits:
-
BaseRequest
- Object
- SyntaxTree::Visitor
- BaseRequest
- RubyLsp::Requests::CodeActionResolve
- Extended by:
- T::Sig
- Defined in:
- lib/ruby_lsp/requests/code_action_resolve.rb
Overview
![Code action resolve demo](../../misc/code_action_resolve.gif)
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.
# Example: Extract to variable
“‘ruby # 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"
Instance Method Summary collapse
-
#initialize(document, code_action) ⇒ CodeActionResolve
constructor
A new instance of CodeActionResolve.
- #run ⇒ Object
Methods inherited from BaseRequest
#full_constant_name, #locate, #range_from_syntax_tree_node, #visible?
Constructor Details
#initialize(document, code_action) ⇒ CodeActionResolve
Returns a new instance of CodeActionResolve.
37 38 39 40 41 |
# File 'lib/ruby_lsp/requests/code_action_resolve.rb', line 37 def initialize(document, code_action) super(document) @code_action = code_action end |
Instance Method Details
#run ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/ruby_lsp/requests/code_action_resolve.rb', line 44 def run source_range = @code_action.dig(:data, :range) return Error::EmptySelection if source_range[:start] == source_range[:end] scanner = @document.create_scanner start_index = scanner.find_char_position(source_range[:start]) end_index = scanner.find_char_position(source_range[:end]) extraction_source = T.must(@document.source[start_index...end_index]) source_line_indentation = T.must(T.must(@document.source.lines[source_range.dig(:start, :line)])[/\A */]).size Interface::CodeAction.new( title: "Refactor: Extract Variable", edit: Interface::WorkspaceEdit.new( document_changes: [ Interface::TextDocumentEdit.new( text_document: Interface::OptionalVersionedTextDocumentIdentifier.new( uri: @code_action.dig(:data, :uri), version: nil, ), edits: edits_to_extract_variable(source_range, extraction_source, source_line_indentation), ), ], ), ) end |