129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.internal.text.revisions.HunkComputer;
|
|
14
|
131
|
15 import dwtx.jface.internal.text.revisions.LineIndexOutOfBoundsException; // packageimport
|
|
16 import dwtx.jface.internal.text.revisions.Hunk; // packageimport
|
|
17 import dwtx.jface.internal.text.revisions.Colors; // packageimport
|
|
18 import dwtx.jface.internal.text.revisions.ChangeRegion; // packageimport
|
|
19 import dwtx.jface.internal.text.revisions.Range; // packageimport
|
|
20 import dwtx.jface.internal.text.revisions.RevisionPainter; // packageimport
|
|
21 import dwtx.jface.internal.text.revisions.RevisionSelectionProvider; // packageimport
|
|
22
|
|
23
|
129
|
24 import dwt.dwthelper.utils;
|
|
25
|
|
26 import java.util.ArrayList;
|
|
27 import java.util.List;
|
|
28
|
|
29 import dwtx.jface.text.source.ILineDiffInfo;
|
|
30 import dwtx.jface.text.source.ILineDiffer;
|
|
31
|
|
32
|
|
33 /**
|
|
34 * Computes the diff hunks from an {@link ILineDiffer}.
|
|
35 *
|
|
36 * @since 3.3
|
|
37 */
|
|
38 public final class HunkComputer {
|
|
39 /**
|
|
40 * Converts the line-based information of {@link ILineDiffer} into {@link Hunk}s, grouping
|
|
41 * contiguous blocks of lines that are changed (added, deleted).
|
|
42 *
|
|
43 * @param differ the line differ to query
|
|
44 * @param lines the number of lines to query
|
|
45 * @return the corresponding {@link Hunk} information
|
|
46 */
|
|
47 public static Hunk[] computeHunks(ILineDiffer differ, int lines) {
|
|
48 List hunks= new ArrayList(lines);
|
|
49
|
|
50 int added= 0;
|
|
51 int changed= 0;
|
|
52 ILineDiffInfo info= null;
|
|
53 for (int line= 0; line < lines; line++) {
|
|
54 info= differ.getLineInfo(line);
|
|
55 if (info is null)
|
|
56 continue;
|
|
57
|
|
58 int changeType= info.getChangeType();
|
|
59 switch (changeType) {
|
|
60 case ILineDiffInfo.ADDED:
|
|
61 added++;
|
|
62 continue;
|
|
63 case ILineDiffInfo.CHANGED:
|
|
64 changed++;
|
|
65 continue;
|
|
66 case ILineDiffInfo.UNCHANGED:
|
|
67 added -= info.getRemovedLinesAbove();
|
|
68 if (added !is 0 || changed !is 0) {
|
|
69 hunks.add(new Hunk(line - changed - Math.max(0, added), added, changed));
|
|
70 added= 0;
|
|
71 changed= 0;
|
|
72 }
|
|
73 }
|
|
74 }
|
|
75
|
|
76 // last hunk
|
|
77 if (info !is null) {
|
|
78 added -= info.getRemovedLinesBelow();
|
|
79 if (added !is 0 || changed !is 0) {
|
|
80 hunks.add(new Hunk(lines - changed, added, changed));
|
|
81 added= 0;
|
|
82 changed= 0;
|
|
83 }
|
|
84 }
|
|
85
|
|
86 return (Hunk[]) hunks.toArray(new Hunk[hunks.size()]);
|
|
87 }
|
|
88 private HunkComputer() {
|
|
89 }
|
|
90 }
|