Class: RubyLsp::NodeContext

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/ruby_lsp/node_context.rb

Overview

This class allows listeners to access contextual information about a node in the AST, such as its parent, its namespace nesting, and the surrounding CallNode (e.g. a method call).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node, parent, nesting_nodes, call_node) ⇒ NodeContext

Returns a new instance of NodeContext.



38
39
40
41
42
43
44
45
46
47
# File 'lib/ruby_lsp/node_context.rb', line 38

def initialize(node, parent, nesting_nodes, call_node)
  @node = node
  @parent = parent
  @nesting_nodes = nesting_nodes
  @call_node = call_node

  nesting, surrounding_method = handle_nesting_nodes(nesting_nodes)
  @nesting = T.let(nesting, T::Array[String])
  @surrounding_method = T.let(surrounding_method, T.nilable(String))
end

Instance Attribute Details

#call_nodeObject (readonly)

Returns the value of attribute call_node.



17
18
19
# File 'lib/ruby_lsp/node_context.rb', line 17

def call_node
  @call_node
end

#nestingObject (readonly)

Returns the value of attribute nesting.



14
15
16
# File 'lib/ruby_lsp/node_context.rb', line 14

def nesting
  @nesting
end

#nodeObject (readonly)

Returns the value of attribute node.



11
12
13
# File 'lib/ruby_lsp/node_context.rb', line 11

def node
  @node
end

#parentObject (readonly)

Returns the value of attribute parent.



11
12
13
# File 'lib/ruby_lsp/node_context.rb', line 11

def parent
  @parent
end

#surrounding_methodObject (readonly)

Returns the value of attribute surrounding_method.



20
21
22
# File 'lib/ruby_lsp/node_context.rb', line 20

def surrounding_method
  @surrounding_method
end

Instance Method Details

#fully_qualified_nameObject



50
51
52
# File 'lib/ruby_lsp/node_context.rb', line 50

def fully_qualified_name
  @fully_qualified_name ||= T.let(@nesting.join("::"), T.nilable(String))
end

#locals_for_scopeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_lsp/node_context.rb', line 55

def locals_for_scope
  locals = []

  @nesting_nodes.each do |node|
    if node.is_a?(Prism::ClassNode) || node.is_a?(Prism::ModuleNode) || node.is_a?(Prism::SingletonClassNode) ||
        node.is_a?(Prism::DefNode)
      locals.clear
    end

    locals.concat(node.locals)
  end

  locals
end