Class: RubyLsp::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_lsp/scope.rb

Defined Under Namespace

Classes: Local

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Scope

: (?Scope? parent) -> void



10
11
12
13
14
15
# File 'lib/ruby_lsp/scope.rb', line 10

def initialize(parent = nil)
  @parent = parent

  # A hash of name => type
  @locals = {} #: Hash[Symbol, Local]
end

Instance Attribute Details

#parentObject (readonly)

: Scope?



7
8
9
# File 'lib/ruby_lsp/scope.rb', line 7

def parent
  @parent
end

Instance Method Details

#add(name, type) ⇒ Object

Add a new local to this scope. The types should only be ‘:parameter` or `:variable` : ((String | Symbol) name, Symbol type) -> void



19
20
21
# File 'lib/ruby_lsp/scope.rb', line 19

def add(name, type)
  @locals[name.to_sym] = Local.new(type)
end

#lookup(name) ⇒ Object

: ((String | Symbol) name) -> Local?



24
25
26
27
28
29
30
31
# File 'lib/ruby_lsp/scope.rb', line 24

def lookup(name)
  sym = name.to_sym
  entry = @locals[sym]
  return entry if entry
  return unless @parent

  @parent.lookup(sym)
end