Skip to content
Browse files

Stash hacking on autoindentation

git-svn-id: http://svn.macosforge.org/repository/ruby/DietRB/trunk@4842 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent 394c003 commit c02693de462fdf8aa2f2d420efc1b54a4c9fa9d2 @alloy alloy committed Oct 28, 2010
Showing with 44 additions and 6 deletions.
  1. +11 −3 lib/irb/context.rb
  2. +9 −0 lib/irb/driver/tty.rb
  3. +24 −3 lib/irb/formatter.rb
View
14 lib/irb/context.rb
@@ -60,9 +60,13 @@ def evaluate(source)
#
# 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 @@ def process_line(line)
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)
View
9 lib/irb/driver/tty.rb
@@ -27,6 +27,15 @@ def consume
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.
#
View
27 lib/irb/formatter.rb
@@ -15,7 +15,7 @@ class Formatter
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 @@ def initialize
@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 @@ def inspect_object(object)
"#<#{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 @@ def filter_backtrace(backtrace)
end
end
-IRB.formatter = IRB::Formatter.new
+IRB.formatter = IRB::Formatter.new

0 comments on commit c02693d

Please sign in to comment.
Something went wrong with that request. Please try again.