Skip to content

Commit

Permalink
Clean up and simplify a bit and make completion work again.
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Jul 10, 2010
1 parent e12d437 commit 97370a1
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 55 deletions.
1 change: 1 addition & 0 deletions bin/dietrb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ IRB.formatter.filter_from_backtrace << /^#{__FILE__}/

if ARGV.empty?
irb(self, TOPLEVEL_BINDING.dup)
# TODO make sure!
puts "ENDED! and this message should show up on stdout"
else
path = ARGV.shift
Expand Down
4 changes: 2 additions & 2 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

# require 'irb/io'
# require 'irb/driver/tty'
# require 'irb/driver/readline'
require 'irb/driver/socket'
require 'irb/driver/readline'
# require 'irb/driver/socket'

require 'irb/source'
require 'irb/version'
Expand Down
5 changes: 4 additions & 1 deletion lib/irb/driver/readline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ def initialize(input = $stdin, output = $stdout)
super
::Readline.input = @input
::Readline.output = @output
::Readline.completion_proc = IRB::Completion.new
end

def readline
# Assigns a context to the completion object and waits for input.
def readline(context)
::Readline.completion_proc.context = context
::Readline.readline(context.prompt, true)
end
end
Expand Down
1 change: 0 additions & 1 deletion lib/irb/driver/socket.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# require 'irb/driver/readline'
require 'irb/driver/tty'
require 'socket'

Expand Down
49 changes: 9 additions & 40 deletions lib/irb/driver/tty.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
module IRB
# class Context
# class << self
# # attr_accessor :current
# def current
# Thread.current[:context]
# end
#
# def current=(context)
# Thread.current[:context] = context
# end
#
# # TODO move into driver
# # def make_current(context)
# # before, self.current = self.current, context
# # yield
# # ensure
# # self.current = before
# # end
# end
# end

class << self
attr_accessor :driver_class

Expand All @@ -36,7 +15,6 @@ def driver
break if driver = thread[:irb_driver]
end
end
p driver
driver || driver_class.new
end
end
Expand Down Expand Up @@ -102,16 +80,16 @@ def initialize(input = $stdin, output = $stdout)
@thread_group.add(Thread.current)
end

def readline
@output.print(current_context.prompt)
def readline(context)
@output.print(context.prompt)
@input.gets
end

# TODO make it take the current context instead of storing it
def consume
readline
def consume(context)
readline(context)
rescue Interrupt
current_context.clear_buffer
context.clear_buffer
""
end

Expand All @@ -126,23 +104,14 @@ def print(*args)

def run(context)
ensure_output_redirector do
make_current(context) do
while line = consume
continue = context.process_line(line)
break unless continue
end
context.driver = self
while line = consume(context)
continue = context.process_line(line)
break unless continue
end
end
end

def make_current(context)
context.driver = self
before, self.current_context = self.current_context, context
yield
ensure
self.current_context = before
end

# Ensure that the standard output object is a OutputRedirector. If it's
# already a OutputRedirector, do nothing.
def ensure_output_redirector
Expand Down
32 changes: 21 additions & 11 deletions lib/irb/ext/completion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,28 @@ def defined do
yield
}

# Returns an array of possible completion results, with the current
# IRB::Context.
#
# This is meant to be used with Readline which takes a completion proc.
def self.call(source)
new(IRB::Context.current, source).results
# # Returns an array of possible completion results, with the current
# # IRB::Context.
# #
# # This is meant to be used with Readline which takes a completion proc.
# def self.call(source)
# new(IRB::Context.current, source).results
# end

attr_reader :source
attr_accessor :context

# def initialize(context, source)
# @context, @source = context, source
# end

def initialize(context = nil)
@context = context
end

attr_reader :context, :source

def initialize(context, source)
@context, @source = context, source
def call(source)
@source = source
results
end

def evaluate(s)
Expand Down Expand Up @@ -190,5 +200,5 @@ def methods_of_object_in_variable(path)
# * Hash: = and >
Readline.basic_word_break_characters= " \t\n`<;|&("
end
Readline.completion_proc = IRB::Completion
# Readline.completion_proc = IRB::Completion
end

0 comments on commit 97370a1

Please sign in to comment.