[macruby-changes] [4858] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sun Oct 31 15:52:03 PDT 2010


Revision: 4858
          http://trac.macosforge.org/projects/ruby/changeset/4858
Author:   eloy.de.enige at gmail.com
Date:     2010-10-31 15:52:01 -0700 (Sun, 31 Oct 2010)
Log Message:
-----------
Also yield a reformatted prompt which contains the new level.

Modified Paths:
--------------
    DietRB/trunk/lib/irb/context.rb
    DietRB/trunk/lib/irb/driver/tty.rb
    DietRB/trunk/lib/irb/ext/colorize.rb
    DietRB/trunk/lib/irb/formatter.rb
    DietRB/trunk/spec/context_spec.rb
    DietRB/trunk/spec/driver/tty_spec.rb
    DietRB/trunk/spec/formatter_spec.rb

Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb	2010-10-31 22:51:51 UTC (rev 4857)
+++ DietRB/trunk/lib/irb/context.rb	2010-10-31 22:52:01 UTC (rev 4858)
@@ -61,13 +61,13 @@
     #
     #   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?
 

Modified: DietRB/trunk/lib/irb/driver/tty.rb
===================================================================
--- DietRB/trunk/lib/irb/driver/tty.rb	2010-10-31 22:51:51 UTC (rev 4857)
+++ DietRB/trunk/lib/irb/driver/tty.rb	2010-10-31 22:52:01 UTC (rev 4858)
@@ -33,13 +33,15 @@
         ""
       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.

Modified: DietRB/trunk/lib/irb/ext/colorize.rb
===================================================================
--- DietRB/trunk/lib/irb/ext/colorize.rb	2010-10-31 22:51:51 UTC (rev 4857)
+++ DietRB/trunk/lib/irb/ext/colorize.rb	2010-10-31 22:52:01 UTC (rev 4858)
@@ -169,7 +169,7 @@
       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
     

Modified: DietRB/trunk/lib/irb/formatter.rb
===================================================================
--- DietRB/trunk/lib/irb/formatter.rb	2010-10-31 22:51:51 UTC (rev 4857)
+++ DietRB/trunk/lib/irb/formatter.rb	2010-10-31 22:52:01 UTC (rev 4858)
@@ -34,14 +34,15 @@
       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 @@
       "#<#{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
 

Modified: DietRB/trunk/spec/context_spec.rb
===================================================================
--- DietRB/trunk/spec/context_spec.rb	2010-10-31 22:51:51 UTC (rev 4857)
+++ DietRB/trunk/spec/context_spec.rb	2010-10-31 22:52:01 UTC (rev 4858)
@@ -131,11 +131,11 @@
   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

Modified: DietRB/trunk/spec/driver/tty_spec.rb
===================================================================
--- DietRB/trunk/spec/driver/tty_spec.rb	2010-10-31 22:51:51 UTC (rev 4857)
+++ DietRB/trunk/spec/driver/tty_spec.rb	2010-10-31 22:52:01 UTC (rev 4858)
@@ -53,7 +53,7 @@
     @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

Modified: DietRB/trunk/spec/formatter_spec.rb
===================================================================
--- DietRB/trunk/spec/formatter_spec.rb	2010-10-31 22:51:51 UTC (rev 4857)
+++ DietRB/trunk/spec/formatter_spec.rb	2010-10-31 22:52:01 UTC (rev 4858)
@@ -80,7 +80,7 @@
 
   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 @@
     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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101031/f80bc4f3/attachment.html>


More information about the macruby-changes mailing list