Class: RubyLsp::RubyDocument

Inherits:
Document show all
Defined in:
lib/ruby_lsp/ruby_document.rb

Overview

: [ParseResultType = Prism::ParseLexResult]

Constant Summary collapse

METHODS_THAT_CHANGE_DECLARATIONS =
[
  :private_constant,
  :attr_reader,
  :attr_writer,
  :attr_accessor,
  :alias_method,
  :include,
  :prepend,
  :extend,
  :public,
  :protected,
  :private,
  :module_function,
  :private_class_method,
].freeze

Constants inherited from Document

Document::EMPTY_CACHE, Document::MAXIMUM_CHARACTERS_FOR_EXPENSIVE_FEATURES

Instance Attribute Summary collapse

Attributes inherited from Document

#encoding, #last_edit, #parse_result, #semantic_tokens, #source, #uri, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Document

#==, #cache_fetch, #cache_get, #cache_set, #clear_cache, #find_index_by_position, #past_expensive_limit?, #push_edits

Constructor Details

#initialize(source:, version:, uri:, global_state:) ⇒ RubyDocument

: (source: String, version: Integer, uri: URI::Generic, global_state: GlobalState) -> void



120
121
122
123
124
# File 'lib/ruby_lsp/ruby_document.rb', line 120

def initialize(source:, version:, uri:, global_state:)
  super
  @code_units_cache = @parse_result
    .code_units_cache(@encoding) #: (^(Integer arg0) -> Integer | Prism::CodeUnitsCache)
end

Instance Attribute Details

#code_units_cacheObject (readonly)

: (^(Integer arg0) -> Integer | Prism::CodeUnitsCache)



117
118
119
# File 'lib/ruby_lsp/ruby_document.rb', line 117

def code_units_cache
  @code_units_cache
end

Class Method Details

.locate(node, char_position, code_units_cache:, node_types: []) ⇒ Object

: (Prism::Node node, Integer char_position, code_units_cache: (^(Integer arg0) -> Integer | Prism::CodeUnitsCache), ?node_types: Array) -> NodeContext



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ruby_lsp/ruby_document.rb', line 25

def locate(node, char_position, code_units_cache:, node_types: [])
  queue = node.child_nodes.compact #: Array[Prism::Node?]
  closest = node
  parent = nil #: Prism::Node?
  nesting_nodes = [] #: Array[(Prism::ClassNode | Prism::ModuleNode | Prism::SingletonClassNode | Prism::DefNode | Prism::BlockNode | Prism::LambdaNode | Prism::ProgramNode)]

  nesting_nodes << node if node.is_a?(Prism::ProgramNode)
  call_node = nil #: Prism::CallNode?

  until queue.empty?
    candidate = queue.shift

    # Skip nil child nodes
    next if candidate.nil?

    # Add the next child_nodes to the queue to be processed. The order here is important! We want to move in the
    # same order as the visiting mechanism, which means searching the child nodes before moving on to the next
    # sibling
    queue.unshift(*candidate.child_nodes)

    # Skip if the current node doesn't cover the desired position
    loc = candidate.location
    loc_start_offset = loc.cached_start_code_units_offset(code_units_cache)
    loc_end_offset = loc.cached_end_code_units_offset(code_units_cache)
    next unless (loc_start_offset...loc_end_offset).cover?(char_position)

    # If the node's start character is already past the position, then we should've found the closest node
    # already
    break if char_position < loc_start_offset

    # If the candidate starts after the end of the previous nesting level, then we've exited that nesting level
    # and need to pop the stack
    previous_level = nesting_nodes.last
    if previous_level &&
        (loc_start_offset > previous_level.location.cached_end_code_units_offset(code_units_cache))
      nesting_nodes.pop
    end

    # Keep track of the nesting where we found the target. This is used to determine the fully qualified name of
    # the target when it is a constant
    case candidate
    when Prism::ClassNode, Prism::ModuleNode, Prism::SingletonClassNode, Prism::DefNode, Prism::BlockNode,
      Prism::LambdaNode
      nesting_nodes << candidate
    end

    if candidate.is_a?(Prism::CallNode)
      arg_loc = candidate.arguments&.location
      blk_loc = candidate.block&.location
      if (arg_loc && (arg_loc.cached_start_code_units_offset(code_units_cache)...
                      arg_loc.cached_end_code_units_offset(code_units_cache)).cover?(char_position)) ||
          (blk_loc && (blk_loc.cached_start_code_units_offset(code_units_cache)...
                      blk_loc.cached_end_code_units_offset(code_units_cache)).cover?(char_position))
        call_node = candidate
      end
    end

    # If there are node types to filter by, and the current node is not one of those types, then skip it
    next if node_types.any? && node_types.none? { |type| candidate.class == type }

    # If the current node is narrower than or equal to the previous closest node, then it is more precise
    closest_loc = closest.location
    closest_node_start_offset = closest_loc.cached_start_code_units_offset(code_units_cache)
    closest_node_end_offset = closest_loc.cached_end_code_units_offset(code_units_cache)
    if loc_end_offset - loc_start_offset <= closest_node_end_offset - closest_node_start_offset
      parent = closest
      closest = candidate
    end
  end

  # When targeting the constant part of a class/module definition, we do not want the nesting to be duplicated.
  # That is, when targeting Bar in the following example:
  #
  # ```ruby
  #   class Foo::Bar; end
  # ```
  # The correct target is `Foo::Bar` with an empty nesting. `Foo::Bar` should not appear in the nesting stack,
  # even though the class/module node does indeed enclose the target, because it would lead to incorrect behavior
  if closest.is_a?(Prism::ConstantReadNode) || closest.is_a?(Prism::ConstantPathNode)
    last_level = nesting_nodes.last

    if (last_level.is_a?(Prism::ModuleNode) || last_level.is_a?(Prism::ClassNode)) &&
        last_level.constant_path == closest
      nesting_nodes.pop
    end
  end

  NodeContext.new(closest, parent, nesting_nodes, call_node)
