Class: RubyLsp::RubyDocument

Inherits:
Document show all
Extended by:
T::Generic, T::Sig
Defined in:
lib/ruby_lsp/ruby_document.rb

Defined Under Namespace

Classes: SorbetLevel

Constant Summary collapse

ParseResultType =
type_member { { fixed: Prism::ParseResult } }

Instance Attribute Summary

Attributes inherited from Document

#encoding, #parse_result, #source, #uri, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Document

#==, #cache_fetch, #cache_get, #cache_set, #create_scanner, #initialize, #push_edits

Constructor Details

This class inherits a constructor from RubyLsp::Document

Class Method Details

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



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
114
115
116
117
118
119
120
121
# File 'lib/ruby_lsp/ruby_document.rb', line 31

def locate(node, char_position, node_types: [])
  queue = T.let(node.child_nodes.compact, T::Array[T.nilable(Prism::Node)])
  closest = node
  parent = T.let(nil, T.nilable(Prism::Node))
  nesting_nodes = T.let(
    [],
    T::Array[T.any(
      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 = T.let(nil, T.nilable(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
    T.unsafe(queue).unshift(*candidate.child_nodes)

    # Skip if the current node doesn't cover the desired position
    loc = candidate.location
    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
    nesting_nodes.pop if previous_level && loc.start_offset > previous_level.location.end_offset

    # 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.start_offset...arg_loc.end_offset).cover?(char_position)) ||
          (blk_loc && (blk_loc.start_offset...blk_loc.end_offset).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
    if loc.end_offset - loc.start_offset <= closest_loc.end_offset - closest_loc.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

#language_idObject



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

def language_id
  LanguageId::Ruby
end

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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/ruby_lsp/ruby_document.rb', line 168

def locate_first_within_range(range, node_types: [])
  scanner = create_scanner
  start_position = scanner.find_char_position(range[:start])
  end_position = scanner.find_char_position(range[:end])
  desired_range = (start_position...end_position)
  queue = T.let(@parse_result.value.child_nodes.compact, T::Array[T.nilable(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
    T.unsafe(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



202
203
204
# File 'lib/ruby_lsp/ruby_document.rb', line 202

def locate_node(position, node_types: [])
  RubyDocument.locate(@parse_result.value, create_scanner.find_char_position(position), node_types: node_types)
end

#parseObject



125
126
127
128
129
130
# File 'lib/ruby_lsp/ruby_document.rb', line 125

def parse
  return @parse_result unless @needs_parsing

  @needs_parsing = false
  @parse_result = Prism.parse(@source)
end

#sorbet_levelObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ruby_lsp/ruby_document.rb', line 143

def sorbet_level
  sigil = parse_result.magic_comments.find do |comment|
    comment.key == "typed"
  end&.value

  case sigil
  when "ignore"
    SorbetLevel::Ignore
  when "false"
    SorbetLevel::False
  when "true"
    SorbetLevel::True
  when "strict", "strong"
    SorbetLevel::Strict
  else
    SorbetLevel::None
  end
end

#syntax_error?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/ruby_lsp/ruby_document.rb', line 133

def syntax_error?
  @parse_result.failure?
end