Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2a0790d26ec4abd06e82bbe31d0037dbdf2c0634 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*******************************************************************************
 * Copyright (C) 2016, Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * 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
 *******************************************************************************/
package org.eclipse.egit.ui.internal.commit;

import java.util.Arrays;
import java.util.regex.Pattern;

import org.eclipse.core.runtime.Assert;
import org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.DiffRegion;
import org.eclipse.egit.ui.internal.commit.DiffRegionFormatter.FileDiffRegion;
import org.eclipse.egit.ui.internal.history.FileDiff;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IDocumentPartitioner;
import org.eclipse.jface.text.TextUtilities;
import org.eclipse.jface.text.rules.FastPartitioner;
import org.eclipse.jface.text.rules.IPartitionTokenScanner;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.lib.Repository;

/**
 * A {@link Document} specialized for displaying unified diffs generated by a
 * {@link DiffRegionFormatter}. Intended usage is to create a DiffDocument, let
 * a DiffRegionFormatter generate into it, and then
 * {@link #connect(DiffRegionFormatter) connect()} the formatter. This will
 * partition the document into regions for file headlines, hunks, and added or
 * removed lines.
 */
public class DiffDocument extends Document {

	static final String HEADLINE_CONTENT_TYPE = "_egit_diff_headline"; //$NON-NLS-1$

	static final String HUNK_CONTENT_TYPE = "_egit_diff_hunk"; //$NON-NLS-1$

	static final String ADDED_CONTENT_TYPE = "_egit_diff_added"; //$NON-NLS-1$

	static final String REMOVED_CONTENT_TYPE = "_egit_diff_removed"; //$NON-NLS-1$

	private DiffRegion[] regions;

	private FileDiffRegion[] fileRegions;

	private Pattern newPathPattern;

	private Pattern oldPathPattern;

	private Repository defaultRepository;

	private FileDiff defaultFileDiff;

	/**
	 * Creates a new {@link DiffDocument}.
	 */
	public DiffDocument() {
		super();
	}

	/**
	 * Creates a new {@link DiffDocument} with initial text.
	 *
	 * @param text
	 *            to set on the document
	 */
	public DiffDocument(String text) {
		super(text);
	}

	/**
	 * Sets up the document to use information from the given
	 * {@link DiffRegionFormatter} for partitioning the document into
	 * partitions for file headlines, hunk headers, and added or removed lines.
	 * It is assumed that the given formatter has been used to generate content
	 * into the document.
	 *
	 * @param formatter
	 *            to obtain information from
	 */
	public void connect(DiffRegionFormatter formatter) {
		regions = formatter.getRegions();
		fileRegions = formatter.getFileRegions();
		if ((fileRegions == null || fileRegions.length == 0)
				&& defaultRepository != null && defaultFileDiff != null) {
			fileRegions = new FileDiffRegion[] { new FileDiffRegion(
					defaultRepository, defaultFileDiff, 0, getLength()) };
		}
		newPathPattern = Pattern.compile(
				Pattern.quote(formatter.getNewPrefix()) + "\\S+"); //$NON-NLS-1$
		oldPathPattern = Pattern.compile(
				Pattern.quote(formatter.getOldPrefix()) + "\\S+"); //$NON-NLS-1$
		// Connect a new partitioner.
		IDocumentPartitioner partitioner = new FastPartitioner(
				new DiffPartitionTokenScanner(),
				new String[] { IDocument.DEFAULT_CONTENT_TYPE,
						HEADLINE_CONTENT_TYPE, HUNK_CONTENT_TYPE,
						ADDED_CONTENT_TYPE, REMOVED_CONTENT_TYPE });
		IDocumentPartitioner oldPartitioner = getDocumentPartitioner();
		if (oldPartitioner != null) {
			oldPartitioner.disconnect();
		}
		partitioner.connect(this);
		setDocumentPartitioner(partitioner);
	}

