Class: RubyLanguageServer::ScopeData::Scope

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby_language_server/scope_data/scope.rb

Overview

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.

Constant Summary

Constants inherited from Base

Base::BLOCK_NAME, Base::JoinHash, Base::TYPE_BLOCK, Base::TYPE_CLASS, Base::TYPE_METHOD, Base::TYPE_MODULE, Base::TYPE_ROOT, Base::TYPE_VARIABLE

Instance Attribute Summary

Attributes inherited from Base

#type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#method?

Class Method Details

.build(parent = nil, type = TYPE_ROOT, name = '', top_line = 1, column = 1) ⇒ Object

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



25
26
27
28
29
30
31
32
33
34
35
# 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

Instance Method Details

#block_scope?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/ruby_language_server/scope_data/scope.rb', line 71

def block_scope?
  class_type == TYPE_BLOCK
end

#depthObject



41
42
43
44
45
# File 'lib/ruby_language_server/scope_data/scope.rb', line 41

def depth
  return 0 if path.blank?

  scope_parts.count
end

#descendantsObject



54
55
56
# File 'lib/ruby_language_server/scope_data/scope.rb', line 54

def descendants
  Scope.where('path like ?', "#{path}_%")
end

#full_nameObject



37
38
39
# File 'lib/ruby_language_server/scope_data/scope.rb', line 37

def full_name
  path # @full_name || @name
end

#named_scope?Boolean

Not a block or root

Returns:

  • (Boolean)


76
77
78
# 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?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/ruby_language_server/scope_data/scope.rb', line 67

def root_scope?
  class_type == TYPE_ROOT
end

#self_and_descendantsObject

Self and all descendents flattened into array



48
49
50
51
52
# 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) ⇒ Object



58
59
60
61
62
63
64
65
# 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