Skip to content

Commit

Permalink
Merge pull request #205 from sh19910711/patch/0037/auto-complete
Browse files Browse the repository at this point in the history
Add auto-completion feature
  • Loading branch information
gsamokovarov authored Jul 18, 2016
2 parents fdbd98c + 2be719d commit f37806d
Show file tree
Hide file tree
Showing 18 changed files with 528 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ matrix:
- env: TEST_SUITE=test:templates
include:
- env: TEST_SUITE=test:templates
rvm: 2.2
rvm: 2.2.5
1 change: 1 addition & 0 deletions lib/web_console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module WebConsole
autoload :Whitelist
autoload :Template
autoload :Middleware
autoload :Context

autoload_at 'web_console/errors' do
autoload :Error
Expand Down
46 changes: 46 additions & 0 deletions lib/web_console/context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module WebConsole
# A context lets you get object names related to the current session binding.
class Context
def initialize(binding)
@binding = binding
end

# Extracts entire objects which can be called by the current session unless the objpath is present.
# Otherwise, it extracts methods and constants of the object specified by the objpath.
def extract(objpath)
if objpath.present?
local(objpath)
else
global
end
end

private

GLOBAL_OBJECTS = [
'global_variables',
'local_variables',
'instance_variables',
'instance_methods',
'class_variables',
'methods',
'Object.constants',
'Kernel.methods',
]

def global
GLOBAL_OBJECTS.map { |cmd| eval(cmd) }.flatten
end

def local(objpath)
[
eval("#{objpath}.methods").map { |m| "#{objpath}.#{m}" },
eval("#{objpath}.constants").map { |c| "#{objpath}::#{c}" },
].flatten
end

def eval(cmd)
@binding.eval(cmd) rescue []
end
end
end
2 changes: 1 addition & 1 deletion lib/web_console/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def id_for_repl_session_stack_frame_change(request)

def update_repl_session(id, request)
json_response_with_session(id, request) do |session|
{ output: session.eval(request.params[:input]) }
{ output: session.eval(request.params[:input]), context: session.context(request.params[:context]) }
end
end

Expand Down
9 changes: 7 additions & 2 deletions lib/web_console/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def from(storage)
def initialize(bindings)
@id = SecureRandom.hex(16)
@bindings = bindings
@evaluator = Evaluator.new(bindings.first)
@evaluator = Evaluator.new(@current_binding = bindings.first)

store_into_memory
end
Expand All @@ -59,7 +59,12 @@ def eval(input)
#
# Returns nothing.
def switch_binding_to(index)
@evaluator = Evaluator.new(@bindings[index.to_i])
@evaluator = Evaluator.new(@current_binding = @bindings[index.to_i])
end

# Returns context of the current binding
def context(objpath)
Context.new(@current_binding).extract(objpath)
end

private
Expand Down
24 changes: 15 additions & 9 deletions lib/web_console/tasks/test_templates.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ namespace :test do
task templates: "templates:all"

namespace :templates do
task all: [ :daemonize, :npm, :rackup, :wait, :mocha, :kill, :exit ]
task all: [ :daemonize, :npm, :rackup, :wait, :spec, :test, :kill, :exit ]
task serve: [ :npm, :rackup ]

work_dir = Pathname(EXPANDED_CWD).join("test/templates")
workdir = Pathname(EXPANDED_CWD).join("test/templates")
pid_file = Pathname(Dir.tmpdir).join("web_console.#{SecureRandom.uuid}.pid")
runner_uri = URI.parse("http://localhost:29292/html/spec_runner.html")
rackup_opts = "-p #{runner_uri.port}"
html_uri = URI.parse("http://#{ENV['IP'] || '127.0.0.1'}:#{ENV['PORT'] || 29292}/html/")
spec_runner = 'spec_runner.html'
test_runner = 'test_runner.html'
rackup_opts = "--host #{html_uri.host} --port #{html_uri.port}"
test_result = nil

def need_to_wait?(uri)
Expand All @@ -23,20 +25,24 @@ namespace :test do
end

task :npm do
Dir.chdir(work_dir) { system "npm install --silent" }
Dir.chdir(workdir) { system "npm install --silent" }
end

task :rackup do
Dir.chdir(work_dir) { system "bundle exec rackup #{rackup_opts}" }
Dir.chdir(workdir) { system "bundle exec rackup #{rackup_opts}" }
end

task :wait do
cnt = 0
need_to_wait?(runner_uri) { sleep 1; cnt += 1; cnt < 5 }
need_to_wait?(URI.join(html_uri, spec_runner)) { sleep 1; cnt += 1; cnt < 5 }
end

task :mocha do
Dir.chdir(work_dir) { test_result = system("$(npm bin)/mocha-phantomjs #{runner_uri}") }
task :spec do
Dir.chdir(workdir) { test_result = system("./node_modules/.bin/mocha-phantomjs #{URI.join(html_uri, spec_runner)}") }
end

task :test do
Dir.chdir(workdir) { test_result = system("./node_modules/.bin/mocha-phantomjs #{URI.join(html_uri, test_runner)}") }
end

task :kill do
Expand Down
Loading

0 comments on commit f37806d

Please sign in to comment.