	/**
	 * Provide default settings about the {@link Repository} and
	 * {@link FileDiff}, to be used in the absence of explicit information from
	 * a connected {@link DiffRegionFormatter}. Useful if the document is
	 * used for only individual edits from a file.
	 *
	 * @param repository
	 *            to use if none set explicitly
	 * @param fileDiff
	 *            to use if none set explicitly
	 */
	public void setDefault(Repository repository, FileDiff fileDiff) {
		defaultRepository = repository;
		defaultFileDiff = fileDiff;
	}

	DiffRegion[] getRegions() {
		return regions;
	}

	FileDiffRegion[] getFileRegions() {
		return fileRegions;
	}

	private int findRegionIndex(int offset) {
		DiffRegion key = new DiffRegion(offset, 0);
		return Arrays.binarySearch(regions, key, (a, b) -> {
			if (!TextUtilities.overlaps(a, b)) {
				return a.getOffset() - b.getOffset();
			}
			return 0;
		});
	}

	DiffRegion findRegion(int offset) {
		int i = findRegionIndex(offset);
		return i >= 0 ? regions[i] : null;
	}

	FileDiffRegion findFileRegion(int offset) {
		FileDiffRegion key = new FileDiffRegion(null, null, offset, 0);
		int i = Arrays.binarySearch(fileRegions, key, (a, b) -> {
			if (!TextUtilities.overlaps(a, b)) {
				return a.getOffset() - b.getOffset();
			}
			return 0;
		});
		return i >= 0 ? fileRegions[i] : null;
	}

	Pattern getPathPattern(DiffEntry.Side side) {
		switch (side) {
		case OLD:
			return oldPathPattern;
		default:
			return newPathPattern;
		}
	}

	private class DiffPartitionTokenScanner implements IPartitionTokenScanner {

		private final Token HEADLINE_TOKEN = new Token(HEADLINE_CONTENT_TYPE);

		private final Token HUNK_TOKEN = new Token(HUNK_CONTENT_TYPE);

		private final Token ADDED_TOKEN = new Token(ADDED_CONTENT_TYPE);

		private final Token DELETED_TOKEN = new Token(REMOVED_CONTENT_TYPE);

		private final Token OTHER_TOKEN = new Token(
				IDocument.DEFAULT_CONTENT_TYPE);

		private int currentOffset;

		private int end;

		private int tokenStart;

		private int currIdx;

		@Override
		public void setRange(IDocument document, int offset, int length) {
			Assert.isLegal(document == DiffDocument.this);
			currentOffset = offset;
			end = offset + length;
			tokenStart = -1;
		}

		@Override
		public IToken nextToken() {
			if (tokenStart < 0) {
				currIdx = findRegionIndex(currentOffset);
				if (currIdx < 0) {
					currIdx = -(currIdx + 1);
				}
			}
			tokenStart = currentOffset;
			if (currentOffset < end) {
				if (currIdx >= DiffDocument.this.regions.length) {
					currentOffset = end;
					return OTHER_TOKEN;
				}
				if (currentOffset < DiffDocument.this.regions[currIdx]
						.getOffset()) {
					currentOffset = DiffDocument.this.regions[currIdx]
							.getOffset();
					return OTHER_TOKEN;
				}
				// We're in range[currIdx]. Typically at the beginning, but if
				// called via setPartialRange, we may also be somewhere in the
				// middle.
				currentOffset += DiffDocument.this.regions[currIdx].getLength()
						- (currentOffset
								- DiffDocument.this.regions[currIdx]
										.getOffset());
				switch (DiffDocument.this.regions[currIdx++].diffType) {
				case HEADLINE:
					return HEADLINE_TOKEN;
				case HUNK:
					return HUNK_TOKEN;
				case ADD:
					return ADDED_TOKEN;
				case REMOVE:
					return DELETED_TOKEN;
				default:
					return OTHER_TOKEN;
				}
			}
			return Token.EOF;
		}

		@Override
		public int getTokenOffset() {
			return tokenStart;
		}

		@Override
		public int getTokenLength() {
			return currentOffset - tokenStart;
		}

		@Override
		public void setPartialRange(IDocument document, int offset, int length,
				String contentType, int partitionOffset) {
			setRange(document, offset, length);
		}

	}
}

Back to the top