Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 64ebc6d42c30bed673776fa6caa351257d21956e (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*******************************************************************************
 * Copyright (C) 2015 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.dialogs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.JFaceColors;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.URLHyperlinkDetector;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.ITokenScanner;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.annotations.Nullable;
import org.eclipse.swt.graphics.Color;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.texteditor.AbstractTextEditor;

/**
 * A simple {@link ITokenScanner} that recognizes hyperlinks using
 * {@link IHyperlinkDetector}s.
 */
public class HyperlinkTokenScanner implements ITokenScanner {

	private static final String URL_HYPERLINK_DETECTOR_KEY = "org.eclipse.ui.internal.editors.text.URLHyperlinkDetector"; //$NON-NLS-1$

	private int tokenStart;

	private int lastLineStart;

	private IToken hyperlinkToken;

	private Set<IHyperlinkDetector> hyperlinkDetectors;

	private final ISourceViewer viewer;

	private final SourceViewerConfiguration configuration;

	/**
	 * The preference store to use to look up hyperlinking-related preferences.
	 */
	private final IPreferenceStore preferenceStore;

	/**
	 * Caches all hyperlinks on a line to avoid calling the hyperlink detectors
	 * too often.
	 */
	private final List<IHyperlink> hyperlinksOnLine = new ArrayList<>();

	/** The current offset in the document. */
	protected int currentOffset;

	/** The end of the range to tokenize. */
	protected int endOfRange;

	/** The {@link IDocument} the current scan operates on. */
	protected IDocument document;

	/** The {@link IToken} to use for default content. */
	protected final IToken defaultToken;

	/**
	 * Creates a new instance that uses the given hyperlink detector and viewer.
	 *
	 * @param configuration
	 *            the {@link SourceViewerConfiguration}s to get the
	 *            {@link IHyperlinkDetector}s from
	 * @param viewer
	 *            the {@link ISourceViewer} to operate in
	 */
	public HyperlinkTokenScanner(SourceViewerConfiguration configuration,
			ISourceViewer viewer) {
		this(configuration, viewer, null);
	}

	/**
	 * Creates a new instance that uses the given hyperlink detector and viewer.
	 *
	 * @param configuration
	 *            the {@link SourceViewerConfiguration}s to get the
	 *            {@link IHyperlinkDetector}s from
	 * @param viewer
	 *            the {@link ISourceViewer} to operate in
	 * @param defaultAttribute
	 *            the {@link TextAttribute} to use for the default token; may be
	 *            {@code null} to use the default style of the viewer
	 */
	public HyperlinkTokenScanner(SourceViewerConfiguration configuration,
			ISourceViewer viewer, @Nullable TextAttribute defaultAttribute) {
		this(configuration, viewer, null, defaultAttribute);
	}

	/**
	 * Creates a new instance that uses the given hyperlink detector and viewer.
	 *
	 * @param configuration
	 *            the {@link SourceViewerConfiguration}s to get the
	 *            {@link IHyperlinkDetector}s from
	 * @param viewer
	 *            the {@link ISourceViewer} to operate in
	 * @param preferenceStore
	 *            to use to look up preferences related to hyperlinking
	 * @param defaultAttribute
	 *            the {@link TextAttribute} to use for the default token; may be
	 *            {@code null} to use the default style of the viewer
	 */
	protected HyperlinkTokenScanner(SourceViewerConfiguration configuration,
			ISourceViewer viewer, @Nullable IPreferenceStore preferenceStore,
			@Nullable TextAttribute defaultAttribute) {
		this.viewer = viewer;
		this.defaultToken = new Token(defaultAttribute);
		this.configuration = configuration;
		this.preferenceStore = preferenceStore == null
				? EditorsUI.getPreferenceStore() : preferenceStore;
	}

	@Override
	public void setRange(IDocument document, int offset, int length) {
		Assert.isNotNull(document);
		setRangeAndColor(document, offset, length, JFaceColors
				.getHyperlinkText(viewer.getTextWidget().getDisplay()));
	}

	@Override
	public IToken nextToken() {
		tokenStart = currentOffset;
		if (currentOffset >= endOfRange) {
			hyperlinksOnLine.clear();
			return Token.EOF;
		}
		if (hyperlinkDetectors != null && !hyperlinkDetectors.isEmpty()) {
			try {
				IRegion currentLine = document
						.getLineInformationOfOffset(currentOffset);
				if (currentLine.getOffset() != lastLineStart) {
					// Compute all hyperlinks in the line
					hyperlinksOnLine.clear();
					Iterator<IHyperlinkDetector> detectors = hyperlinkDetectors
							.iterator();
					while (detectors.hasNext()) {
						IHyperlinkDetector hyperlinkDetector = detectors.next();
						// The NoMaskHyperlinkDetectors can be skipped; if there
						// are any, they're only duplicates of others to ensure
						// hyperlinks open also if no modifier key is active.
						if (hyperlinkDetector instanceof HyperlinkSourceViewer.NoMaskHyperlinkDetector) {
							continue;
						}
						IHyperlink[] newLinks = null;
						try {
							newLinks = hyperlinkDetector.detectHyperlinks(
									viewer, currentLine, false);
						} catch (RuntimeException e) {
							// Do *not* log: we have no way of identifying the
							// broken hyperlink detector to ignore it in the
							// future. Since we re-get the contributed detectors
							// frequently, we'll get new instances of
							// HyperlinkDetectorDelegate, and that doesn't give
							// access to the extension id. So even if we remove
							// the detector here, we may acquire a new broken
							// instance again and then log over and over again,
							// which is hyper-bothersome if the error log is
							// open and set to activate on new errors. And
							// anyway the problem is not in EGit.
							detectors.remove();
						}
						if (newLinks != null && newLinks.length > 0) {
							Collections.addAll(hyperlinksOnLine, newLinks);
						}
					}
					// Sort them by offset, and with increasing length
					Collections.sort(hyperlinksOnLine,
							new Comparator<IHyperlink>() {
								@Override
								public int compare(IHyperlink a, IHyperlink b) {
									int diff = a.getHyperlinkRegion()
											.getOffset()
											- b.getHyperlinkRegion()
													.getOffset();
									if (diff != 0) {
										return diff;
									}
									return a.getHyperlinkRegion().getLength()
											- b.getHyperlinkRegion()
													.getLength();
								}
							});
					lastLineStart = currentLine.getOffset();
				}
				if (!hyperlinksOnLine.isEmpty()) {
					// Find first hyperlink for the position. We may have to
					// skip a few in case there are several hyperlinks at the
					// same position and with the same length.
					Iterator<IHyperlink> iterator = hyperlinksOnLine.iterator();
					while (iterator.hasNext()) {
						IHyperlink next = iterator.next();
						IRegion linkRegion = next.getHyperlinkRegion();
						int linkEnd = linkRegion.getOffset()
								+ linkRegion.getLength();
						if (currentOffset >= linkEnd) {
							iterator.remove();
						} else if (linkRegion.getOffset() <= currentOffset) {
							// This is our link
							iterator.remove();
							int end = Math.min(endOfRange, linkEnd);
							if (end > currentOffset) {
								currentOffset = end;
								return hyperlinkToken;
							}
						} else {
							// Next hyperlink is beyond current position
							break;
						}
					}
				}
			} catch (BadLocationException e) {
				// Ignore and keep going
			}
		}
		int actualOffset = currentOffset;
		IToken token = scanToken();
		if (token != null && actualOffset < currentOffset) {
			return token;
		}
		currentOffset = actualOffset + 1;
		return defaultToken;
	}

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

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

	/**
	 * Configures the scanner by providing access to the document range that
	 * should be scanned, plus defining the foreground color to use for
	 * hyperlink syntax coloring.
	 *
	 * @param document
	 *            the document to scan
	 * @param offset
	 *            the offset of the document range to scan
	 * @param length
	 *            the length of the document range to scan
	 * @param color
	 *            the foreground color to use for hyperlinks; may be
	 *            {@code null} in which case the default color is applied
	 */
	protected void setRangeAndColor(@NonNull IDocument document, int offset,
			int length, @Nullable Color color) {
		Assert.isTrue(document == viewer.getDocument());
		this.document = document;
		this.lastLineStart = -1;
		this.endOfRange = offset + length;
		this.currentOffset = offset;
		this.tokenStart = -1;
		this.hyperlinkToken = new Token(
				new TextAttribute(color, null, TextAttribute.UNDERLINE));
		this.hyperlinkDetectors = getHyperlinkDetectors();
	}

	/**
	 * Invoked if there is no hyperlink at the current position; may check for
	 * additional tokens. If a token is found, must advance currentOffset and
	 * return the token.
	 *
	 * @return the {@link IToken}, or {@code null} if none.
	 */
	protected IToken scanToken() {
		return null;
	}

	private @NonNull Set<IHyperlinkDetector> getHyperlinkDetectors() {
		Set<IHyperlinkDetector> allDetectors = new LinkedHashSet<>();
		IHyperlinkDetector[] configuredDetectors = configuration
				.getHyperlinkDetectors(viewer);
		if (configuredDetectors != null && configuredDetectors.length > 0) {
			for (IHyperlinkDetector detector : configuredDetectors) {
				allDetectors.add(detector);
			}
			if (preferenceStore.getBoolean(URL_HYPERLINK_DETECTOR_KEY)
					|| !preferenceStore.getBoolean(
							AbstractTextEditor.PREFERENCE_HYPERLINKS_ENABLED)) {
				return allDetectors;
			}
			// URLHyperlinkDetector can only detect hyperlinks at the start of
			// the range. We need one that can detect all hyperlinks in a given
			// region.
			allDetectors.add(new MultiURLHyperlinkDetector());
		}
		return allDetectors;
	}

	/**
	 * A {@link URLHyperlinkDetector} that returns all hyperlinks in a region.
	 * <p>
	 * This internal class assumes that the region is either empty or else spans
	 * a whole line.
	 * </p>
	 */
	private static class MultiURLHyperlinkDetector
			extends URLHyperlinkDetector {

		@Override
		public IHyperlink[] detectHyperlinks(ITextViewer textViewer,
				IRegion region, boolean canShowMultipleHyperlinks) {
			if (region.getLength() == 0) {
				return super.detectHyperlinks(textViewer, region,
						canShowMultipleHyperlinks);
			}
			// URLHyperlinkDetector only finds hyperlinks at the region start.
			// We know here that the given region spans a whole line since we're
			// only called from HyperlinkTokenScanner.nextToken().
			try {
				String line = textViewer.getDocument().get(region.getOffset(),
						region.getLength());
				int currentOffset = region.getOffset();
				int lineStart = currentOffset;
				int regionEnd = currentOffset + region.getLength();
				List<IHyperlink> allLinks = new ArrayList<>();
				while (currentOffset < regionEnd) {
					IHyperlink[] newLinks = super.detectHyperlinks(
							textViewer, new Region(currentOffset, 0),
							canShowMultipleHyperlinks);
					currentOffset++;
					if (newLinks != null && newLinks.length > 0) {
						Collections.addAll(allLinks, newLinks);
						for (IHyperlink link : newLinks) {
							int end = link.getHyperlinkRegion().getOffset()
									+ link.getHyperlinkRegion().getLength();
							if (end > currentOffset) {
								currentOffset = end;
							}
						}
					}
					// Advance to the next "://" combination.
					int nextCandidatePos = lineStart
							+ line.indexOf("://", currentOffset - lineStart); //$NON-NLS-1$
					if (nextCandidatePos > currentOffset) {
						currentOffset = nextCandidatePos;
					} else if (nextCandidatePos < currentOffset) {
						// No more links.
						break;
					}
				}
				return allLinks.toArray(new IHyperlink[allLinks.size()]);
			} catch (BadLocationException e) {
				return null;
			}
		}

	}
}

Back to the top