Class: RubyLsp::Ree::CompletionItemsMapper

Inherits:
Object
  • Object
show all
Includes:
Requests::Support::Common, ReeLspUtils
Defined in:
lib/ruby_lsp/ruby_lsp_ree/completion/completion_items_mapper.rb

Constant Summary

Constants included from ReeLspUtils

ReeLspUtils::Entry

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(index) ⇒ CompletionItemsMapper

Returns a new instance of CompletionItemsMapper.



10
11
12
# File 'lib/ruby_lsp/ruby_lsp_ree/completion/completion_items_mapper.rb', line 10

def initialize(index)
  @index = index
end

Instance Method Details

#map_class_name_objects(class_name_objects, node, parsed_doc, ree_context) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ruby_lsp/ruby_lsp_ree/completion/completion_items_mapper.rb', line 93

def map_class_name_objects(class_name_objects, node, parsed_doc, ree_context)
  imported_consts = []
  not_imported_consts = []

  class_name_objects.each do |full_class_name|
    entries = @index[full_class_name]

    entries.each do |entry|
      class_name = full_class_name.split('::').last
      package_name = package_name_from_uri(entry.uri)
      file_name = File.basename(entry.uri.to_s)
      entry_comment = entry.comments && entry.comments.size > 0 ? " (#{entry.comments})" : ''

      matched_import = parsed_doc.find_import_for_package(class_name, package_name)

      if matched_import   
        label_details = Interface::CompletionItemLabelDetails.new(
          description: "imported from: :#{package_name}",
          detail: entry_comment
        )
        
        imported_consts << Interface::CompletionItem.new(
          label: class_name,
          label_details: label_details,
          filter_text: class_name,
          text_edit: Interface::TextEdit.new(
            range:  range_from_location(node.location),
            new_text: class_name,
          ),
          kind: Constant::CompletionItemKind::CLASS,
          additional_text_edits: []
        )
      else
        label_details = Interface::CompletionItemLabelDetails.new(
          description: "from: :#{package_name}",
          detail: entry_comment + " #{file_name}"
        )

        addition_edits = ree_context.is_link_object? ? [] : ConstAdditionalTextEditsCreator.call(parsed_doc, class_name, package_name, entry)
        not_imported_consts << Interface::CompletionItem.new(
          label: class_name,
          label_details: label_details,
          filter_text: class_name,
          text_edit: Interface::TextEdit.new(
            range:  range_from_location(node.location),
            new_text: class_name,
          ),
          kind: Constant::CompletionItemKind::CLASS,
          additional_text_edits: addition_edits
        )
      end
    end
  end

  imported_consts + not_imported_consts
end

#map_ree_object_methods(ree_object_methods, location, node, description) ⇒ Object



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
45
46
47
48
49
50
51
52
# File 'lib/ruby_lsp/ruby_lsp_ree/completion/completion_items_mapper.rb', line 14

def map_ree_object_methods(ree_object_methods, location, node, description)
  default_range = Interface::Range.new(
    start: Interface::Position.new(line: location.start_line - 1, character: location.end_column + 1),
    end: Interface::Position.new(line: location.start_line - 1, character: location.end_column + 1),
  )

  ree_object_methods.map do |object_method|
    signature = object_method.signatures&.first

    label_details = Interface::CompletionItemLabelDetails.new(
      description: description,
      detail: get_detail_string(signature)
    )

    if node.arguments && node.name.to_s == object_method.name
      new_text = object_method.name
      method_range = range_from_location(node.message_loc)
    else
      new_text = get_method_string(object_method.name, signature)
      method_range = default_range
    end

    Interface::CompletionItem.new(
      label: object_method.name,
      label_details: label_details,
      filter_text: object_method.name,
      text_edit: Interface::TextEdit.new(
        range:  method_range,
        new_text: new_text
      ),
      kind: Constant::CompletionItemKind::METHOD,
      insert_text_format: Constant::InsertTextFormat::SNIPPET,
      data: {
        owner_name: "Object",
        guessed_type: false,
      }
    )
  end
end

#map_ree_objects(ree_objects, node, parsed_doc) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ruby_lsp/ruby_lsp_ree/completion/completion_items_mapper.rb', line 54

def map_ree_objects(ree_objects, node, parsed_doc)
  ree_objects.map do |ree_object|
    ree_object_name = ree_object.name
    package_name = package_name_from_uri(ree_object.uri)
    signature = ree_object.signatures.first
    ree_type = get_ree_type(ree_object)

    label_details = Interface::CompletionItemLabelDetails.new(
      description: "#{ree_type}, from: :#{package_name}",
      detail: get_detail_string(signature)
    )

    if node.arguments && node.name.to_s == ree_object_name
      new_text = ree_object_name
      range = range_from_location(node.message_loc)
    else
      new_text = get_method_string(ree_object_name, signature)
      range = range_from_location(node.location)
    end

    Interface::CompletionItem.new(
      label: ree_object_name,
      label_details: label_details,
      filter_text: ree_object_name,
      text_edit: Interface::TextEdit.new(
        range:  range,
        new_text: new_text
      ),
      kind: Constant::CompletionItemKind::METHOD,
      insert_text_format: Constant::InsertTextFormat::SNIPPET,
      data: {
        owner_name: "Object",
        guessed_type: false,
      },
      additional_text_edits: MethodAdditionalTextEditsCreator.call(parsed_doc, ree_object_name, package_name)
    )
  end
end