Commit 8bac51d9 authored by David Rabel's avatar David Rabel Committed by Kai Willadsen

Applied new text filter mechanism to folder comparison

In commit eee58a57 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
parent 3ce95607
......@@ -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
......
......@@ -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(
......
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment