Skip to content

Commit

Permalink
Move auto_indent option to IRB::Formatter
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.macosforge.org/repository/ruby/DietRB/trunk@4856 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
alloy committed Oct 31, 2010
1 parent 01fdf4c commit b5bc8e3
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
* Move auto_indent option to formatter
* Add auto_indent option to bin
* Complete file paths in strings (for require etc).
* Write docs for using a as library. Probably right after creating a Cocoa client.
* Write a not useless README.
Expand Down
4 changes: 2 additions & 2 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def output(string)
end
end

def prompt(indent = false)
formatter.prompt(self, indent)
def prompt(ignore_auto_indent = false)
formatter.prompt(self, ignore_auto_indent)
end

def input_line(line)
Expand Down
2 changes: 1 addition & 1 deletion lib/irb/driver/readline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def initialize(input = $stdin, output = $stdout)
end

def readline
source = ::Readline.readline(prompt, true)
source = ::Readline.readline(context.prompt, true)
IRB::History.input(source)
source
end
Expand Down
7 changes: 1 addition & 6 deletions lib/irb/driver/tty.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module IRB
module Driver
class TTY
attr_reader :input, :output, :context_stack
attr_accessor :auto_indent # TODO: this should probably go to a more global config, which means we shouldn't completely deprecate the CONF

def initialize(input = $stdin, output = $stdout)
@input = input
Expand All @@ -16,12 +15,8 @@ def context
@context_stack.last
end

def prompt
context.prompt(@auto_indent)
end

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

Expand Down
2 changes: 1 addition & 1 deletion lib/irb/ext/colorize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def colorize(str)
Ripper.lex(str).map { |_, type, token| colorize_token(type, token) }.join
end

def prompt(context, indent = false)
def prompt(context, ignore_auto_indent = false)
colorize_token(:prompt, super)
end

Expand Down
15 changes: 7 additions & 8 deletions spec/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ def o.to_s; "self"; end
@output = setup_current_driver.output
@context = IRB::Context.new(main)
@context.extend(InputStubMixin)
@context.formatter.auto_indent = true
end

after do
@context.formatter.auto_indent = false
end

it "adds the received code to the source buffer" do
Expand All @@ -125,20 +130,14 @@ def o.to_s; "self"; end
@context.source.to_s.should == "def foo\n p :ok"
end

it "yields the line if it changed after reindenting" do
it "yields the line if it changed, *after* reindenting" do
line = nil
@context.process_line("def foo") { |x| line = x }
line.should == nil
@context.process_line("p :ok") { |x| line = x }
line.should == " p :ok"
end

it "clears the source buffer" do
@context.process_line("def foo")
@context.clear_buffer
@context.source.to_s.should == ""
end


it "increases the current line number, *after* yielding the new re-indented line" do
@context.line.should == 1
@context.process_line("def foo")
Expand Down
18 changes: 11 additions & 7 deletions spec/driver/readline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def self.readline(prompt, use_history)
Readline.clear_printed!
end

after do
@context.formatter.auto_indent = false
end

it "is a subclass of IRB::Driver::TTY" do
IRB::Driver::Readline.superclass.should == IRB::Driver::TTY
end
Expand All @@ -58,23 +62,23 @@ def self.readline(prompt, use_history)
end

it "reads a line through the Readline module" do
Readline.stub_input "nom nom nom"
Readline.stub_input("nom nom nom")
@driver.readline.should == "nom nom nom"
end

it "prints a prompt" do
@context.source << "def foo"
Readline.stub_input "nom nom nom"
@context.process_line("def foo")
Readline.stub_input("nom nom nom")
@driver.readline
Readline.printed.should == @context.prompt
end

it "prints a prompt with indentation if it's configured" do
@driver.auto_indent = true
@context.source << "def foo"
Readline.stub_input "nom nom nom"
@context.formatter.auto_indent = true
@context.process_line("def foo")
Readline.stub_input("nom nom nom")
@driver.readline
Readline.printed.should == @context.prompt(true)
Readline.printed[-2,2].should == " "
end

it "tells the Readline module to use the history" do
Expand Down
26 changes: 15 additions & 11 deletions spec/driver/tty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@
@context = IRB::Context.new(Object.new)
@driver.context_stack << @context
end


after do
@context.formatter.auto_indent = false
end

it "prints the prompt and reads a line of input" do
@context.source << "def foo"
@driver.input.stub_input "calzone"
@context.process_line("def foo")
@driver.input.stub_input("calzone")
@driver.readline.should == "calzone"
@driver.output.printed.should == @context.prompt
end

it "prints a prompt with indentation if it's configured" do
@driver.auto_indent = true
@context.source << "def foo"
@driver.input.stub_input "calzone"
@context.formatter.auto_indent = true
@context.process_line("def foo")
@driver.input.stub_input("calzone")
@driver.readline
@driver.output.printed.should == @context.prompt(true)
@driver.output.printed[-2,2].should == " "
end

it "consumes input" do
@driver.input.stub_input "calzone"
@driver.input.stub_input("calzone")
@driver.consume.should == "calzone"
end

Expand All @@ -45,23 +49,23 @@ def @driver.readline; raise Interrupt; end

it "makes the given context the current one, for this driver, for the duration of the runloop" do
$from_context = nil
@driver.input.stub_input "$from_context = IRB::Driver.current.context"
@driver.input.stub_input("$from_context = IRB::Driver.current.context")
@driver.run(@context)
$from_context.should == @context
IRB::Driver.current.context.should == nil
end

it "feeds input into a given context" do
$from_context = false
@driver.input.stub_input "$from_context = true", "exit"
@driver.input.stub_input("$from_context = true", "exit")
@driver.run(@context)
$from_context.should == true
end

it "makes sure there's a global output redirector while running a context" do
before = $stdout
$from_context = nil
@driver.input.stub_input "$from_context = $stdout", "exit"
@driver.input.stub_input("$from_context = $stdout", "exit")
@driver.run(@context)
$from_context.class == IRB::Driver::OutputRedirector
$stdout.should == before
Expand Down

0 comments on commit b5bc8e3

Please sign in to comment.