Permalink
Browse files

Also yield a reformatted prompt which contains the new level.

git-svn-id: http://svn.macosforge.org/repository/ruby/DietRB/trunk@4858 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent 67a556b commit fe2e6cb3b9b17d791b21c60ebbe54a09df074eab @alloy alloy committed Oct 31, 2010
Showing with 46 additions and 34 deletions.
  1. +6 −6 lib/irb/context.rb
  2. +5 −3 lib/irb/driver/tty.rb
  3. +1 −1 lib/irb/ext/colorize.rb
  4. +14 −7 lib/irb/formatter.rb
  5. +5 −5 spec/context_spec.rb
  6. +1 −1 spec/driver/tty_spec.rb
  7. +14 −11 spec/formatter_spec.rb
View
@@ -61,13 +61,13 @@ def evaluate(source)
#
# process_line("quit") # => false
#
- # If re-indenting the line results in a new line, the new line is yielded
- # to the optional block. This happens *before* the line is actually
- # processed, so the caller (driver) has the opportunity to update the last
- # printed line.
+ # If re-indenting the line results in a new line, the reformatted line and
+ # prompt are yielded to the optional block. This happens *before* the line
+ # is actually processed, so the caller (driver) has the opportunity to
+ # update the last printed line.
def process_line(line)
- new_line = formatter.reindent_last_line_in_source(@source) { @source << line }
- yield new_line if new_line && block_given?
+ prompt_and_line = formatter.reindent_last_line(self) { @source << line }
+ yield(*prompt_and_line) if prompt_and_line && block_given?
return false if @source.terminate?
View
@@ -33,13 +33,15 @@ def consume
""
end
- def update_last_line(reformatted_line)
+ def update_last_line(prompt, reformatted_line)
@output.print CLEAR_LAST_LINE
- @output.puts(context.prompt(true) + reformatted_line)
+ @output.puts("#{prompt}#{reformatted_line}")
end
def process_input(line)
- context.process_line(line) { |reformatted_line| update_last_line(reformatted_line) }
+ context.process_line(line) do |prompt, line|
+ update_last_line(prompt, line)
+ end
end
# Feeds input into a given context.
View
@@ -169,7 +169,7 @@ def colorize(str)
Ripper.lex(str).map { |_, type, token| colorize_token(type, token) }.join
end
- def prompt(context, ignore_auto_indent = false)
+ def prompt(context, ignore_auto_indent = false, level = nil)
colorize_token(:prompt, super)
end
View
@@ -34,14 +34,15 @@ def indentation(level)
INDENTATION * level
end
- def prompt(context, ignore_auto_indent = false)
+ def prompt(context, ignore_auto_indent = false, level = nil)
+ level ||= context.level
prompt = case @prompt
- when :default then DEFAULT_PROMPT % [context.object.inspect, context.line, context.level]
+ when :default then DEFAULT_PROMPT % [context.object.inspect, context.line, level]
when :simple then SIMPLE_PROMPT
else
NO_PROMPT
end
- @auto_indent && !ignore_auto_indent ? (prompt + indentation(context.level)) : prompt
+ @auto_indent && !ignore_auto_indent ? "#{prompt}#{indentation(level)}" : prompt
end
def inspect_object(object)
@@ -60,17 +61,23 @@ def minimal_inspect_object(object)
"#<#{object.class}:0x%x>" % address
end
- def reindent_last_line_in_source(source)
+ def reindent_last_line(context)
unless @auto_indent
yield
nil
else
+ source = context.source
old_level = source.level
yield
+ level = source.level < old_level ? source.level : old_level
line = source.buffer[-1]
- new_line = indentation(source.level < old_level ? source.level : old_level)
- new_line += line.lstrip
- source.buffer[-1] = new_line unless new_line == line
+ new_line = indentation(level)
+ new_line << line.lstrip
+ unless line == new_line && level == old_level
+ source.buffer[-1] = new_line
+ new_prompt = prompt(context, true, level)
+ [new_prompt, new_line]
+ end
end
end
View
@@ -131,11 +131,11 @@ def o.to_s; "self"; end
end
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"
+ prompt_and_line = nil
+ @context.process_line("def foo") { |p, l| prompt_and_line = [p, l] }
+ prompt_and_line.should == nil
+ @context.process_line("p :ok") { |p, l| prompt_and_line = [p, l] }
+ prompt_and_line.should == ["irb(main):002:1> ", " p :ok"]
end
it "increases the current line number, *after* yielding the new re-indented line" do
View
@@ -53,7 +53,7 @@ def @driver.readline; raise Interrupt; end
@driver.process_input(" end")
@driver.output.printed.strip.should == [
IRB::Driver::TTY::CLEAR_LAST_LINE + "irb(main):002:1> p :ok",
- IRB::Driver::TTY::CLEAR_LAST_LINE + "irb(main):003:1> end"
+ IRB::Driver::TTY::CLEAR_LAST_LINE + "irb(main):003:0> end"
].join("\n")
end
end
View
@@ -80,7 +80,7 @@ def object.__id__; 2158110700; end
it "always skips re-indenting the last line in a Source#buffer if `auto_indent' is turned off" do
@context.source << "class A"
- @formatter.reindent_last_line_in_source(@context.source) { @context.source << "def foo" }.should == nil
+ @formatter.reindent_last_line(@context) { @context.source << "def foo" }.should == nil
@context.source.buffer.last.should == "def foo"
end
@@ -109,22 +109,25 @@ def object.__id__; 2158110700; end
end
it "reindents the last line in a Source#buffer after execution of the block, and returns the new line" do
- source = @context.source
+ # the line number in the prompt is irrelevant for this test
lines = [
- ["\tclass A", "class A"],
- ["def foo", " def foo"],
- [" end", " end"],
- [" end", "end"]
+ ["\tclass A", ["irb(main):001:0> ", "class A"]],
+ ["def foo", ["irb(main):001:1> ", " def foo"]],
+ [" end", ["irb(main):001:1> ", " end"]],
+ [" end", ["irb(main):001:0> ", "end"]]
]
- lines.each do |line, expected_new_line|
- @formatter.reindent_last_line_in_source(source) { source << line }.should == expected_new_line
+ lines.each do |line, expected_prompt_and_line|
+ @formatter.reindent_last_line(@context) do
+ @context.source << line
+ end.should == expected_prompt_and_line
end
- source.to_s.should == lines.map(&:last).join("\n")
+ @context.source.to_s.should == lines.map { |x| x[1][1] }.join("\n")
end
- it "returns nil if the last line was not reindented" do
+ it "returns nil if the last line was not reindented and the level didn't change" do
@context.source << "class A"
- @formatter.reindent_last_line_in_source(@context.source) { @context.source << " def foo" }.should == nil
+ @formatter.reindent_last_line(@context) { @context.source << " def foo" }.should == nil
+ @formatter.reindent_last_line(@context) { @context.source << " end" }.should_not == nil
end
end
end

0 comments on commit fe2e6cb

Please sign in to comment.