[macruby-changes] [4596] DietRB/trunk
source_changes at macosforge.org
source_changes at macosforge.org
Fri Oct 8 03:46:06 PDT 2010
Revision: 4596
http://trac.macosforge.org/projects/ruby/changeset/4596
Author: eloy.de.enige at gmail.com
Date: 2010-10-08 03:46:05 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Cache the source reflection when possible.
From: Eloy Duran <eloy.de.enige at gmail.com>
Modified Paths:
--------------
DietRB/trunk/lib/irb/source.rb
DietRB/trunk/spec/source_spec.rb
Modified: DietRB/trunk/lib/irb/source.rb
===================================================================
--- DietRB/trunk/lib/irb/source.rb 2010-10-08 10:45:56 UTC (rev 4595)
+++ DietRB/trunk/lib/irb/source.rb 2010-10-08 10:46:05 UTC (rev 4596)
@@ -3,14 +3,16 @@
module IRB
class Source
class Reflector < Ripper::SexpBuilder
+ attr_reader :level
+
def initialize(source)
super
@level = 0
+ @valid = !parse.nil?
end
- def level
- parse
- @level
+ def valid?
+ @valid
end
def on_kw(token)
@@ -30,7 +32,9 @@
@buffer = buffer
end
+ # Adds a source line to the buffer and flushes the cached reflection.
def <<(source)
+ @reflection = nil
@buffer << source.chomp
end
@@ -39,7 +43,7 @@
end
def level
- Reflector.new(source).level
+ reflect.level
end
# This does not take syntax errors in account, but only whether or not the
@@ -53,7 +57,11 @@
#
# def foo; p :ok; end
def valid?
- !!Reflector.new(source).parse
+ reflect.valid?
end
+
+ def reflect
+ @reflection ||= Reflector.new(source)
+ end
end
end
\ No newline at end of file
Modified: DietRB/trunk/spec/source_spec.rb
===================================================================
--- DietRB/trunk/spec/source_spec.rb 2010-10-08 10:45:56 UTC (rev 4595)
+++ DietRB/trunk/spec/source_spec.rb 2010-10-08 10:46:05 UTC (rev 4596)
@@ -66,22 +66,43 @@
@source << " end; end"
@source.level.should == 0
end
+
+ it "caches the reflection when possible" do
+ @source << "def foo"
+ reflection = @source.reflect
+ @source.level
+ @source.valid?
+ @source.reflect.should == reflection
+
+ @source << "end"
+ @source.level
+ new_reflection = @source.reflect
+ new_reflection.should.not == reflection
+ @source.valid?
+ @source.reflect.should == new_reflection
+ end
end
describe "IRB::Source::Reflector" do
- def level(source)
- IRB::Source::Reflector.new(source).level
+ def reflect(source)
+ IRB::Source::Reflector.new(source)
end
+ it "returns whether or not the source is a valid code block" do
+ reflect("def foo").should.not.be.valid
+ reflect("def foo; p :ok").should.not.be.valid
+ reflect("def foo; p :ok; end").should.be.valid
+ end
+
it "returns the code block indentation level" do
- level("").should == 0
- level("class A").should == 1
- level("class A; def foo").should == 2
- level("class A; def foo; p :ok").should == 2
- level("class A; def foo; p :ok; end").should == 1
- level("class A; class B").should == 2
- level("class A; class B; def bar").should == 3
- level("class A; class B; def bar; p :ok; end").should == 2
- level("class A; class B; def bar; p :ok; end; end; end").should == 0
+ reflect("").level.should == 0
+ reflect("class A").level.should == 1
+ reflect("class A; def foo").level.should == 2
+ reflect("class A; def foo; p :ok").level.should == 2
+ reflect("class A; def foo; p :ok; end").level.should == 1
+ reflect("class A; class B").level.should == 2
+ reflect("class A; class B; def bar").level.should == 3
+ reflect("class A; class B; def bar; p :ok; end").level.should == 2
+ reflect("class A; class B; def bar; p :ok; end; end; end").level.should == 0
end
end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/0c7f697c/attachment-0001.html>
More information about the macruby-changes
mailing list