Skip to main content
summaryrefslogtreecommitdiffstats
blob: 423c1cf6e84734e7a5297e365c3597c4c9c5c170 (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
/*******************************************************************************
 * Copyright (c) 2004 - 2006 University Of British Columbia 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:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylyn.internal.bugs.java;

import java.util.List;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Comment;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.TextElement;
import org.eclipse.jdt.internal.corext.dom.NodeFinder;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.javaeditor.ASTProvider;
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
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.hyperlink.IHyperlink;
import org.eclipse.mylyn.internal.bugzilla.ui.BugzillaHyperlinkUtil;
import org.eclipse.mylyn.internal.core.util.MylarStatusHandler;
import org.eclipse.mylyn.provisional.tasklist.TaskRepository;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.texteditor.ITextEditor;

/**
 * @author Shawn Minto
 * Detects bugzilla hyperlinks within source code
 */
public class BugzillaHyperLinkDetector extends AbstractHyperlinkDetector {

	private TaskRepository repository;
	
	public BugzillaHyperLinkDetector(TaskRepository repository) {
		this.repository = repository;
	}
	
	@SuppressWarnings("unchecked")
	public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
		
		ITextEditor textEditor = getEditor();
		if (region == null || textEditor == null || canShowMultipleHyperlinks || !(textEditor instanceof JavaEditor))
			return null;

		IEditorSite site = textEditor.getEditorSite();
		if (site == null)
			return null;

		IJavaElement javaElement;
		Object adapter = textEditor.getEditorInput().getAdapter(IJavaElement.class);
		if (adapter instanceof IJavaElement) {
			javaElement = (IJavaElement)adapter;
		} else {
			return null;
		}
		
		if (javaElement == null)
			return null;

		CompilationUnit ast = JavaPlugin.getDefault().getASTProvider().getAST(javaElement, ASTProvider.WAIT_NO, null);
		if (ast == null)
			return null;

		ASTNode node = NodeFinder.perform(ast, region.getOffset(), 1);

		if (node == null || !(node instanceof TextElement || node instanceof Block))
			return null;

		String comment = null;
		int commentStart = -1;

		if (node instanceof TextElement) {
			TextElement element = (TextElement) node;
			comment = element.getText();
			commentStart = element.getStartPosition();
		} else if (node instanceof Block) {
			Comment c = findComment(ast.getCommentList(), region.getOffset(), 1);
			if (c != null) {
				try {
					IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
					String commentString = document.get(c.getStartPosition(), c.getLength());
					comment = getStringFromComment(c, region.getOffset(), commentString);
					commentStart = getLocationFromComment(c, comment, commentString) + c.getStartPosition();
				} catch (BadLocationException e) {
					MylarStatusHandler.log(e, "Failed to get text for comment");
				}
			}
		}

		if (comment == null)
			return null;

		int startOffset = region.getOffset();
		int endOffset = startOffset + region.getLength();
		
		return BugzillaHyperlinkUtil.findBugHyperlinks(repository.getUrl(), startOffset, endOffset, comment, commentStart);
	}

//	private IHyperlink[] findBugHyperlinks(int startOffset, int endOffset, String comment, int commentStart) {
//
//		Pattern p = Pattern.compile("^.*bug\\s+\\d+.*");
//		Matcher m = p.matcher(comment.toLowerCase().trim());
//		boolean b = m.matches();
//
//		p = Pattern.compile("^.*bug#\\s+\\d+.*");
//		m = p.matcher(comment.toLowerCase().trim());
//		boolean b2 = m.matches();
//
//		p = Pattern.compile("^.*bug\\s#\\d+.*");
//		m = p.matcher(comment.toLowerCase().trim());
//		boolean b3 = m.matches();
//
//		p = Pattern.compile("^.*bug#\\d+.*");
//		m = p.matcher(comment.toLowerCase().trim());
//		boolean b4 = m.matches();
//
//		// XXX walk forward from where we are
//		if (b || b2 || b3 || b4) {
//
//			int start = comment.toLowerCase().indexOf("bug");
//			int ahead = 4;
//			if (b2 || b3 || b4) {
//				int pound = comment.toLowerCase().indexOf("#", start);
//				ahead = pound - start + 1;
//			}
//			String endComment = comment.substring(start + ahead, comment.length());
//			endComment = endComment.trim();
//			int endCommentStart = comment.indexOf(endComment);
//
//			int end = comment.indexOf(" ", endCommentStart);
//			int end2 = comment.indexOf(":", endCommentStart);
//
//			if ((end2 < end && end2 != -1) || (end == -1 && end2 != -1)) {
//				end = end2;
//			}
//
//			if (end == -1)
//				end = comment.length();
//
//			try {
//				int bugId = Integer.parseInt(comment.substring(endCommentStart, end).trim());
//
//				start += commentStart;
//				end += commentStart;
//
//				if (startOffset >= start && endOffset <= end) {
//					IRegion sregion = new Region(start, end - start);
//					return new IHyperlink[] { new BugzillaHyperLink(sregion, bugId) };
//				}
//			} catch (NumberFormatException e) {
//				return null;
//			}
//		}
//		return null;
//	}

	private int getLocationFromComment(Comment c, String commentLine, String commentString) {
		if (commentLine == null) {
			return -1;
		} else {
			return commentString.indexOf(commentLine);
		}
	}

	private String getStringFromComment(Comment comment, int desiredOffset, String commentString) {
		String[] parts = commentString.split("\n");
		if (parts.length > 1) {
			int offset = comment.getStartPosition();
			for (String part : parts) {
				int newOffset = offset + part.length() + 1;
				if (desiredOffset >= offset && desiredOffset <= newOffset) {
					return part;
				}

			}
		} else {
			return commentString;
		}

		return null;
	}

	private Comment findComment(List<Comment> commentList, int offset, int i) {
		for (Comment comment : commentList) {
			if (comment.getStartPosition() <= offset
					&& (comment.getStartPosition() + comment.getLength() >= offset + i)) {
				return comment;
			}
		}
		return null;
	}
}

Back to the top