Skip to content
Browse files

Re-indent the last line added to a Source#buffer

git-svn-id: http://svn.macosforge.org/repository/ruby/DietRB/trunk@4849 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information...
1 parent 4470106 commit 0d988afd2ae7e93c02d7033129c7f3d419005984 @alloy alloy committed
Showing with 38 additions and 16 deletions.
  1. +11 −3 lib/irb/formatter.rb
  2. +1 −1 lib/irb/source.rb
  3. +26 −12 spec/formatter_spec.rb
View
14 lib/irb/formatter.rb
@@ -27,8 +27,8 @@ def initialize
@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 @@ def prompt(context, indent = false)
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 @@ def minimal_inspect_object(object)
"#<#{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
View
2 lib/irb/source.rb
@@ -16,7 +16,7 @@ def initialize(buffer = [])
# 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
View
38 spec/formatter_spec.rb
@@ -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 @@ def object.__id__; 2158110700; end
@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

0 comments on commit 0d988af

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