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)


93
94
95
# File 'lib/ruby_language_server/scope_data/scope.rb', line 93

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



64
65
66
# File 'lib/ruby_language_server/scope_data/scope.rb', line 64

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

#root_scope?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/ruby_language_server/scope_data/scope.rb', line 89

def root_scope?
  class_type == TYPE_ROOT
end

#self_and_descendantsObject

Self and all descendents flattened into array



58
59
60
61
62
# File 'lib/ruby_language_server/scope_data/scope.rb', line 58

def self_and_descendants
  return Scope.all if root_scope?

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

#set_superclass_name(partial) ⇒ Object

self, parent, parent.parent…

def self_and_ancestors

return [self] if path.blank?
remaining_path = path.dup
ancestor_paths = scope_parts.inject([]) do |ary, scope_part|
  ary << remaining_path
  remaining_path =
  ary
end
[self, parent&.self_and_ancestors].flatten.compact

end



80
81
82
83
84
85
86
87
# File 'lib/ruby_language_server/scope_data/scope.rb', line 80

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