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
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import java.util.ArrayList;
|
|
18 import java.util.List;
|
|
19
|
|
20 import dwtx.jface.text.source.ILineDiffInfo;
|
|
21 import dwtx.jface.text.source.ILineDiffer;
|
|
22
|
|
23
|
|
24 /**
|
|
25 * Computes the diff hunks from an {@link ILineDiffer}.
|
|
26 *
|
|
27 * @since 3.3
|
|
28 */
|
|
29 public final class HunkComputer {
|
|
30 /**
|
|
31 * Converts the line-based information of {@link ILineDiffer} into {@link Hunk}s, grouping
|
|
32 * contiguous blocks of lines that are changed (added, deleted).
|
|
33 *
|
|
34 * @param differ the line differ to query
|
|
35 * @param lines the number of lines to query
|
|
36 * @return the corresponding {@link Hunk} information
|
|
37 */
|
|
38 public static Hunk[] computeHunks(ILineDiffer differ, int lines) {
|
|
39 List hunks= new ArrayList(lines);
|
|
40
|
|
41 int added= 0;
|
|
42 int changed= 0;
|
|
43 ILineDiffInfo info= null;
|
|
44 for (int line= 0; line < lines; line++) {
|
|
45 info= differ.getLineInfo(line);
|
|
46 if (info is null)
|
|
47 continue;
|
|
48
|
|
49 int changeType= info.getChangeType();
|
|
50 switch (changeType) {
|
|
51 case ILineDiffInfo.ADDED:
|
|
52 added++;
|
|
53 continue;
|
|
54 case ILineDiffInfo.CHANGED:
|
|
55 changed++;
|
|
56 continue;
|
|
57 case ILineDiffInfo.UNCHANGED:
|
|
58 added -= info.getRemovedLinesAbove();
|
|
59 if (added !is 0 || changed !is 0) {
|
|
60 hunks.add(new Hunk(line - changed - Math.max(0, added), added, changed));
|
|
61 added= 0;
|
|
62 changed= 0;
|
|
63 }
|
|
64 }
|
|
65 }
|
|
66
|
|
67 // last hunk
|
|
68 if (info !is null) {
|
|
69 added -= info.getRemovedLinesBelow();
|
|
70 if (added !is 0 || changed !is 0) {
|
|
71 hunks.add(new Hunk(lines - changed, added, changed));
|
|
72 added= 0;
|
|
73 changed= 0;
|
|
74 }
|
|
75 }
|
|
76
|
|
77 return (Hunk[]) hunks.toArray(new Hunk[hunks.size()]);
|
|
78 }
|
|
79 private HunkComputer() {
|
|
80 }
|
|
81 }
|