Class: RubyLsp::RailsPartial::Completion

Inherits:
Object
  • Object
show all
Includes:
Requests::Support::Common
Defined in:
lib/ruby_lsp/ruby_lsp_rails_partial/completion.rb

Overview

Completes partial names while typing the partial name string of a render call. Candidates are always offered in path form (e.g. “admin/areas/form”).

NOTE: A completion request dispatches only the single node directly under the cursor, and the target node types do not include StringNode. When the cursor is inside the string, what gets dispatched is the enclosing CallNode (render), so this is handled in ‘on_call_node_enter` rather than `on_string_node_enter`.

Instance Method Summary collapse

Constructor Details

#initialize(response_builder, node_context, resolver, _uri, dispatcher) ⇒ Completion

uri is part of the fixed listener signature but unused: candidates do not depend on the current file’s directory (always path form).



17
18
19
20
21
22
23
# File 'lib/ruby_lsp/ruby_lsp_rails_partial/completion.rb', line 17

def initialize(response_builder, node_context, resolver, _uri, dispatcher)
  @response_builder = response_builder
  @node_context = node_context
  @resolver = resolver

  dispatcher.register(self, :on_call_node_enter)
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_lsp/ruby_lsp_rails_partial/completion.rb', line 25

def on_call_node_enter(node)
  return unless Util.render_call?(node)

  string_node = Util.partial_string_argument(node)
  return unless string_node

  edit_range = content_range(string_node)
  prefix = string_node.unescaped

  @resolver.completion_candidates.each do |key, path|
    # label, new_text, and the implicit filter_text (defaults to label) are all the path-form
    # key, so the item survives the client-side filter regardless of how the user typed the
    # reference. Mirrors ruby-lsp's require completion (build_completion).
    next unless key.start_with?(prefix)

    @response_builder << Interface::CompletionItem.new(
      label: key,
      kind: Constant::CompletionItemKind::FILE,
      text_edit: Interface::TextEdit.new(range: edit_range, new_text: key),
      detail: relative_detail(path)
    )
  end
end