[meld] Applied new text filter mechanism to folder comparison



commit 8bac51d95a11e1decdee602cfaedb50cd523bd49
Author: David Rabel <david rabel noresoft com>
Date:   Tue Mar 8 23:13:54 2016 +0100

    Applied new text filter mechanism to folder comparison
    
    In commit eee58a571b1dc75f0d85d266aecec48a52df38c3 we changed the text
    filter mechanism. By now this was not affecting the way text filters are
    applied in folder comparison.
    
    With this patch, the text filter mechanism is the same for file and
    folder comparison.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=761028

 meld/dirdiff.py  |    5 +++--
 meld/filediff.py |   42 ++++++++++++------------------------------
 meld/misc.py     |   36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 32 deletions(-)
---
diff --git a/meld/dirdiff.py b/meld/dirdiff.py
index a7cffca..02ab2a7 100644
--- a/meld/dirdiff.py
+++ b/meld/dirdiff.py
@@ -193,8 +193,9 @@ def _files_same(files, regexes, comparison_args):
         # For probable text files, discard newline differences to match
         # file comparisons.
         contents = ["\n".join(c.splitlines()) for c in contents]
-        for r in regexes:
-            contents = [re.sub(r, "", c) for c in contents]
+
+        contents = [misc.apply_text_filters(c, regexes) for c in contents]
+
         if ignore_blank_lines:
             contents = [remove_blank_lines(c) for c in contents]
         result = SameFiltered if all_same(contents) else Different
diff --git a/meld/filediff.py b/meld/filediff.py
index 94950d9..4da970b 100644
--- a/meld/filediff.py
+++ b/meld/filediff.py
@@ -757,37 +757,19 @@ class FileDiff(melddoc.MeldDoc, gnomeglade.Component):
         dimmed_tag = buf.get_tag_table().lookup("dimmed")
         buf.remove_tag(dimmed_tag, txt_start_iter, txt_end_iter)
 
-        try:
-            filter_ranges = []
-
-            active_filters = [f for f in self.text_filters if f.active]
-            for filt in active_filters:
-                for match in filt.filter.finditer(txt):
-                    # If there are no groups in the match, use the whole match
-                    if not filt.filter.groups:
-                        span = match.span()
-                        if span[0] != span[1]:
-                            filter_ranges.append(span)
-                        continue
-
-                    # If there are groups in the regex, include all groups that
-                    # participated in the match
-                    for i in range(filt.filter.groups):
-                        span = match.span(i + 1)
-                        if span != (-1, -1) and span[0] != span[1]:
-                            filter_ranges.append(span)
-
-            filter_ranges = misc.merge_intervals(filter_ranges)
-
-            for (start, end) in reversed(filter_ranges):
-                assert txt[start:end].count("\n") == 0
-                txt = txt[:start] + txt[end:]
-                start_iter = txt_start_iter.copy()
-                start_iter.forward_chars(start)
-                end_iter = txt_start_iter.copy()
-                end_iter.forward_chars(end)
-                buf.apply_tag(dimmed_tag, start_iter, end_iter)
+        def cutter(txt, start, end):
+            assert txt[start:end].count("\n") == 0
+            txt = txt[:start] + txt[end:]
+            start_iter = txt_start_iter.copy()
+            start_iter.forward_chars(start)
+            end_iter = txt_start_iter.copy()
+            end_iter.forward_chars(end)
+            buf.apply_tag(dimmed_tag, start_iter, end_iter)
+            return txt
 
+        try:
+            regexes = [f.filter for f in self.text_filters if f.active]
+            txt = misc.apply_text_filters(txt, regexes, cutter)
         except AssertionError:
             if not self.warned_bad_comparison:
                 misc.error_dialog(
diff --git a/meld/misc.py b/meld/misc.py
index 481f9ac..f7aae9a 100644
--- a/meld/misc.py
+++ b/meld/misc.py
@@ -477,3 +477,39 @@ def merge_intervals(interval_list):
         current_start, current_end = merged_intervals[-1]
 
     return merged_intervals
+
+
+def apply_text_filters(txt, regexes, cutter=lambda txt, start, end:
+                       txt[:start] + txt[end:]):
+    """Apply text filters
+
+    Text filters "regexes", resolved as regular expressions are applied
+    to "txt".
+
+    "cutter" defines the way how to apply them. Default is to just cut
+    out the matches.
+    """
+    filter_ranges = []
+    for r in regexes:
+        for match in r.finditer(txt):
+
+            # If there are no groups in the match, use the whole match
+            if not r.groups:
+                span = match.span()
+                if span[0] != span[1]:
+                    filter_ranges.append(span)
+                continue
+
+            # If there are groups in the regex, include all groups that
+            # participated in the match
+            for i in range(r.groups):
+                span = match.span(i + 1)
+                if span != (-1, -1) and span[0] != span[1]:
+                    filter_ranges.append(span)
+                    
+    filter_ranges = merge_intervals(filter_ranges)
+
+    for (start, end) in reversed(filter_ranges):
+        txt = cutter(txt, start, end)
+
+    return txt


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