diff options
2 files changed, 142 insertions, 0 deletions
diff --git a/gerrit/org.eclipse.mylyn.gerrit.ui.tests/src/org/eclipse/mylyn/internal/gerrit/ui/ChangeIdHyperlinkTest.java b/gerrit/org.eclipse.mylyn.gerrit.ui.tests/src/org/eclipse/mylyn/internal/gerrit/ui/ChangeIdHyperlinkTest.java new file mode 100644 index 000000000..8f83052e4 --- /dev/null +++ b/gerrit/org.eclipse.mylyn.gerrit.ui.tests/src/org/eclipse/mylyn/internal/gerrit/ui/ChangeIdHyperlinkTest.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2011 GitHub Inc. 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: + * GitHub Inc. - initial API and implementation + *******************************************************************************/ +package org.eclipse.mylyn.internal.gerrit.ui; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import org.eclipse.jface.text.hyperlink.IHyperlink; +import org.eclipse.mylyn.internal.gerrit.core.GerritConnector; +import org.eclipse.mylyn.tasks.core.TaskRepository; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit tests of + * {@link GerritConnectorUi#findHyperlinks(org.eclipse.mylyn.tasks.core.TaskRepository, org.eclipse.mylyn.tasks.core.ITask, String, int, int)} + * + * @author Kevin Sawicki + */ +public class ChangeIdHyperlinkTest { + + private TaskRepository repo; + + private GerritConnectorUi connector; + + private void checkLink(IHyperlink link) { + assertNotNull(link); + assertNotNull(link.getHyperlinkRegion()); + assertTrue(link.getHyperlinkRegion().getLength() > 0); + } + + @Before + public void setUp() { + connector = new GerritConnectorUi(); + repo = new TaskRepository(GerritConnector.CONNECTOR_KIND, "http://localhost"); //$NON-NLS-1$ + } + + /** + * Test with empty text + */ + @Test + public void emptyText() { + IHyperlink[] links = connector.findHyperlinks(repo, null, "", -1, 0); //$NON-NLS-1$ + assertNull(links); + } + + /** + * Test with invalid hex segment of change id (ends with 't') + */ + @Test + public void invalidHexPortion() { + String changeId = "Change-Id: I9a2336216f0bc1256073bff692c087cfebff8ct"; //$NON-NLS-1$ + IHyperlink[] links = connector.findHyperlinks(repo, null, changeId, -1, 0); + assertNull(links); + } + + /** + * Test text with single hyperlink and no padding + */ + @Test + public void singleHyperlinkNoPadding() { + String changeId = "Change-Id: I9a2336216f0bc1256073bff692c087cfbebff8cc"; //$NON-NLS-1$ + IHyperlink[] links = connector.findHyperlinks(repo, null, changeId, -1, 0); + assertNotNull(links); + assertEquals(1, links.length); + checkLink(links[0]); + } + + /** + * Test text with single hyperlink with padding prefix + */ + @Test + public void singleHyperlinkWithPadding() { + String changeId = " Change-Id: I9a2336216f0bc1256073bff692c087cfbebff8cc"; //$NON-NLS-1$ + IHyperlink[] links = connector.findHyperlinks(repo, null, changeId, -1, 0); + assertNotNull(links); + assertEquals(1, links.length); + checkLink(links[0]); + assertEquals(4, links[0].getHyperlinkRegion().getOffset()); + } + + /** + * Test text with two hyperlinks on different lines + */ + @Test + public void twoHyperlinks() { + String change1 = "Change-Id: I9a2336216f0bc1256073bff692c087cfbebff8cc"; //$NON-NLS-1$ + String change2 = "Change-Id: I9a2336216f0bc1256073bff692c087cfbebff8cf"; //$NON-NLS-1$ + IHyperlink[] links = connector.findHyperlinks(repo, null, change1 + "\n" + change2, -1, 0); //$NON-NLS-1$ + assertNotNull(links); + assertEquals(2, links.length); + for (IHyperlink link : links) { + checkLink(link); + } + assertEquals(0, links[0].getHyperlinkRegion().getOffset()); + assertEquals(change1.length() + 1, links[1].getHyperlinkRegion().getOffset()); + } +} diff --git a/gerrit/org.eclipse.mylyn.gerrit.ui/src/org/eclipse/mylyn/internal/gerrit/ui/GerritConnectorUi.java b/gerrit/org.eclipse.mylyn.gerrit.ui/src/org/eclipse/mylyn/internal/gerrit/ui/GerritConnectorUi.java index f1bcae2fd..65d5157bc 100644 --- a/gerrit/org.eclipse.mylyn.gerrit.ui/src/org/eclipse/mylyn/internal/gerrit/ui/GerritConnectorUi.java +++ b/gerrit/org.eclipse.mylyn.gerrit.ui/src/org/eclipse/mylyn/internal/gerrit/ui/GerritConnectorUi.java @@ -8,10 +8,18 @@ * Contributors:
* Sony Ericsson/ST Ericsson - initial API and implementation
* Tasktop Technologies - improvements
+ * GitHub Inc. - fixes for bug 355179
*********************************************************************/
package org.eclipse.mylyn.internal.gerrit.ui;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.jface.text.Region;
+import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.IWizard;
import org.eclipse.mylyn.internal.gerrit.core.GerritConnector;
@@ -21,6 +29,7 @@ import org.eclipse.mylyn.tasks.core.ITask; import org.eclipse.mylyn.tasks.core.ITaskMapping;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.AbstractRepositoryConnectorUi;
+import org.eclipse.mylyn.tasks.ui.TaskHyperlink;
import org.eclipse.mylyn.tasks.ui.wizards.ITaskRepositoryPage;
import org.eclipse.mylyn.tasks.ui.wizards.ITaskSearchPage;
import org.eclipse.mylyn.tasks.ui.wizards.NewTaskWizard;
@@ -32,9 +41,12 @@ import org.eclipse.mylyn.tasks.ui.wizards.RepositoryQueryWizard; * @author Mikael Kober
* @author Thomas Westling
* @author Steffen Pingel
+ * @author Kevin Sawicki
*/
public class GerritConnectorUi extends AbstractRepositoryConnectorUi {
+ private static final Pattern PATTERN_CHANGE_ID = Pattern.compile("(?:\\W||^)(Change-Id: (I[0-9a-f]{40}))"); //$NON-NLS-1$
+
@Override
public String getConnectorKind() {
return GerritConnector.CONNECTOR_KIND;
@@ -77,4 +89,26 @@ public class GerritConnectorUi extends AbstractRepositoryConnectorUi { return GerritImages.OVERLAY_REVIEW;
}
+ @Override
+ public IHyperlink[] findHyperlinks(final TaskRepository repository, ITask task, String text, int index,
+ int textOffset) {
+ List<IHyperlink> links = null;
+ Matcher matcher = PATTERN_CHANGE_ID.matcher(text);
+ while (matcher.find()) {
+ if (index != -1 && (index < matcher.start() || index > matcher.end())) {
+ continue;
+ }
+ if (links == null) {
+ links = new ArrayList<IHyperlink>();
+ }
+ String key = matcher.group(2);
+ if (task == null || !key.startsWith(task.getTaskKey())) {
+ int start = matcher.start(1);
+ Region region = new Region(textOffset + start, matcher.end(1) - start);
+ links.add(new TaskHyperlink(region, repository, key));
+ }
+ }
+ return links != null ? links.toArray(new IHyperlink[links.size()]) : null;
+ }
+
}
|