[macruby-changes] [4842] DietRB/trunk/lib/irb

source_changes at macosforge.org source_changes at macosforge.org
Thu Oct 28 07:01:17 PDT 2010


Revision: 4842
          http://trac.macosforge.org/projects/ruby/changeset/4842
Author:   eloy.de.enige at gmail.com
Date:     2010-10-28 07:01:15 -0700 (Thu, 28 Oct 2010)
Log Message:
-----------
Stash hacking on autoindentation

Modified Paths:
--------------
    DietRB/trunk/lib/irb/context.rb
    DietRB/trunk/lib/irb/driver/tty.rb
    DietRB/trunk/lib/irb/formatter.rb

Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb	2010-10-28 12:15:46 UTC (rev 4841)
+++ DietRB/trunk/lib/irb/context.rb	2010-10-28 14:01:15 UTC (rev 4842)
@@ -60,9 +60,13 @@
     #
     #   process_line("quit") # => false
     def process_line(line)
-      @source << line
+      reindented = formatter.add_input_to_context(self, line)
+      if reindented
+        driver.last_line_decreased_indentation_level(line)
+      end
+
       return false if @source.terminate?
-      
+
       if @source.syntax_error?
         output(formatter.syntax_error(@line, @source.syntax_error))
         @source.pop
@@ -75,10 +79,14 @@
       true
     end
     
+    def driver
+      IRB::Driver.current
+    end
+
     # Output is directed to the IRB::Driver.current driver’s output if a
     # current driver is available. Otherwise it’s simply printed to $stdout.
     def output(string)
-      if driver = IRB::Driver.current
+      if driver = self.driver
         driver.output.puts(string)
       else
         puts(string)

Modified: DietRB/trunk/lib/irb/driver/tty.rb
===================================================================
--- DietRB/trunk/lib/irb/driver/tty.rb	2010-10-28 12:15:46 UTC (rev 4841)
+++ DietRB/trunk/lib/irb/driver/tty.rb	2010-10-28 14:01:15 UTC (rev 4842)
@@ -27,6 +27,15 @@
         context.clear_buffer
         ""
       end
+
+      def last_line_decreased_indentation_level(reformatted_line)
+        move_one_line_up      = "\e[1A"
+        move_to_begin_of_line = "\r"
+        clear_to_end_of_line  = "\e[0K"
+        clear_last_line       = move_one_line_up + move_to_begin_of_line + clear_to_end_of_line
+        @output.print clear_last_line
+        @output.puts(context.prompt + reformatted_line)
+      end
       
       # Feeds input into a given context.
       #

Modified: DietRB/trunk/lib/irb/formatter.rb
===================================================================
--- DietRB/trunk/lib/irb/formatter.rb	2010-10-28 12:15:46 UTC (rev 4841)
+++ DietRB/trunk/lib/irb/formatter.rb	2010-10-28 14:01:15 UTC (rev 4842)
@@ -15,7 +15,7 @@
     NO_PROMPT      = ""
     RESULT_PREFIX  = "=>"
     SYNTAX_ERROR   = "SyntaxError: compile error\n(irb):%d: %s"
-    SOURCE_ROOT    = /^#{File.expand_path('../../../', __FILE__)}/
+    SOURCE_ROOT    = Regexp.new("^#{File.expand_path('../../../', __FILE__)}")
     
     attr_writer   :prompt
     attr_accessor :inspect
@@ -26,14 +26,19 @@
       @inspect = true
       @filter_from_backtrace = [SOURCE_ROOT]
     end
+
+    def indentation(context)
+      '  ' * context.source.level
+    end
     
     def prompt(context)
-      case @prompt
+      prompt = case @prompt
       when :default then DEFAULT_PROMPT % [context.object.inspect, context.line, context.source.level]
       when :simple  then SIMPLE_PROMPT
       else
         NO_PROMPT
       end
+      prompt + indentation(context)
     end
     
     def inspect_object(object)
@@ -47,6 +52,22 @@
         "#<#{object.class}:0x%x>" % address
       end
     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
+      level_before = source.level
+      source << line
+      if source.level < level_before
+        source.buffer[-1] = indentation(context) + line
+        true
+      end
+    end
+
+    def reindent_last_input(context)
+      line = context.source.buffer.last
+      indentation(context, -1) + line
+    end
     
     def result(object)
       "#{RESULT_PREFIX} #{inspect_object(object)}"
@@ -69,4 +90,4 @@
   end
 end
 
-IRB.formatter = IRB::Formatter.new
\ No newline at end of file
+IRB.formatter = IRB::Formatter.new
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101028/ec017c8a/attachment.html>


More information about the macruby-changes mailing list