Class: RubyLsp::Listeners::Completion

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Requests::Support::Common
Defined in:
lib/ruby_lsp/listeners/completion.rb

Constant Summary collapse

KEYWORDS =
[
  "alias",
  "and",
  "begin",
  "BEGIN",
  "break",
  "case",
  "class",
  "def",
  "defined?",
  "do",
  "else",
  "elsif",
  "end",
  "END",
  "ensure",
  "false",
  "for",
  "if",
  "in",
  "module",
  "next",
  "nil",
  "not",
  "or",
  "redo",
  "rescue",
  "retry",
  "return",
  "self",
  "super",
  "then",
  "true",
  "undef",
  "unless",
  "until",
  "when",
  "while",
  "yield",
  "__ENCODING__",
  "__FILE__",
  "__LINE__",
].freeze

Instance Method Summary collapse

Methods included from Requests::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?, #sorbet_level_true_or_higher?, #visible?

Constructor Details

#initialize(response_builder, global_state, node_context, sorbet_level, dispatcher, uri, trigger_character) ⇒ Completion

rubocop:disable Metrics/ParameterLists



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
92
93
94
95
# File 'lib/ruby_lsp/listeners/completion.rb', line 65

def initialize( # rubocop:disable Metrics/ParameterLists
  response_builder,
  global_state,
  node_context,
  sorbet_level,
  dispatcher,
  uri,
  trigger_character
)
  @response_builder = response_builder
  @global_state = global_state
  @index = T.let(global_state.index, RubyIndexer::Index)
  @type_inferrer = T.let(global_state.type_inferrer, TypeInferrer)
  @node_context = node_context
  @sorbet_level = sorbet_level
  @uri = uri
  @trigger_character = trigger_character

  dispatcher.register(
    self,
    :on_constant_path_node_enter,
    :on_constant_read_node_enter,
    :on_call_node_enter,
    :on_instance_variable_read_node_enter,
    :on_instance_variable_write_node_enter,
    :on_instance_variable_and_write_node_enter,
    :on_instance_variable_operator_write_node_enter,
    :on_instance_variable_or_write_node_enter,
    :on_instance_variable_target_node_enter,
  )
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ruby_lsp/listeners/completion.rb', line 134

def on_call_node_enter(node)
  # The only scenario where Sorbet doesn't provide constant completion is on ignored files. Even if the file has
  # no sigil, Sorbet will still provide completion for constants
  if @sorbet_level == RubyDocument::SorbetLevel::Ignore
    receiver = node.receiver

    # When writing `Foo::`, the AST assigns a method call node (because you can use that syntax to invoke
    # singleton methods). However, in addition to providing method completion, we also need to show possible
    # constant completions
    if (receiver.is_a?(Prism::ConstantReadNode) || receiver.is_a?(Prism::ConstantPathNode)) &&
        node.call_operator == "::"

      name = constant_name(receiver)

      if name
        start_loc = node.location
        end_loc = T.must(node.call_operator_loc)

        constant_path_completion(
          "#{name}::",
          Interface::Range.new(
            start: Interface::Position.new(line: start_loc.start_line - 1, character: start_loc.start_column),
            end: Interface::Position.new(line: end_loc.end_line - 1, character: end_loc.end_column),
          ),
        )
        return
      end
    end
  end

  name = node.message
  return unless name

  case name
  when "require"
    complete_require(node)
  when "require_relative"
    complete_require_relative(node)
  else
    complete_methods(node, name)
  end
end

#on_constant_path_node_enter(node) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/ruby_lsp/listeners/completion.rb', line 122

def on_constant_path_node_enter(node)
  # The only scenario where Sorbet doesn't provide constant completion is on ignored files. Even if the file has
  # no sigil, Sorbet will still provide completion for constants
  return if @sorbet_level != RubyDocument::SorbetLevel::Ignore

  name = constant_name(node)
  return if name.nil?

  constant_path_completion(name, range_from_location(node.location))
end

#on_constant_read_node_enter(node) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ruby_lsp/listeners/completion.rb', line 99

def on_constant_read_node_enter(node)
  # The only scenario where Sorbet doesn't provide constant completion is on ignored files. Even if the file has
  # no sigil, Sorbet will still provide completion for constants
  return if @sorbet_level != RubyDocument::SorbetLevel::Ignore

  name = constant_name(node)
  return if name.nil?

  candidates = @index.prefix_search(name, @node_context.nesting)
  candidates.each do |entries|
    complete_name = T.must(entries.first).name
    @response_builder << build_entry_completion(
      complete_name,
      name,
      range_from_location(node.location),
      entries,
      top_level?(complete_name),
    )
  end
end

#on_instance_variable_and_write_node_enter(node) ⇒ Object



188
189
190
# File 'lib/ruby_lsp/listeners/completion.rb', line 188

def on_instance_variable_and_write_node_enter(node)
  handle_instance_variable_completion(node.name.to_s, node.name_loc)
end

#on_instance_variable_operator_write_node_enter(node) ⇒ Object



193
194
195
# File 'lib/ruby_lsp/listeners/completion.rb', line 193

def on_instance_variable_operator_write_node_enter(node)
  handle_instance_variable_completion(node.name.to_s, node.name_loc)
end

#on_instance_variable_or_write_node_enter(node) ⇒ Object



198
199
200
# File 'lib/ruby_lsp/listeners/completion.rb', line 198

def on_instance_variable_or_write_node_enter(node)
  handle_instance_variable_completion(node.name.to_s, node.name_loc)
end

#on_instance_variable_read_node_enter(node) ⇒ Object



178
179
180
# File 'lib/ruby_lsp/listeners/completion.rb', line 178

def on_instance_variable_read_node_enter(node)
  handle_instance_variable_completion(node.name.to_s, node.location)
end

#on_instance_variable_target_node_enter(node) ⇒ Object



203
204
205
# File 'lib/ruby_lsp/listeners/completion.rb', line 203

def on_instance_variable_target_node_enter(node)
  handle_instance_variable_completion(node.name.to_s, node.location)
end

#on_instance_variable_write_node_enter(node) ⇒ Object



183
184
185
# File 'lib/ruby_lsp/listeners/completion.rb', line 183

def on_instance_variable_write_node_enter(node)
  handle_instance_variable_completion(node.name.to_s, node.name_loc)
end