end

Instance Method Details

#astObject

: -> Prism::ProgramNode



138
139
140
# File 'lib/ruby_lsp/ruby_document.rb', line 138

def ast
  @parse_result.value.first
end

#language_idObject

: -> Symbol



150
151
152
# File 'lib/ruby_lsp/ruby_document.rb', line 150

def language_id
  :ruby
end

#locate_first_within_range(range, node_types: []) ⇒ Object

: (Hash[Symbol, untyped] range, ?node_types: Array) -> Prism::Node?



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ruby_lsp/ruby_document.rb', line 155

def locate_first_within_range(range, node_types: [])
  start_position, end_position = find_index_by_position(range[:start], range[:end])

  desired_range = (start_position...end_position)
  queue = ast.child_nodes.compact #: Array[Prism::Node?]

  until queue.empty?
    candidate = queue.shift

    # Skip nil child nodes
    next if candidate.nil?

    # Add the next child_nodes to the queue to be processed. The order here is important! We want to move in the
    # same order as the visiting mechanism, which means searching the child nodes before moving on to the next
    # sibling
    queue.unshift(*candidate.child_nodes)

    # Skip if the current node doesn't cover the desired position
    loc = candidate.location

    if desired_range.cover?(loc.start_offset...loc.end_offset) &&
        (node_types.empty? || node_types.any? { |type| candidate.class == type })
      return candidate
    end
  end
end

#locate_node(position, node_types: []) ⇒ Object

: (Hash[Symbol, untyped] position, ?node_types: Array) -> NodeContext



183
184
185
186
187
188
189
190
191
192
# File 'lib/ruby_lsp/ruby_document.rb', line 183

def locate_node(position, node_types: [])
  char_position, _ = find_index_by_position(position)

  RubyDocument.locate(
    ast,
    char_position,
    code_units_cache: @code_units_cache,
    node_types: node_types,
  )
end

#parse!Object

: -> bool



128
129
130
131
132
133
134
135
# File 'lib/ruby_lsp/ruby_document.rb', line 128

def parse!
  return false unless @needs_parsing

  @needs_parsing = false
  @parse_result = Prism.parse_lex(@source)
  @code_units_cache = @parse_result.code_units_cache(@encoding)
  true
end

#should_index?Boolean

: -> bool

Returns:

  • (Boolean)


195
196
197
198
199
200
201
# File 'lib/ruby_lsp/ruby_document.rb', line 195

def should_index?
  # This method controls when we should index documents. If there's no recent edit and the document has just been
  # opened, we need to index it
  return true unless @last_edit

  last_edit_may_change_declarations?
end

#syntax_error?Boolean

: -> bool

Returns:

  • (Boolean)


144
145
146
# File 'lib/ruby_lsp/ruby_document.rb', line 144

def syntax_error?
  @parse_result.failure?
end