Highlighting issue links: ignore invalid replacement patterns (EA-83133)

This commit is contained in:
Alexander Zolotov
2016-06-09 14:26:33 +03:00
parent f1e465883b
commit 1df198f2ef
4 changed files with 30 additions and 14 deletions

View File

@@ -1,5 +1,5 @@
/**
* Fixes ABC-1123 and ABC-2. See details at BBB-22
* Fixes ABC-1123 and ABC-2. See details at BBB-22 and INVALID-43
*/
class IssueLinksInJavaDoc {
// Fixes ABC-22 and ABC-11. See details at BBB-33

View File

@@ -112,8 +112,8 @@ public class JavadocHighlightingTest extends LightDaemonAnalyzerTestCase {
IssueNavigationConfiguration navigationConfiguration = IssueNavigationConfiguration.getInstance(getProject());
List<IssueNavigationLink> oldLinks = navigationConfiguration.getLinks();
try {
IssueNavigationLink link = new IssueNavigationLink("ABC-\\d+", "http://example.com/$0");
navigationConfiguration.setLinks(ContainerUtil.newArrayList(link));
navigationConfiguration.setLinks(ContainerUtil.newArrayList(new IssueNavigationLink("ABC-\\d+", "http://example.com/$0"),
new IssueNavigationLink("INVALID-\\d+", "http://example.com/$0/$1")));
configureByFile(getTestName(false) + ".java");
List<String> expected = ContainerUtil.newArrayList(
"http://example.com/ABC-1123", "http://example.com/ABC-2", "http://example.com/ABC-22", "http://example.com/ABC-11");

View File

@@ -20,6 +20,7 @@ import com.intellij.lifecycle.PeriodicalTasksCloser;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SimpleModificationTracker;
import com.intellij.openapi.util.TextRange;
@@ -37,7 +38,10 @@ import java.util.regex.Pattern;
* @author yole
*/
@State(name = "IssueNavigationConfiguration", storages = @Storage("vcs.xml"))
public class IssueNavigationConfiguration extends SimpleModificationTracker implements PersistentStateComponent<IssueNavigationConfiguration> {
public class IssueNavigationConfiguration extends SimpleModificationTracker
implements PersistentStateComponent<IssueNavigationConfiguration> {
private static final Logger LOG = Logger.getInstance(IssueNavigationConfiguration.class);
public static IssueNavigationConfiguration getInstance(Project project) {
return PeriodicalTasksCloser.getInstance().safeGetService(project, IssueNavigationConfiguration.class);
}
@@ -82,23 +86,27 @@ public class IssueNavigationConfiguration extends SimpleModificationTracker impl
if (!(o instanceof LinkMatch)) {
return 0;
}
LinkMatch rhs = (LinkMatch) o;
return myRange.getStartOffset() - rhs.getRange().getStartOffset();
return myRange.getStartOffset() - ((LinkMatch)o).getRange().getStartOffset();
}
}
public List<LinkMatch> findIssueLinks(CharSequence text) {
final List<LinkMatch> result = new ArrayList<LinkMatch>();
for(IssueNavigationLink link: myLinks) {
for (IssueNavigationLink link : myLinks) {
Pattern issuePattern = link.getIssuePattern();
Matcher m = issuePattern.matcher(text);
while(m.find()) {
String replacement = issuePattern.matcher(m.group(0)).replaceFirst(link.getLinkRegexp());
addMatch(result, new TextRange(m.start(), m.end()), replacement);
while (m.find()) {
try {
String replacement = issuePattern.matcher(m.group(0)).replaceFirst(link.getLinkRegexp());
addMatch(result, new TextRange(m.start(), m.end()), replacement);
}
catch (Exception e) {
LOG.debug("Malformed regex replacement. IssueLink: " + link + "; text: " + text, e);
}
}
}
Matcher m = URLUtil.URL_PATTERN.matcher(text);
while(m.find()) {
while (m.find()) {
addMatch(result, new TextRange(m.start(), m.end()), m.group());
}
Collections.sort(result);
@@ -106,7 +114,7 @@ public class IssueNavigationConfiguration extends SimpleModificationTracker impl
}
private static void addMatch(final List<LinkMatch> result, final TextRange range, final String replacement) {
for (Iterator<LinkMatch> iterator = result.iterator(); iterator.hasNext();) {
for (Iterator<LinkMatch> iterator = result.iterator(); iterator.hasNext(); ) {
LinkMatch oldMatch = iterator.next();
if (range.contains(oldMatch.getRange())) {
iterator.remove();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2000-2009 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,8 +16,8 @@
package com.intellij.openapi.vcs;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.util.regex.Pattern;
@@ -81,4 +81,12 @@ public class IssueNavigationLink {
result = 31 * result + myLinkRegexp.hashCode();
return result;
}
@Override
public String toString() {
return "IssueNavigationLink{" +
"myIssueRegexp='" + myIssueRegexp + '\'' +
", myLinkRegexp='" + myLinkRegexp + '\'' +
'}';
}
}