Class: RubyLsp::Requests::PrepareRename

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

Overview

The [prepare_rename](microsoft.github.io/language-server-protocol/specification#textDocument_prepareRename) # request checks the validity of a rename operation at a given location.

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, position) ⇒ PrepareRename

: (RubyDocument document, Hash[Symbol, untyped] position) -> void



13
14
15
16
17
# File 'lib/ruby_lsp/requests/prepare_rename.rb', line 13

def initialize(document, position)
  super()
  @document = document
  @position = position #: Hash[Symbol, Integer]
end

Instance Method Details

#performObject

: -> Interface::Range?



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ruby_lsp/requests/prepare_rename.rb', line 21

def perform
  char_position, _ = @document.find_index_by_position(@position)

  node_context = RubyDocument.locate(
    @document.ast,
    char_position,
    node_types: [Prism::ConstantReadNode, Prism::ConstantPathNode, Prism::ConstantPathTargetNode],
    code_units_cache: @document.code_units_cache,
  )
  target = node_context.node
  parent = node_context.parent
  return if !target || target.is_a?(Prism::ProgramNode)

  if target.is_a?(Prism::ConstantReadNode) && parent.is_a?(Prism::ConstantPathNode)
    target = determine_target(
      target,
      parent,
      @position,
    )
  end

  range_from_location(target.location)
end