Permalink
Browse files

Keys.as_home_menu: intelligently save when some branches collapsed, a…

…nd save to difflog. TreeCursor: created.
  • Loading branch information...
1 parent 56bf058 commit dc36022fa055eb7278126553c17ff51a88e6819e @trogdoro committed Nov 9, 2011
Showing with 252 additions and 17 deletions.
  1. +13 −12 file_tree.rb
  2. +9 −1 menu.rb
  3. +5 −2 menus/outline.rb
  4. +1 −1 ol.rb
  5. +93 −0 spec/tree_cursor_spec.rb
  6. +24 −1 spec/tree_spec.rb
  7. +20 −0 tree.rb
  8. +87 −0 tree_cursor.rb
View
@@ -360,21 +360,21 @@ def self.apply_styles
Styles.apply('https?://[a-zA-Z0-9\/.~_:?&=|#+-]+', :notes_link) # Url
# |... lines (quotes)
- Styles.apply("^ +\\(|\\)\\( *\\)", nil, :quote_heading_pipe, :ls_quote)
- Styles.apply("^ +\\(|\\)\\(.*\n\\)", nil, :quote_heading_pipe, :ls_quote)
- Styles.apply("^ +\\(|\\)\\(.+?\\)([+-].*[-+])", nil, :quote_heading_pipe, :ls_quote) # quoted lines: beginnings of lines
- Styles.apply("^ +|.*([-+].*[+-])\\(.+\\)$", nil, :ls_quote) # quoted lines: ends of lines
+ Styles.apply("^ *\\(|\\)\\( *\\)", nil, :quote_heading_pipe, :ls_quote)
+ Styles.apply("^ *\\(|\\)\\(.*\n\\)", nil, :quote_heading_pipe, :ls_quote)
+ Styles.apply("^ *\\(|\\)\\(.+?\\)([+-].*[-+])", nil, :quote_heading_pipe, :ls_quote) # quoted lines: beginnings of lines
+ Styles.apply("^ *|.*([-+].*[+-])\\(.+\\)$", nil, :ls_quote) # quoted lines: ends of lines
Styles.apply("[+-])\\(.*?\\)([+-]", nil, :ls_quote) # quoted lines: between diffs
- Styles.apply("^ +\\(|\\)\\( \\)\\(>\\)\\(\n\\| .*\n\\)", nil, :quote_heading_pipe, :ls_quote, :quote_heading_bracket, :quote_heading)
- Styles.apply("^ +\\(|\\)\\( \\)\\(>>\\)\\(\n\\| .*\n\\)", nil, :quote_heading_pipe, :ls_quote, :quote_heading_bracket, :quote_heading_small)
+ Styles.apply("^ *\\(|\\)\\( \\)\\(>\\)\\(\n\\| .*\n\\)", nil, :quote_heading_pipe, :ls_quote, :quote_heading_bracket, :quote_heading)
+ Styles.apply("^ *\\(|\\)\\( \\)\\(>>\\)\\(\n\\| .*\n\\)", nil, :quote_heading_pipe, :ls_quote, :quote_heading_bracket, :quote_heading_small)
# | >... Headings
# |+... diffs
Styles.apply("^ +\\(:[0-9]+\\)$", nil, :ls_quote)
- Styles.apply("^ +\\(|\\+.*\\)", nil, :diff_green) # whole lines
- Styles.apply("^ +\\(|\-.*\\)", nil, :diff_red)
- Styles.apply("^ +\\(|@@ .*\n\\)", nil, :diff_line_number)
+ Styles.apply("^ *\\(|\\+.*\\)", nil, :diff_green) # whole lines
+ Styles.apply("^ *\\(|\-.*\\)", nil, :diff_red)
+ Styles.apply("^ *\\(|@@ .*\n\\)", nil, :diff_line_number)
#Styles.apply('^[ -]*\\([ a-zA-Z0-9\/_\.$-]*\\w/\\)$', nil, :ls_dir) # Most dirs
Styles.apply('^ *\\(//?\\)$', nil, :ls_dir) # /
@@ -991,7 +991,7 @@ def self.enter_lines pattern=nil, options={}
indent_more = options[:path] ? '' : ' '
if path =~ /^\/\w+@/
contents = Remote.file_contents path
- Tree.under contents, :escape=>'| '
+ Tree.under contents, :escape=>'| ', :no_slash=>1
return
end
@@ -1068,7 +1068,8 @@ def self.files_in_dir dir, options={}
self.remote_files_in_dir(dir)
else
all = Dir.glob("#{dir}*", File::FNM_DOTMATCH).
- select {|i| i !~ /\/\.(\.*|svn|git)$/}.sort
+ select {|i| i !~ /\/\.(\.*|svn|git)$/}.
+ select {|i| i !~ /\/\.#/}.sort
if options[:date_sort]
all = all.sort{|a,b| File.mtime(b) <=> File.mtime(a)}
@@ -1321,9 +1322,9 @@ def self.delete_file
return
end
+ Tree.kill_under
Line.delete
Move.to_line_text_beginning
-
end
def self.copy_to_latest_screenshot dest_path, dest_dir
View
@@ -300,11 +300,19 @@ def self.as_home_menu
path = File.expand_path "~/menus/#{root}.menu"
file_existed = File.exists? path
+
+ if file_existed
+ treeb = File.read path
+ txt = Tree.restore txt, treeb
+
+ DiffLog.save_diffs :patha=>path, :textb=>txt
+ end
+
File.open(path, "w") { |f| f << txt }
View.cursor = orig
- View.success "- #{file_existed ? 'Updated' : 'Created'} the \"#{root}/\" menu", :times=>4
+ View.success "- #{file_existed ? 'Updated' : 'Created'} menu: #{root}/", :times=>3
nil
end
View
@@ -4,14 +4,17 @@ def self.menu *target
if target.blank? # If just filter, show results
return View.txt.grep(/^> /).select{|o| o !~ /^> ?$/}.join("").gsub(/^/, '| ')
end
-
# Navigated to target text
+ # Grab line manually, because input will have all lines grouped together
+ # Should there be an option to have just 1 line passed in?
+ # How would it work?
+ # Something in the routing?
target = Line.value
target.sub! /^ *\| /, ''
View.to_highest
- Search.forward "^#{target}$"
+ result = Search.forward "^#{target}$"
Move.to_line_text_beginning
nil
View
@@ -20,7 +20,7 @@ def self.log txt, l=nil, name=nil, time=nil
end
# If n seconds passed since last call
- heading = self.pause_since_last?(time) ? "\n|\n" : nil
+ heading = self.pause_since_last?(time) ? "\n>\n" : nil
if l.is_a?(Array) # If an array of lines was passed
result = ""
@@ -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
View
@@ -1,7 +1,7 @@
$:.unshift "spec/"
require 'el_mixin'
-require 'tree'
require 'ol'
+require 'tree'
require 'core_ext'
describe Tree, "#traverse" do
@@ -329,5 +329,28 @@
result.should =~ /you/
result.should_not =~ /hey/
end
+end
+
+
+describe Tree, "#restore" do
+
+ it "copies missing children when removed" do
+ treea = "
+ - m/
+ - n/
+ ".unindent
+ treeb = "
+ - m/
+ - mm/
+ - mm2/
+ ".unindent
+
+ Tree.restore(treea, treeb).should == "
+ - m/
+ - mm/
+ - mm2/
+ - n/
+ ".unindent
+ end
end
View
@@ -1,3 +1,5 @@
+require 'tree_cursor'
+
class Tree
def self.search options={}
return $xiki_no_search=false if $xiki_no_search
@@ -900,4 +902,22 @@ def self.rest path
self.leaf(path)
end
+
+ # Copy children from treeb to treea, but only for branches in treea where children were removed.
+ def self.restore treea, treeb
+
+ treea, treeb = TreeCursor.new(treea), TreeCursor.new(treeb)
+
+ # For each leaf in A
+ treea.each do
+ next unless treea.at_leaf? # We only care about leafs
+
+ treeb.select treea.line # Find branch in B
+ next if treeb.at_leaf? # Skip if no children children
+
+ treea << treeb.under # Grab them and move into A
+ end
+
+ treea.txt
+ end
end
View
@@ -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

0 comments on commit dc36022

Please sign in to comment.