Class: RubyLsp::Ree::ReeRenameHandler

Inherits:
Object
  • Object
show all
Includes:
ReeLspUtils
Defined in:
lib/ruby_lsp/ruby_lsp_ree/ree_rename_handler.rb

Constant Summary

Constants included from ReeLspUtils

RubyLsp::Ree::ReeLspUtils::Entry

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReeLspUtils

#camelize, #find_local_file_path, #get_range_for_fn_insert, #get_ree_type, #get_uri_path, #package_name_from_spec_uri, #package_name_from_uri, #package_path_from_uri, #parameter_name, #path_from_package_folder, #signature_params_from_node, #spec_relative_file_path_from_uri, #underscore

Class Method Details

.call(changes) ⇒ Object



6
7
8
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_rename_handler.rb', line 6

def self.call(changes)
  new.call(changes)
end

Instance Method Details

#call(changes) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_lsp/ruby_lsp_ree/ree_rename_handler.rb', line 10

def call(changes)
  old_uri = URI.parse(changes.detect{ _1[:type] == Constant::FileChangeType::DELETED }[:uri])
  new_uri = URI.parse(changes.detect{ _1[:type] == Constant::FileChangeType::CREATED }[:uri])

  old_path = get_uri_path(old_uri)
  new_path = get_uri_path(new_uri)

  old_file_name = File.basename(old_path, '.rb').chomp(" copy")
  new_file_name = File.basename(new_path, '.rb')

  return if old_file_name == new_file_name

  parsed_doc = RubyLsp::Ree::ParsedDocumentBuilder.build_from_uri(new_uri)

  return if !parsed_doc || !parsed_doc.class_node

  old_class_name = old_file_name.split('_').collect(&:capitalize).join
  new_class_name = new_file_name.split('_').collect(&:capitalize).join

  return unless parsed_doc.class_name == old_class_name

  file_content_lines = File.read(new_path).lines
  
  class_line = parsed_doc.class_node.location.start_line - 1

  file_content_lines[class_line].gsub!(/\b#{old_class_name}\b/, new_class_name)

  if parsed_doc.links_container_node
    links_container_node_line = parsed_doc.links_container_node.location.start_line - 1

    file_content_lines[links_container_node_line].gsub!(/\b#{old_file_name}\b/, new_file_name)
  end

  File.write(new_path, file_content_lines.join)
end