[meld] treehelpers: Move several path-related helper functions



commit 8e8036b82c359e6e999505814556489ec5d76bf1
Author: Kai Willadsen <kai willadsen gmail com>
Date:   Sun Oct 23 07:58:12 2016 +1000

    treehelpers: Move several path-related helper functions

 meld/dirdiff.py     | 44 +-------------------------------------------
 meld/treehelpers.py | 43 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 43 deletions(-)
---
diff --git a/meld/dirdiff.py b/meld/dirdiff.py
index 656aaed8..fa3a460b 100644
--- a/meld/dirdiff.py
+++ b/meld/dirdiff.py
@@ -46,6 +46,7 @@ from meld.conf import _
 from meld.misc import all_same
 from meld.recent import RecentType
 from meld.settings import bind_settings, meldsettings, settings
+from meld.treehelpers import refocus_deleted_path
 
 
 ################################################################################
@@ -981,49 +982,6 @@ class DirDiff(melddoc.MeldDoc, gnomeglade.Component):
                     misc.error_dialog(_("Error deleting %s") % name, str(e))
 
     def on_treemodel_row_deleted(self, model, path):
-
-        # TODO: Move this and path tools to new tree helper module
-        def refocus_deleted_path(model, path):
-            # Since the passed path has been deleted, either the path is now a
-            # valid successor, or there are no successors. If valid, return it.
-            # If not, and the path has a predecessor sibling (immediate or
-            # otherwise), then return that. If there are no siblings, traverse
-            # parents until we get a valid path, and return that.
-
-            def tree_path_prev(path):
-                if not path or path[-1] == 0:
-                    return None
-                return path[:-1] + (path[-1] - 1,)
-
-            def tree_path_up(path):
-                if not path:
-                    return None
-                return path[:-1]
-
-            def valid_path(model, path):
-                try:
-                    model.get_iter(path)
-                    return True
-                except ValueError:
-                    return False
-
-            if valid_path(model, path):
-                return path
-
-            new_path = tree_path_prev(path)
-            while new_path:
-                if valid_path(model, new_path):
-                    return new_path
-                new_path = tree_path_prev(new_path)
-
-            new_path = tree_path_up(path)
-            while new_path:
-                if valid_path(model, new_path):
-                    return new_path
-                new_path = tree_path_up(new_path)
-
-            return None
-
         if self.current_path == path:
             self.current_path = refocus_deleted_path(model, path)
             if self.current_path and self.focus_pane:
diff --git a/meld/treehelpers.py b/meld/treehelpers.py
index cdae8d37..b8f6afb9 100644
--- a/meld/treehelpers.py
+++ b/meld/treehelpers.py
@@ -17,6 +17,49 @@
 from gi.repository import Gtk
 
 
+def tree_path_prev(path):
+    if not path or path[-1] == 0:
+        return None
+    return path[:-1] + (path[-1] - 1,)
+
+
+def tree_path_up(path):
+    if not path:
+        return None
+    return path[:-1]
+
+
+def valid_path(model, path):
+    try:
+        model.get_iter(path)
+        return True
+    except ValueError:
+        return False
+
+
+def refocus_deleted_path(model, path):
+    # Since the passed path has been deleted, either the path is now a
+    # valid successor, or there are no successors. If valid, return it.
+    # If not, and the path has a predecessor sibling (immediate or
+    # otherwise), then return that. If there are no siblings, traverse
+    # parents until we get a valid path, and return that.
+
+    if valid_path(model, path):
+        return path
+
+    new_path = tree_path_prev(path)
+    while new_path:
+        if valid_path(model, new_path):
+            return new_path
+        new_path = tree_path_prev(new_path)
+
+    new_path = tree_path_up(path)
+    while new_path:
+        if valid_path(model, new_path):
+            return new_path
+        new_path = tree_path_up(new_path)
+
+
 class SearchableTreeStore(Gtk.TreeStore):
 
     def inorder_search_down(self, it):


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]