Class: RubyLsp::Ree::ConstAdditionalTextEditsCreator

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

Constant Summary

Constants included from ReeLspUtils

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

Constructor Details

#initialize(parsed_doc, const_name, package_name, entry) ⇒ ConstAdditionalTextEditsCreator

Returns a new instance of ConstAdditionalTextEditsCreator.



10
11
12
13
14
15
# File 'lib/ruby_lsp/ruby_lsp_ree/completion/const_additional_text_edits_creator.rb', line 10

def initialize(parsed_doc, const_name, package_name, entry)
  @parsed_doc = parsed_doc
  @const_name = const_name
  @package_name = package_name
  @entry = entry
end

Class Method Details

.call(parsed_doc, const_name, package_name, entry) ⇒ Object



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

def self.call(parsed_doc, const_name, package_name, entry)
  new(parsed_doc, const_name, package_name, entry).call
end

Instance Method Details

#callObject



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_lsp/ruby_lsp_ree/completion/const_additional_text_edits_creator.rb', line 17

def call
  if @parsed_doc.includes_linked_constant?(@const_name)
    return []
  end

  const_link = get_constant_link

  if existing_link = @parsed_doc.find_link_node(const_link[:object_name])
    # attach to existing link
    if existing_link.has_import_section?
      new_text = "& #{@const_name} }"
      position = existing_link.location.end_column - 1
      range = Interface::Range.new(
        start: Interface::Position.new(line: existing_link.location.start_line-1, character: position),
        end: Interface::Position.new(line: existing_link.location.start_line-1, character: position + new_text.size),
      )
    else
      if existing_link.object_name_type?
        new_text = ", import: -> { #{@const_name} }"
      else
        new_text = ", -> { #{@const_name} }"
      end

      position = existing_link.location.end_column + 1
      range = Interface::Range.new(
        start: Interface::Position.new(line: existing_link.location.start_line-1, character: position),
        end: Interface::Position.new(line: existing_link.location.start_line-1, character: position + new_text.size),
      )
    end          
  else
    # add new link

    link_text = if const_link[:link_type] == :file_path
      "\s\slink #{const_link[:link_name]}, -> { #{@const_name} }"
    else
      "\s\slink #{const_link[:link_name]}, import: -> { #{@const_name} }"
    end

    if @parsed_doc.links_container_node
      link_text = "\s\s" + link_text
    end

    new_text = "\n" + link_text

    if @parsed_doc.has_blank_links_container?
      new_text = "\sdo#{link_text}\n\s\send\n"
    end

    range = get_range_for_fn_insert(@parsed_doc, link_text)
  end

  [
    Interface::TextEdit.new(
      range:    range,
      new_text: new_text,
    )
  ]
end