Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b032078a9a622e374ba24bc5581209dac5da4dba (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
/*******************************************************************************
 * Copyright (c) 2006, 2018 Red Hat Inc. and others.
 *
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Kyu Lee <klee@redhat.com> - initial API and implementation
 *    Remy Chi Jian Suen <remy.suen@gmail.com> - clean up internal API references (bug #179389)
 *******************************************************************************/
package org.eclipse.linuxtools.internal.changelog.core.editors;

import org.eclipse.core.filebuffers.FileBuffers;
import org.eclipse.core.filebuffers.ITextFileBuffer;
import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITypedRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.jface.text.rules.Token;

public class GNUHyperlinkDetector extends AbstractHyperlinkDetector {

	private IPath documentLocation;

	/**
	 * Detector using RuleBasedScanner.
	 */
	@Override
	public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
		if (documentLocation == null) {
			ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
			ITextFileBuffer buffer = bufferManager.getTextFileBuffer(textViewer.getDocument());
			if (buffer == null) {
				return null;
			}
			documentLocation = buffer.getLocation().removeLastSegments(1);
		}

		IDocument thisDoc = textViewer.getDocument();

		GNUHyperlinkScanner scanner = new GNUHyperlinkScanner();

		ITypedRegion partitionInfo = null;

		try {
			partitionInfo = thisDoc.getPartition(region.getOffset());
		} catch (org.eclipse.jface.text.BadLocationException e1) {
			e1.printStackTrace();
			return null;
		}

		scanner.setRange(thisDoc, partitionInfo.getOffset(), partitionInfo.getLength());

		Token tmpToken = (Token) scanner.nextToken();

		String tokenStr = (String) tmpToken.getData();

		if (tokenStr == null) {
			return null;
		}

		// try to find non-default token containing region..if none, return
		// null.
		while (region.getOffset() < scanner.getTokenOffset() || region.getOffset() > scanner.getOffset()
				|| tokenStr.equals("_other")) {
			tmpToken = (Token) scanner.nextToken();
			tokenStr = (String) tmpToken.getData();
			if (tokenStr == null)
				return null;
		}

		Region tokenRegion = new Region(scanner.getTokenOffset(), scanner.getTokenLength());

		String line = "";
		try {
			line = thisDoc.get(tokenRegion.getOffset(), tokenRegion.getLength());
		} catch (org.eclipse.jface.text.BadLocationException e1) {
			e1.printStackTrace();
			return null;
		}

		// process file link
		if (tokenStr.equals(GNUHyperlinkScanner.FILE_NAME)) {

			Region pathRegion = null;

			int lineOffset = 0;

			// cut "* " if necessary
			if (line.startsWith("* ")) {
				lineOffset = 2;
				line = line.substring(2);
			}
			pathRegion = new Region(tokenRegion.getOffset() + lineOffset, line.length());

			if (documentLocation == null)
				return null;

			// Replace any escape characters added to name
			line = line.replaceAll("\\\\(.)", "$1");

			IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
			IResource resource = root.findMember(documentLocation.append(line));
			IFile fileLoc = resource.getAdapter(IFile.class);
			if (fileLoc != null && fileLoc.exists()) {
				return new IHyperlink[] { new FileHyperlink(pathRegion, fileLoc) };
			}

		}

		return null;
	}
}

Back to the top