Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9a69d52642d89d0ab56f6f5dc4cb3a9818da67c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
 * Copyright (c) 2000, 2015 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jface.text.revisions;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.eclipse.swt.graphics.RGB;

import org.eclipse.jface.internal.text.revisions.ChangeRegion;
import org.eclipse.jface.internal.text.revisions.Hunk;
import org.eclipse.jface.internal.text.revisions.Range;

import org.eclipse.jface.text.IInformationControlCreator;
import org.eclipse.jface.text.source.ILineRange;

/**
 * Describes a revision of a document. A revision consists of one ore more {@link ILineRange}s.
 * <p>
 * Clients may subclass.
 * </p>
 *
 * @since 3.2
 */
public abstract class Revision {
	/** The original list of change regions. */
	private final List<ChangeRegion> fChangeRegions= new ArrayList<>();
	/**
	 * The cached list of adjusted ranges. <code>null</code>
	 * if the list must be re-computed. Unmodifiable.
	 *
	 * @since 3.3
	 */
	private List<RevisionRange> fRanges= null;

	/**
	 * Creates a new revision.
	 */
	protected Revision() {
	}

	/**
	 * Adds a line range to this revision. The range must be non-empty and have a legal start line
	 * (not -1).
	 *
	 * @param range a line range that was changed with this revision
	 * @throws IndexOutOfBoundsException if the line range is empty or has a negative start line
	 */
	public final void addRange(ILineRange range) throws IndexOutOfBoundsException {
		fChangeRegions.add(new ChangeRegion(this, range));
	}

	/**
	 * Returns the contained {@link RevisionRange}s adapted to the current diff state. The returned
	 * information is only valid at the moment it is returned, and may change as the annotated
	 * document is modified.
	 *
	 * @return an unmodifiable view of the contained ranges
	 */
	public final List<RevisionRange> getRegions() {
		if (fRanges == null) {
			List<RevisionRange> ranges= new ArrayList<>(fChangeRegions.size());
			for (Iterator<ChangeRegion> it= fChangeRegions.iterator(); it.hasNext();) {
				ChangeRegion region= it.next();
				for (Iterator<Range> inner= region.getAdjustedRanges().iterator(); inner.hasNext();) {
					ILineRange range= inner.next();
					ranges.add(new RevisionRange(this, range));
				}
			}
			fRanges= Collections.unmodifiableList(ranges);
		}
		return fRanges;
	}

	/**
	 * Adjusts the revision information to the given diff information. Any previous diff information
	 * is discarded.
	 *
	 * @param hunks the diff hunks to adjust the revision information to
	 * @since 3.3
	 */
	final void applyDiff(Hunk[] hunks) {
		fRanges= null; // mark for recomputation
		for (Iterator<ChangeRegion> regions= fChangeRegions.iterator(); regions.hasNext();) {
			ChangeRegion region= regions.next();
			region.clearDiff();
			for (int i= 0; i < hunks.length; i++) {
				Hunk hunk= hunks[i];
				region.adjustTo(hunk);
			}
		}
	}

	/**
	 * Returns the hover information that will be shown when the user hovers over the a change
	 * region of this revision.
	 * <p>
	 * <strong>Note:</strong> The hover information control which is used to display the information
	 * must be able process the given object. If the default information control creator is used
	 * the supported format is simple text, full HTML or an HTML fragment.
	 * </p>
	 *
	 * @return the hover information for this revision or <code>null</code> for no hover
	 * @see RevisionInformation#setHoverControlCreator(IInformationControlCreator)
	 */
	public abstract Object getHoverInfo();

	/**
	 * Returns the author color for this revision. This color can be used to visually distinguish
	 * one revision from another, for example as background color.
	 * <p>
	 * Revisions from the same author must return the same color and revisions from different authors
	 * must return distinct colors.</p>
	 *
	 * @return the RGB color for this revision's author
	 */
	public abstract RGB getColor();

	/**
	 * Returns the unique (within the document) id of this revision. This may be the version string
	 * or a different identifier.
	 *
	 * @return the id of this revision
	 */
	public abstract String getId();

	/**
	 * Returns the modification date of this revision.
	 *
	 * @return the modification date of this revision
	 */
	public abstract Date getDate();

	@Override
	public String toString() {
		return "Revision " + getId(); //$NON-NLS-1$
	}

	/**
	 * Returns the display string for the author of this revision.
	 * <p>
	 * Subclasses should replace - the default implementation returns the empty string.
	 * </p>
	 *
	 * @return the author name
	 * @since 3.3
	 */
	public String getAuthor() {
		return ""; //$NON-NLS-1$
	}
}

Back to the top