Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keys.as_home_menu: intelligently save when some branches collapsed, a…
…nd save to difflog. TreeCursor: created.
- Loading branch information
Showing
8 changed files
with
252 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
$:.unshift "spec/" | ||
# require 'el_mixin' | ||
require 'el_mixin' | ||
require 'core_ext' | ||
require 'tree_cursor' | ||
require 'ol' | ||
|
||
def a_aa_tree | ||
" | ||
- a/ | ||
- aa/ | ||
".unindent | ||
end | ||
|
||
describe TreeCursor, "#each" do | ||
it "loops through each line" do | ||
tc = TreeCursor.new "- a/\n - aa/", 1 | ||
|
||
result, indexes = "", [] | ||
tc.each do | ||
result << tc.line | ||
indexes << tc.i | ||
end | ||
|
||
result.should == "- a/ - aa/" | ||
indexes.should == [0, 1] | ||
end | ||
end | ||
|
||
describe TreeCursor, "#at_leaf?" do | ||
it "recognizes leaf if no children" do | ||
TreeCursor.new(" | ||
- a/ | ||
- aa/".unindent).at_leaf?.should == true | ||
end | ||
|
||
it "returns false if children" do | ||
TreeCursor.new(a_aa_tree).at_leaf?.should == false | ||
end | ||
|
||
it "recognizes leaf if at end" do | ||
TreeCursor.new(" | ||
- a/ | ||
- aa/".unindent, 1).at_leaf?.should == true | ||
end | ||
end | ||
|
||
describe TreeCursor, "#select" do | ||
it "finds matching line" do | ||
tc = TreeCursor.new(a_aa_tree) | ||
|
||
tc.select " - aa/" | ||
tc.i.should == 1 | ||
end | ||
|
||
it "stays at 0 when no matching line" do | ||
tc = TreeCursor.new(a_aa_tree) | ||
|
||
tc.select " - aaa/" | ||
tc.i.should == 0 | ||
end | ||
|
||
it "finds matching when bullet changed" do | ||
tc = TreeCursor.new(a_aa_tree) | ||
tc.select " + aa/" | ||
tc.i.should == 1 | ||
end | ||
end | ||
|
||
describe TreeCursor, "#under" do | ||
it "returns lines under" do | ||
tc = TreeCursor.new(a_aa_tree+" - bb/") | ||
tc.under.should == " - aa/\n - bb/\n" | ||
end | ||
|
||
it "returns none if at end" do | ||
tc = TreeCursor.new(a_aa_tree) | ||
tc.i = 1 | ||
tc.under.should == "" | ||
end | ||
end | ||
|
||
describe TreeCursor, "#<<" do | ||
it "inserts in tree" do | ||
tc = TreeCursor.new(a_aa_tree) | ||
tc << " - zz/\n" | ||
tc.txt.should == " | ||
- a/ | ||
- zz/ | ||
- aa/ | ||
".unindent | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
class TreeCursor | ||
@i = 0 | ||
@txt = "" | ||
|
||
def initialize txt, i=0 | ||
raise "1st parameter to TreeCursor.initialize must be a string" unless txt.is_a? String | ||
@i = i | ||
@txt = txt | ||
end | ||
|
||
def to_s | ||
"[#{@i}, #{@txt.inspect}]" | ||
end | ||
def inspect | ||
to_s | ||
end | ||
|
||
def i; @i; end | ||
def i= num; @i = num; end | ||
def txt; @txt; end | ||
|
||
def to_a | ||
@txt.strip.split("\n", -1) | ||
end | ||
|
||
def length | ||
@txt.strip.count("\n") + 1 | ||
end | ||
|
||
def line | ||
to_a[@i] | ||
end | ||
|
||
def each &block | ||
@i = 0 | ||
while @i < length | ||
# while @i < @txt.split("\n").length | ||
block.call | ||
@i += 1 | ||
end | ||
end | ||
|
||
def at_leaf? | ||
a = to_a | ||
next_line = a[@i+1] | ||
return true if next_line.nil? | ||
|
||
indent = Line.indent a[@i] | ||
next_indent = Line.indent a[@i+1] | ||
indent.length >= next_indent.length | ||
end | ||
|
||
def [] index | ||
to_a[index] | ||
end | ||
|
||
def select line | ||
line = line.sub /^( *)\+ /, "\\1- " | ||
index = to_a.index{|o| o.sub(/^( *)\+ /, "\\1- ") == line} | ||
@i = index if index | ||
end | ||
|
||
def under | ||
result = "" | ||
a = to_a | ||
target_indent = Line.indent(a[@i]).length | ||
i = @i + 1 | ||
while i < length | ||
indent = Line.indent(a[i]).length | ||
break if indent <= target_indent | ||
result << "#{a[i]}\n" | ||
i += 1 | ||
end | ||
|
||
result | ||
end | ||
|
||
def index_after | ||
to_a[0..@i].join("\n").length | ||
end | ||
|
||
def << lines | ||
@txt.insert index_after+1, lines | ||
end | ||
|
||
|
||
end |