Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4a40f5b29b72ad5b3259cd154c2432eeadf4f604 (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
/*******************************************************************************
 * Copyright (c) 2008 Alexander Kurtakov.
 * 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:
 *    Alexander Kurtakov - initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.internal.rpm.ui.editor.hyperlink;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.core.resources.IFile;
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.hyperlink.AbstractHyperlinkDetector;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.linuxtools.internal.rpm.ui.editor.ISpecfileSpecialSymbols;
import org.eclipse.linuxtools.internal.rpm.ui.editor.parser.SpecfileSource;
import org.eclipse.linuxtools.rpm.ui.editor.parser.Specfile;
import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileDefine;
import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileElement;
import org.eclipse.linuxtools.rpm.ui.editor.parser.SpecfileParser;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

import com.ibm.icu.util.StringTokenizer;

/**
 * Hyperlink detector for source, patch and defines in the spec file.
 *
 */
public class SpecfileElementHyperlinkDetector extends AbstractHyperlinkDetector {

	private static final String PATCH_IDENTIFIER = "%patch"; //$NON-NLS-1$
	private static final String SOURCE_IDENTIFIER = "%{SOURCE"; //$NON-NLS-1$
	private Specfile specfile;

	@Override
	public IHyperlink[] detectHyperlinks(ITextViewer textViewer,
			IRegion region, boolean canShowMultipleHyperlinks) {

		if (region == null || textViewer == null) {
			return null;
		}

		IDocument document = textViewer.getDocument();
		if (document == null) {
			return null;
		}

		if (specfile == null) {
			SpecfileParser parser = new SpecfileParser();
			specfile = parser.parse(document);
		}


		int offset = region.getOffset();

		IRegion lineInfo;
		String line;
		try {
			lineInfo = document.getLineInformationOfOffset(offset);
			line = document.get(lineInfo.getOffset(), lineInfo.getLength());
		} catch (BadLocationException ex) {
			return null;
		}

		int offsetInLine = offset - lineInfo.getOffset();

		StringTokenizer tokens = new StringTokenizer(line);
		String word = ""; //$NON-NLS-1$
		int tempLineOffset = 0;
		int wordOffsetInLine = 0;
		while (tokens.hasMoreTokens()) {
			String tempWord = tokens.nextToken();
			Pattern defineRegexp = Pattern.compile("%\\{(.*?)\\}"); //$NON-NLS-1$
			Matcher fit = defineRegexp.matcher(tempWord);
			while (fit.find()){
				if ((fit.start()+tempLineOffset <= offsetInLine) && (offsetInLine <= fit.end()+tempLineOffset)){
					tempWord = fit.group();
					wordOffsetInLine = fit.start();
					tempLineOffset += fit.start();
					break;
				}
			}
			tempLineOffset += tempWord.length();
			word = tempWord;
			if (tempLineOffset > offsetInLine) {
				break;
			}
		}
		if (word.startsWith(SOURCE_IDENTIFIER)) {
			int sourceNumber = Integer.valueOf(
					word.substring(SOURCE_IDENTIFIER.length(),
							word.length() - 1)).intValue();
			SpecfileSource source = specfile.getSource(sourceNumber);
			if (source != null) {
				return prepareHyperlink(lineInfo, line, word, source);
			}
		} else if (word.startsWith(PATCH_IDENTIFIER)) {

			int sourceNumber = Integer.valueOf(
					word.substring(PATCH_IDENTIFIER.length(), word.length()))
					.intValue();
			SpecfileSource source = specfile.getPatch(sourceNumber);
			if (source != null) {
				return prepareHyperlink(lineInfo, line, word, source);
			}
		} else {
			String defineName = getDefineName(word);
			SpecfileDefine define = specfile.getDefine(defineName);
			if (define != null) {
				return prepareHyperlink(lineInfo, line, defineName, define,
						wordOffsetInLine);
			}
		}
		return null;
	}

	private String getDefineName(String word) {
		if (word.startsWith(ISpecfileSpecialSymbols.MACRO_START_LONG)) {
			return word.substring(2, word.length() - 1);
		}
		return ""; //$NON-NLS-1$
	}

	private IHyperlink[] prepareHyperlink(IRegion lineInfo, String line,
			String word, SpecfileElement source, int lineIndex) {
		IRegion urlRegion = new Region(lineInfo.getOffset()
				+ line.indexOf(word, lineIndex), word.length());

		IWorkbench wb = PlatformUI.getWorkbench();
		IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
		IWorkbenchPage page = win.getActivePage();
		IEditorPart editor = page.getActiveEditor();
		// A IFile cannot be retrieve from a IFileStoreEditorInput, so at this time
		// we can only provide this functionality for resources inside the workbench.
		if (editor.getEditorInput() instanceof FileEditorInput) {
			IFile original = ((FileEditorInput) editor.getEditorInput()).getFile();
			return new IHyperlink[] { new SpecfileElementHyperlink(urlRegion,
					source, original) };
		} else {
			return null;
		}


	}

	private IHyperlink[] prepareHyperlink(IRegion lineInfo, String line,
			String word, SpecfileElement source) {
		return prepareHyperlink(lineInfo, line, word, source, 0);
	}

	public void setSpecfile(Specfile specfile) {
		this.specfile = specfile;
	}
}

Back to the top