Class: RubyLsp::Requests::References

Inherits:
Request
  • Object
show all
Includes:
Support::Common
Defined in:
lib/ruby_lsp/requests/references.rb

Overview

The [references](microsoft.github.io/language-server-protocol/specification#textDocument_references) request finds all references for the selected symbol.

Instance Method Summary collapse

Methods included from 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?

Constructor Details

#initialize(global_state, store, document, params) ⇒ References

: (GlobalState global_state, Store store, (RubyDocument | ERBDocument) document, Hash[Symbol, untyped] params) -> void



13
14
15
16
17
18
19
20
# File 'lib/ruby_lsp/requests/references.rb', line 13

def initialize(global_state, store, document, params)
  super()
  @global_state = global_state
  @store = store
  @document = document
  @params = params
  @locations = [] #: Array[Interface::Location]
end

Instance Method Details

#performObject

: -> Array



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
75
76
77
78
79
80
# File 'lib/ruby_lsp/requests/references.rb', line 24

def perform
  position = @params[:position]
  char_position, _ = @document.find_index_by_position(position)

  node_context = RubyDocument.locate(
    @document.ast,
    char_position,
    node_types: [
      Prism::ConstantReadNode,
      Prism::ConstantPathNode,
      Prism::ConstantPathTargetNode,
      Prism::InstanceVariableAndWriteNode,
      Prism::InstanceVariableOperatorWriteNode,
      Prism::InstanceVariableOrWriteNode,
      Prism::InstanceVariableReadNode,
      Prism::InstanceVariableTargetNode,
      Prism::InstanceVariableWriteNode,
      Prism::CallNode,
      Prism::DefNode,
    ],
    code_units_cache: @document.code_units_cache,
  )
  target = node_context.node
  parent = node_context.parent
  return @locations if !target || target.is_a?(Prism::ProgramNode)

  if target.is_a?(Prism::ConstantReadNode) && parent.is_a?(Prism::ConstantPathNode)
    target = determine_target(
      target,
      parent,
      position,
    )
  end

  target = target #: as Prism::ConstantReadNode | Prism::ConstantPathNode | Prism::ConstantPathTargetNode | Prism::InstanceVariableAndWriteNode | Prism::InstanceVariableOperatorWriteNode | Prism::InstanceVariableOrWriteNode | Prism::InstanceVariableReadNode | Prism::InstanceVariableTargetNode | Prism::InstanceVariableWriteNode | Prism::CallNode | Prism::DefNode,

  reference_target = create_reference_target(target, node_context)
  return @locations unless reference_target

  Dir.glob(File.join(@global_state.workspace_path, "**/*.rb")).each do |path|
    uri = URI::Generic.from_path(path: path)
    # If the document is being managed by the client, then we should use whatever is present in the store instead
    # of reading from disk
    next if @store.key?(uri)

    parse_result = Prism.parse_lex_file(path)
    collect_references(reference_target, parse_result, uri)
  rescue Errno::EISDIR, Errno::ENOENT
    # If `path` is a directory, just ignore it and continue. If the file doesn't exist, then we also ignore it.
  end

  @store.each do |_uri, document|
    collect_references(reference_target, document.parse_result, document.uri)
  end

  @locations
end