[macruby-changes] [4849] DietRB/trunk

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


Revision: 4849
          http://trac.macosforge.org/projects/ruby/changeset/4849
Author:   eloy.de.enige at gmail.com
Date:     2010-10-31 15:50:50 -0700 (Sun, 31 Oct 2010)
Log Message:
-----------
Re-indent the last line added to a Source#buffer

Modified Paths:
--------------
    DietRB/trunk/lib/irb/formatter.rb
    DietRB/trunk/lib/irb/source.rb
    DietRB/trunk/spec/formatter_spec.rb

Modified: DietRB/trunk/lib/irb/formatter.rb
===================================================================
--- DietRB/trunk/lib/irb/formatter.rb	2010-10-31 22:50:41 UTC (rev 4848)
+++ DietRB/trunk/lib/irb/formatter.rb	2010-10-31 22:50:50 UTC (rev 4849)
@@ -27,8 +27,8 @@
       @filter_from_backtrace = [SOURCE_ROOT]
     end
 
-    def indentation(context)
-      '  ' * context.source.level
+    def indentation(level)
+      '  ' * level
     end
     
     def prompt(context, indent = false)
@@ -38,7 +38,7 @@
       else
         NO_PROMPT
       end
-      indent ? (prompt + indentation(context)) : prompt
+      indent ? (prompt + indentation(context.source.level)) : prompt
     end
     
     def inspect_object(object)
@@ -57,6 +57,14 @@
       "#<#{object.class}:0x%x>" % address
     end
 
+    def reindent_last_line_in_source(source)
+      old_level = source.level
+      yield
+      new_line  = indentation(source.level < old_level ? source.level : old_level)
+      new_line += source.buffer[-1].lstrip
+      source.buffer[-1] = new_line
+    end
+
     # Returns +true+ if adding the +line+ to the context’s source decreases the indentation level.
     def add_input_to_context(context, line)
       source = context.source

Modified: DietRB/trunk/lib/irb/source.rb
===================================================================
--- DietRB/trunk/lib/irb/source.rb	2010-10-31 22:50:41 UTC (rev 4848)
+++ DietRB/trunk/lib/irb/source.rb	2010-10-31 22:50:50 UTC (rev 4849)
@@ -16,7 +16,7 @@
     
     # Adds a source line to the buffer and flushes the cached reflection.
     def <<(source)
-      source = source.strip
+      source = source.rstrip
       unless source.empty?
         @reflection = nil
         @buffer << source

Modified: DietRB/trunk/spec/formatter_spec.rb
===================================================================
--- DietRB/trunk/spec/formatter_spec.rb	2010-10-31 22:50:41 UTC (rev 4848)
+++ DietRB/trunk/spec/formatter_spec.rb	2010-10-31 22:50:50 UTC (rev 4849)
@@ -15,18 +15,6 @@
     @context.source << "def foo"
     @formatter.prompt(@context).should == "irb(main):023:1> "
   end
-
-  it "also auto-indents the prompt, based on the source level, if requested" do
-    @formatter.prompt(@context, true).should == "irb(main):001:0> "
-
-    @context.process_line("class A")
-    @formatter.prompt(@context, true).should == "irb(main):002:1>   "
-    @formatter.prompt(@context).should       == "irb(main):002:1> "
-
-    @context.process_line("def foo")
-    @formatter.prompt(@context, true).should == "irb(main):003:2>     "
-    @formatter.prompt(@context).should       == "irb(main):003:2> "
-  end
   
   it "describes the context's object in the prompt" do
     o = Object.new
@@ -89,4 +77,30 @@
     @formatter.syntax_error(2, "syntax error, unexpected '}'").should ==
       "SyntaxError: compile error\n(irb):2: syntax error, unexpected '}'"
   end
+
+  it "pads the prompt, with indentation whitespace based on the source level, if requested" do
+    @formatter.prompt(@context, true).should == "irb(main):001:0> "
+
+    @context.process_line("class A")
+    @formatter.prompt(@context, true).should == "irb(main):002:1>   "
+    @formatter.prompt(@context).should       == "irb(main):002:1> "
+
+    @context.process_line("def foo")
+    @formatter.prompt(@context, true).should == "irb(main):003:2>     "
+    @formatter.prompt(@context).should       == "irb(main):003:2> "
+  end
+
+  it "reindents the last line in a Source#buffer after execution of the block, and returns the new line" do
+    source = @context.source
+    lines = [
+      ["\tclass A", "class A"],
+      ["def foo",   "  def foo"],
+      ["    end",   "  end"],
+      ["    end",   "end"]
+    ]
+    lines.each do |line, expected_new_line|
+      @formatter.reindent_last_line_in_source(source) { source << line }.should == expected_new_line
+    end
+    source.to_s.should == lines.map(&:last).join("\n")
+  end
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101031/69c47bc9/attachment-0001.html>


More information about the macruby-changes mailing list