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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_language_server/scope_data/scope.rb', line 27

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:,
    top_line:,
    column:,
    name:,
    path: full_name,
    class_type: type
  )
end

Instance Method Details

#block_scope?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/ruby_language_server/scope_data/scope.rb', line 73

def block_scope?
  class_type == TYPE_BLOCK
end

#close(line) ⇒ Object

Called from ScopeParser when a peer of this block starts - because we don’t have an end notifier. So we do some reasonable cleanup here.



84
85
86
87
88
89
90
# File 'lib/ruby_language_server/scope_data/scope.rb', line 84

def close(line)
  return destroy! if block_scope? && variables.none?

  self.top_line ||= variables.map(&:top_line).min
  self.bottom_line = [bottom_line, line].compact.min
  save!
end

#depthObject



43
44
45
46
47
# File 'lib/ruby_language_server/scope_data/scope.rb', line 43

def depth
  return 0 if path.blank?

  scope_parts.count
end

#descendantsObject



56
57
58
# File 'lib/ruby_language_server/scope_data/scope.rb', line 56

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

#full_nameObject



39
40
41
# File 'lib/ruby_language_server/scope_data/scope.rb', line 39

def full_name
  path # @full_name || @name
end

#named_scope?Boolean

Not a block or root

Returns:

  • (Boolean)


78
79
80
# File 'lib/ruby_language_server/scope_data/scope.rb', line 78

def named_scope?
  [TYPE_MODULE, TYPE_CLASS, TYPE_METHOD, TYPE_VARIABLE].include?(class_type)
end

#root_scope?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/ruby_language_server/scope_data/scope.rb', line 69

def root_scope?
  class_type == TYPE_ROOT
end

#self_and_descendantsObject

Self and all descendents flattened into array



50
51
52
53
54
# File 'lib/ruby_language_server/scope_data/scope.rb', line 50

def self_and_descendants
  return Scope.all if root_scope?

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

#set_superclass_name(partial) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/ruby_language_server/scope_data/scope.rb', line 60

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