class RubyLanguageServer::ScopeData::Scope

The Scope class is basically a container with context. It is used to track top & bottom line, variables in this scope, constants, and children - which could be functions, classes, blocks, etc. Anything that adds scope.

Public Class Methods

build(parent = nil, type = TYPE_ROOT, name = '', top_line = 1, column = 1) click to toggle source

attr_accessor :top_line # first line attr_accessor :bottom_line # last line attr_accessor :parent # parent scope attr_accessor :constants # constants declared in this scope attr_accessor :name # method attr_accessor :superclass_name # superclass name

# File lib/ruby_language_server/scope_data/scope.rb, line 25
def self.build(parent = nil, type = TYPE_ROOT, name = '', top_line = 1, column = 1)
  full_name = [parent ? parent.full_name : nil, name].compact.join(JoinHash[type])
  create!(
    parent: parent,
    top_line: top_line,
    column: column,
    name: name,
    path: full_name,
    class_type: type
  )
end

Public Instance Methods

block_scope?() click to toggle source
# File lib/ruby_language_server/scope_data/scope.rb, line 71
def block_scope?
  class_type == TYPE_BLOCK
end
depth() click to toggle source
# File lib/ruby_language_server/scope_data/scope.rb, line 41
def depth
  return 0 if path.blank?

  scope_parts.count
end
descendants() click to toggle source
# File lib/ruby_language_server/scope_data/scope.rb, line 54
def descendants
  Scope.where('path like ?', "#{path}_%")
end
full_name() click to toggle source
# File lib/ruby_language_server/scope_data/scope.rb, line 37
def full_name
  path # @full_name || @name
end
named_scope?() click to toggle source

Not a block or root

# File lib/ruby_language_server/scope_data/scope.rb, line 76
def named_scope?
  [TYPE_MODULE, TYPE_CLASS, TYPE_METHOD, TYPE_VARIABLE].include?(class_type)
end
root_scope?() click to toggle source
# File lib/ruby_language_server/scope_data/scope.rb, line 67
def root_scope?
  class_type == TYPE_ROOT
end
self_and_descendants() click to toggle source

Self and all descendents flattened into array

# File lib/ruby_language_server/scope_data/scope.rb, line 48
def self_and_descendants
  return Scope.all if root_scope?

  Scope.where('path like ?', "#{path}%")
end
set_superclass_name(partial) click to toggle source
# File lib/ruby_language_server/scope_data/scope.rb, line 58
def set_superclass_name(partial)
  if partial.start_with?('::')
    self.superclass_name = partial.gsub(/^::/, '')
  else
    self.superclass_name = [parent ? parent.full_name : nil, partial].compact.join(JoinHash[class_type])
  end
  save!
end

Private Instance Methods

scope_parts() click to toggle source
# File lib/ruby_language_server/scope_data/scope.rb, line 82
def scope_parts
  path&.split(/#{JoinHash.values.reject(&:blank?).uniq.join('|')}/)
end