[macruby-changes] [4851] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Sun Oct 31 15:51:05 PDT 2010


Revision: 4851
          http://trac.macosforge.org/projects/ruby/changeset/4851
Author:   eloy.de.enige at gmail.com
Date:     2010-10-31 15:51:03 -0700 (Sun, 31 Oct 2010)
Log Message:
-----------
The Context now re-indents the last line of the source buffer and yields it if it was changed.

Modified Paths:
--------------
    DietRB/trunk/lib/irb/context.rb
    DietRB/trunk/spec/context_spec.rb

Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb	2010-10-31 22:50:56 UTC (rev 4850)
+++ DietRB/trunk/lib/irb/context.rb	2010-10-31 22:51:03 UTC (rev 4851)
@@ -11,13 +11,14 @@
   class Context
     IGNORE_RESULT = :irb_ignore_result
     
-    attr_reader :object, :binding, :line, :source
+    attr_reader :object, :binding, :line, :level, :source
     attr_accessor :formatter
     
     def initialize(object, explicit_binding = nil)
       @object  = object
       @binding = explicit_binding || object.instance_eval { binding }
       @line    = 1
+      @level   = 0
       clear_buffer
       
       @last_result_assigner = __evaluate__("_ = nil; proc { |val| _ = val }")
@@ -59,11 +60,14 @@
     # But at code block indentation level 0, `quit' means exit the runloop:
     #
     #   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.
     def process_line(line)
-      reindented = formatter.add_input_to_context(self, line)
-      #if reindented
-        #driver.last_line_decreased_indentation_level(line)
-      #end
+      new_line = formatter.reindent_last_line_in_source(@source) { @source << line }
+      yield new_line if new_line && block_given?
 
       return false if @source.terminate?
 
@@ -75,6 +79,7 @@
         clear_buffer
       end
       @line += 1
+      @level = source.level
       
       true
     end
@@ -93,8 +98,8 @@
       end
     end
     
-    def prompt
-      formatter.prompt(self)
+    def prompt(indent = false)
+      formatter.prompt(self, indent)
     end
     
     def input_line(line)

Modified: DietRB/trunk/spec/context_spec.rb
===================================================================
--- DietRB/trunk/spec/context_spec.rb	2010-10-31 22:50:56 UTC (rev 4850)
+++ DietRB/trunk/spec/context_spec.rb	2010-10-31 22:51:03 UTC (rev 4851)
@@ -122,23 +122,39 @@
   it "adds the received code to the source buffer" do
     @context.process_line("def foo")
     @context.process_line("p :ok")
-    @context.source.to_s.should == "def foo\np :ok"
+    @context.source.to_s.should == "def foo\n  p :ok"
   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"
+  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" do
+  it "increases the current line number, *after* yielding the new re-indented line" do
     @context.line.should == 1
     @context.process_line("def foo")
     @context.line.should == 2
-    @context.process_line("p :ok")
+    @context.process_line("p :ok") 
     @context.line.should == 3
   end
   
+  it "increases the current source level, *after* yielding the new re-indented line" do
+    @context.level.should == 0
+    @context.process_line("def foo")
+    @context.level.should == 1
+    @context.process_line("end") { |_| @context.level.should == 1 }
+    @context.level.should == 0
+  end
+  
   it "evaluates the buffered source once it's a valid code block" do
     def @context.evaluate(source); @evaled = source; end
     
@@ -147,7 +163,7 @@
     @context.process_line("end; p foo")
     
     source = @context.instance_variable_get(:@evaled)
-    source.to_s.should == "def foo\n:ok\nend; p foo"
+    source.to_s.should == "def foo\n  :ok\nend; p foo"
   end
   
   it "prints that a syntax error occurred on the last line and reset the buffer to the previous line" do
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101031/29551619/attachment.html>


More information about the macruby-changes mailing list