Class: RiderServer::Workspace

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/rider_server/workspace.rb

Instance Method Summary collapse

Methods included from Logger

configure_logger, log, #log

Constructor Details

#initializeWorkspace

Returns a new instance of Workspace.



19
20
21
22
# File 'lib/rider_server/workspace.rb', line 19

def initialize
  @binding_name = "main"
  @binding = TOPLEVEL_BINDING
end

Instance Method Details

#binding_local_variable_set(name, value, ns = @binding) ⇒ Object



55
56
57
# File 'lib/rider_server/workspace.rb', line 55

def binding_local_variable_set(name, value, ns = @binding)
  ns.local_variable_set(name.to_sym, value)
end

#binding_name(obj) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/rider_server/workspace.rb', line 103

def binding_name(obj)
  if obj.is_a?(Class) || obj.is_a?(Module)
    obj.name
  else
    obj.to_s
  end
end

#create_binding(obj) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rider_server/workspace.rb', line 91

def create_binding(obj)
  if obj.is_a?(Binding)
    [obj, obj.to_s]
  elsif obj.is_a?(Class)
    [obj.class_eval("binding", __FILE__, __LINE__), obj.name]
  elsif obj.is_a?(Module)
    [obj.module_eval("binding", __FILE__, __LINE__), obj.name]
  else
    [obj.instance_eval("binding", __FILE__, __LINE__), obj.to_s]
  end
end

#evaluate(statements, ns = @binding, file = "", line = 0) ⇒ Object

Evaluate the given statements in the context of the workspace See ruby-hacking-guide.github.io/anyeval.html



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rider_server/workspace.rb', line 39

def evaluate(statements, ns = @binding, file = "", line = 0)
  ns = if ns.nil?
    @binding
  elsif ns.is_a?(String) && ns.empty? || ns == "main"
    TOPLEVEL_BINDING
  elsif ns.is_a?(String)
    ns, _ = create_binding(lookup_module(ns))
    ns
  else # ns is a class, module or instance
    ns, _ = create_binding(ns)
    ns
  end

  eval(statements, ns, file, line) # rubocop:disable Security/Eval
end

#lookup_module(module_name, klass = ::Object) ⇒ Object

Lookup a module by a namespace for example ::RiderServer::Workspace the defaulting starting module is Object. ruby-doc.org/3.2.2/Module.html#method-i-const_get



62
63
64
65
66
67
68
# File 'lib/rider_server/workspace.rb', line 62

def lookup_module(module_name, klass = ::Object)
  if module_name.empty?
    klass
  else
    lookup_module_ast(parse_string(module_name), klass)
  end
end

#lookup_module_ast(node, klass = ::Object) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rider_server/workspace.rb', line 76

def lookup_module_ast(node, klass = ::Object)
  case node.type
  when :cbase
    ::Object
  when :const
    if node.children.first.nil?
      klass.const_get(node.children.last)
    else
      lookup_module_ast(node.children.first, klass).const_get(node.children.last)
    end
  else
    raise ModuleLookupError, "Unknown node type #{node.type}"
  end
end

#namespace_nameObject



33
34
35
# File 'lib/rider_server/workspace.rb', line 33

def namespace_name
  @binding_name
end

#parse_string(string) ⇒ Object



70
71
72
73
74
# File 'lib/rider_server/workspace.rb', line 70

def parse_string(string)
  buffer = Parser::Source::Buffer.new("(string)")
  buffer.source = string
  Parser::CurrentRuby.new.parse(buffer)
end

#set_namespace(namespace) ⇒ Object



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

def set_namespace(namespace)
  if namespace == "main"
    @binding_name = "main"
    @binding = TOPLEVEL_BINDING
  else
    @binding, @binding_name = create_binding(namespace)
  end
end