Skip to content

Commit

Permalink
Re-indent the last line added to a Source#buffer
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.macosforge.org/repository/ruby/DietRB/trunk@4849 23306eb0-4c56-4727-a40e-e92c0eb68959
  • Loading branch information
alloy committed Oct 31, 2010
1 parent 4470106 commit 0d988af
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
14 changes: 11 additions & 3 deletions lib/irb/formatter.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/irb/source.rb
Expand Up @@ -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
Expand Down
38 changes: 26 additions & 12 deletions spec/formatter_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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.