From 052972fbbfc1e9328d005bad5f7defcd25dc6c5d Mon Sep 17 00:00:00 2001 From: Eloy Duran Date: Sun, 31 Oct 2010 22:51:10 +0000 Subject: [PATCH] Only return the re-indented line if it changed. git-svn-id: http://svn.macosforge.org/repository/ruby/DietRB/trunk@4852 23306eb0-4c56-4727-a40e-e92c0eb68959 --- lib/irb/formatter.rb | 9 +++++---- spec/formatter_spec.rb | 9 +++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/irb/formatter.rb b/lib/irb/formatter.rb index cf6eda9..1277502 100644 --- a/lib/irb/formatter.rb +++ b/lib/irb/formatter.rb @@ -33,12 +33,12 @@ def indentation(level) def prompt(context, indent = false) prompt = case @prompt - when :default then DEFAULT_PROMPT % [context.object.inspect, context.line, context.source.level] + when :default then DEFAULT_PROMPT % [context.object.inspect, context.line, context.level] when :simple then SIMPLE_PROMPT else NO_PROMPT end - indent ? (prompt + indentation(context.source.level)) : prompt + indent ? (prompt + indentation(context.level)) : prompt end def inspect_object(object) @@ -60,9 +60,10 @@ def minimal_inspect_object(object) def reindent_last_line_in_source(source) old_level = source.level yield + line = source.buffer[-1] new_line = indentation(source.level < old_level ? source.level : old_level) - new_line += source.buffer[-1].lstrip - source.buffer[-1] = new_line + new_line += line.lstrip + source.buffer[-1] = new_line unless new_line == line end def result(object) diff --git a/spec/formatter_spec.rb b/spec/formatter_spec.rb index 74f3ebd..420c04d 100644 --- a/spec/formatter_spec.rb +++ b/spec/formatter_spec.rb @@ -12,8 +12,8 @@ @formatter.prompt(@context).should == "irb(main):001:0> " @context.instance_variable_set(:@line, 23) @formatter.prompt(@context).should == "irb(main):023:0> " - @context.source << "def foo" - @formatter.prompt(@context).should == "irb(main):023:1> " + @context.process_line("def foo") + @formatter.prompt(@context).should == "irb(main):024:1> " end it "describes the context's object in the prompt" do @@ -103,4 +103,9 @@ def object.__id__; 2158110700; end end source.to_s.should == lines.map(&:last).join("\n") end + + it "returns nil if the last line was not reindented" do + @context.source << "class A" + @formatter.reindent_last_line_in_source(@context.source) { @context.source << " def foo" }.should == nil + end end