Files
mercury/samples/diff/filter.m
Andrew Bromage bc0a109801 Modified files
Estimated hours taken: 30

Modified files
--------------

samples/diff/README:
	Info about this new version.  Put the existing READMEs in
	reverse order, so that they'll be in the most sensible order
	for someone reading top-down.

samples/diff/Mmakefile:
	Supply more detail about which bit of this program GCC 2.7.2
	under Digital Unix 3.2 doesn't compile properly.

samples/diff/diff.m:
	Slight reorganisation.  Accept options, remove some high
	coupling betweem diff__do_diff and lcss.m.

samples/diff/file.m:
	Add support for attaching a filename to a file.

samples/diff/lcss.m:
	Add more documentation, one slight performance improvement.
	lcss__to_diff now works on the reversed LCSS, to avoid going
	to the trouble of reversing it.

New files
---------

samples/diff/TODO:
	Meaning should be obvious.

samples/diff/diff_out.m:
	Replacement for diffs.m (which was never a very good name).
	Now contains code to display a diff in lots more output
	styles than previously supported.

samples/diff/difftype.m:
	Replacement for lcsstype.m.  Contains the type of a diff (but
	not of an lcss, which is now local to lcss.m, hence the
	renaming).

samples/diff/filter.m:
	Contains code to filter a diff before being displayed.  This
	is only required if the user specified that they didn't want
	to see part of the diff (e.g. with --ignore-blank-lines).

samples/diff/globals.m:
samples/diff/options.m:
	Support for option handling.

Removed files
-------------

samples/diff/diffs.m:
	Functionality moved to diff_out.m.

samples/diff/lcsstype.m:
	Functionality moved to difftype.m.
1998-01-13 00:52:08 +00:00

102 lines
3.0 KiB
Mathematica

%-----------------------------------------------------------------------------%
% Copyright (C) 1998 The University of Melbourne.
% This file may only be copied under the terms of the GNU General
% Public License - see the file COPYING in the Mercury distribution.
%-----------------------------------------------------------------------------%
% Main author: bromage
% This module contains code to filter the diff before output, based on
% the command-line options presented.
% At the moment, only one option is handled: --ignore-blank-lines.
% This causes edits to be dropped if they contain changes which only
% add, delete or change blank lines.
% TO DO: What is a blank line, exactly, and does its definition change
% if --ignore-space-change or --ignore-all-space have been
% specified? At the moment, we define a blank line to be a line
% containing zero or more whitespace characters.
%-----------------------------------------------------------------------------%
:- module filter.
:- interface.
:- import_module difftype, file, io.
:- pred filter_diff(diff :: in, file :: in, file :: in, diff :: out,
io__state :: di, io__state :: uo) is det.
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
:- implementation.
:- import_module globals, options.
:- import_module bool, list, int, std_util, string, char.
filter_diff(Diff0, File1, File2, Diff) -->
globals__io_lookup_bool_option(ignore_blank_lines, FilterBlank),
{ FilterBlank = no ->
% If we didn't request a filter, skip this pass.
Diff = Diff0
;
filter__blank_lines(Diff0, File1, File2, Diff)
}.
:- pred filter__blank_lines(diff :: in, file :: in, file :: in, diff :: out)
is det.
filter__blank_lines([], _, _, []).
filter__blank_lines([Edit | Diff0], File1, File2, Diff) :-
filter__blank_lines(Diff0, File1, File2, Diff1),
( Edit = add(_, Y1 - Y2),
( range_has_only_blank_lines(Y1, Y2, File2) ->
Diff = Diff1
;
Diff = [Edit | Diff1]
)
; Edit = delete(X1 - X2, _),
( range_has_only_blank_lines(X1, X2, File1) ->
Diff = Diff1
;
Diff = [Edit | Diff1]
)
; Edit = change(X1 - X2, Y1 - Y2),
(
range_has_only_blank_lines(X1, X2, File1),
range_has_only_blank_lines(Y1, Y2, File2)
->
Diff = Diff1
;
Diff = [Edit | Diff1]
)
).
%-----------------------------------------------------------------------------%
:- pred range_has_only_blank_lines(int, int, file).
:- mode range_has_only_blank_lines(in, in, in) is semidet.
range_has_only_blank_lines(First, Last, File) :-
(
First < Last
=>
(
file__get_line(File, First, Line),
string__to_char_list(Line, Chars),
(
list__member(C, Chars)
=>
char__is_whitespace(C)
),
Next is First + 1,
range_has_only_blank_lines(Next, Last, File)
)
).
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%