[macruby-changes] [4855] DietRB/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Sun Oct 31 15:51:34 PDT 2010
Revision: 4855
http://trac.macosforge.org/projects/ruby/changeset/4855
Author: eloy.de.enige at gmail.com
Date: 2010-10-31 15:51:32 -0700 (Sun, 31 Oct 2010)
Log Message:
-----------
Add a auto_indent option to IRB::Formatter
Modified Paths:
--------------
DietRB/trunk/lib/irb/formatter.rb
DietRB/trunk/spec/formatter_spec.rb
Modified: DietRB/trunk/lib/irb/formatter.rb
===================================================================
--- DietRB/trunk/lib/irb/formatter.rb 2010-10-31 22:51:25 UTC (rev 4854)
+++ DietRB/trunk/lib/irb/formatter.rb 2010-10-31 22:51:32 UTC (rev 4855)
@@ -14,31 +14,34 @@
SIMPLE_PROMPT = ">> "
NO_PROMPT = ""
RESULT_PREFIX = "=>"
+ INDENTATION = " "
SYNTAX_ERROR = "SyntaxError: compile error\n(irb):%d: %s"
SOURCE_ROOT = Regexp.new("^#{File.expand_path('../../../', __FILE__)}")
attr_writer :prompt
attr_accessor :inspect
+ attr_accessor :auto_indent
attr_reader :filter_from_backtrace
def initialize
- @prompt = :default
- @inspect = true
+ @prompt = :default
+ @inspect = true
+ @auto_indent = false
@filter_from_backtrace = [SOURCE_ROOT]
end
def indentation(level)
- ' ' * level
+ INDENTATION * level
end
- def prompt(context, indent = false)
+ def prompt(context, ignore_auto_indent = false)
prompt = case @prompt
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.level)) : prompt
+ @auto_indent && !ignore_auto_indent ? (prompt + indentation(context.level)) : prompt
end
def inspect_object(object)
@@ -58,12 +61,17 @@
end
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 += line.lstrip
- source.buffer[-1] = new_line unless new_line == line
+ unless @auto_indent
+ yield
+ nil
+ else
+ old_level = source.level
+ yield
+ line = source.buffer[-1]
+ new_line = indentation(source.level < old_level ? source.level : old_level)
+ new_line += line.lstrip
+ source.buffer[-1] = new_line unless new_line == line
+ end
end
def result(object)
Modified: DietRB/trunk/spec/formatter_spec.rb
===================================================================
--- DietRB/trunk/spec/formatter_spec.rb 2010-10-31 22:51:25 UTC (rev 4854)
+++ DietRB/trunk/spec/formatter_spec.rb 2010-10-31 22:51:32 UTC (rev 4855)
@@ -78,34 +78,53 @@
"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> "
+ it "always skips re-indenting the last line in a Source#buffer if `auto_indent' is turned off" do
+ @context.source << "class A"
+ @formatter.reindent_last_line_in_source(@context.source) { @context.source << "def foo" }.should == nil
+ @context.source.buffer.last.should == "def foo"
+ end
- @context.process_line("class A")
- @formatter.prompt(@context, true).should == "irb(main):002:1> "
- @formatter.prompt(@context).should == "irb(main):002:1> "
+ describe "with auto-indentation" do
+ before do
+ @formatter.auto_indent = true
+ end
- @context.process_line("def foo")
- @formatter.prompt(@context, true).should == "irb(main):003:2> "
- @formatter.prompt(@context).should == "irb(main):003:2> "
- end
+ it "returns the whitespace to append to the prompt, based on the given level" do
+ @formatter.indentation(0).should == ""
+ @formatter.indentation(1).should == " "
+ @formatter.indentation(2).should == " "
+ 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
+ it "pads the prompt, based on the source level" do
+ @formatter.prompt(@context).should == "irb(main):001:0> "
+ @context.process_line("class A")
+ @formatter.prompt(@context).should == "irb(main):002:1> "
+ @context.process_line("def foo")
+ @formatter.prompt(@context).should == "irb(main):003:2> "
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
+ it "does not pad the prompt if it's explicitely specified" do
+ @context.process_line("class A")
+ @formatter.prompt(@context, true).should == "irb(main):002:1> "
+ 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
+
+ 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
end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101031/4b1a3465/attachment-0001.html>
More information about the macruby-changes
mailing list