Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Wolf2021-06-10 09:57:13 +0000
committerMickael Istria2021-06-10 17:02:38 +0000
commita399de39bf42ddb450ee76a2cc9e926dd8bcfbb7 (patch)
treec6e9cc27e7542bf0a0403a4e2aa17bd3ef812ad9
parentb86fe7cd8bf0a0258dcbfe694061d7b5fbe3791b (diff)
downloadeclipse.platform.text-a399de39bf42ddb450ee76a2cc9e926dd8bcfbb7.tar.gz
eclipse.platform.text-a399de39bf42ddb450ee76a2cc9e926dd8bcfbb7.tar.xz
eclipse.platform.text-a399de39bf42ddb450ee76a2cc9e926dd8bcfbb7.zip
Bug 558656 - URLHyperlinkDetector: exclude trailing periodsI20210610-1850
While technically allowed, URLs in practice don't end with periods. Exclude them from the match; this gives better results in plain text when a URL occurs at a sentence end. Only periods are excluded; other punctuation is not handled. Change-Id: I290d6b951c1ab196dd3420719653c76527a032ed Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch> Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.text/+/181769 Reviewed-by: Mickael Istria <mistria@redhat.com> Tested-by: Platform Bot <platform-bot@eclipse.org>
-rw-r--r--org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java10
-rw-r--r--org.eclipse.jface.text/src/org/eclipse/jface/text/hyperlink/URLHyperlinkDetector.java11
2 files changed, 17 insertions, 4 deletions
diff --git a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java
index 78acc4d47b8..8e79f41bd99 100644
--- a/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java
+++ b/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextViewerTest.java
@@ -57,6 +57,7 @@ import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.text.hyperlink.IHyperlink;
+import org.eclipse.jface.text.hyperlink.URLHyperlink;
import org.eclipse.jface.text.hyperlink.URLHyperlinkDetector;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.tests.util.DisplayHelper;
@@ -382,7 +383,11 @@ public class TextViewerTest {
return Arrays.stream(links).map(l -> {
IRegion region= l.getHyperlinkRegion();
try {
- return document.get(region.getOffset(), region.getLength());
+ String fromDocument= document.get(region.getOffset(), region.getLength());
+ if (l instanceof URLHyperlink) {
+ assertEquals(((URLHyperlink) l).getURLString(), fromDocument);
+ }
+ return fromDocument;
} catch (BadLocationException e) {
return "Invalid region <" + region + '>';
}
@@ -417,6 +422,9 @@ public class TextViewerTest {
checkHyperlink(textViewer, 24, "https:// foo https://bar bar", "[https://bar]");
checkHyperlink(textViewer, 15, "<a href=\"test:https://bugs.eclipse.org/bugs\"></a>", "[https://bugs.eclipse.org/bugs]");
checkHyperlink(textViewer, 19, "<a href=\"scm:git:https://bugs.eclipse.org/bugs\"></a>", "[https://bugs.eclipse.org/bugs]");
+ checkHyperlink(textViewer, 40, "Find more information at https://www.eclipse.org.", "[https://www.eclipse.org]");
+ checkHyperlink(textViewer, 3, "http://... links should not be used anymore; use https://... instead.", "[]");
+ checkHyperlink(textViewer, 50, "http://... links should not be used anymore; use https://... instead.", "[]");
} finally {
shell.dispose();
}
diff --git a/org.eclipse.jface.text/src/org/eclipse/jface/text/hyperlink/URLHyperlinkDetector.java b/org.eclipse.jface.text/src/org/eclipse/jface/text/hyperlink/URLHyperlinkDetector.java
index 6221db3548d..2ad08986701 100644
--- a/org.eclipse.jface.text/src/org/eclipse/jface/text/hyperlink/URLHyperlinkDetector.java
+++ b/org.eclipse.jface.text/src/org/eclipse/jface/text/hyperlink/URLHyperlinkDetector.java
@@ -110,18 +110,23 @@ public class URLHyperlinkDetector extends AbstractHyperlinkDetector {
}
}
// Right to "://"
- int end= urlSeparatorOffset + 3;
+ int afterSeparator= urlSeparatorOffset + 3;
+ int end= afterSeparator;
while (end < lineEnd && STOP_CHARACTERS.indexOf(line.charAt(end)) < 0) {
end++;
}
- if (end > urlSeparatorOffset + 3) {
+ // Remove trailing periods.
+ while (end > afterSeparator && line.charAt(end - 1) == '.') {
+ end--;
+ }
+ if (end > afterSeparator) {
urlLength= end - urlOffsetInLine;
if (offsetInLine >= urlOffsetInLine && offsetInLine <= urlOffsetInLine + urlLength) {
break;
}
}
- urlSeparatorOffset= line.indexOf("://", urlSeparatorOffset + 3); //$NON-NLS-1$
+ urlSeparatorOffset= line.indexOf("://", afterSeparator); //$NON-NLS-1$
}
if (urlSeparatorOffset < 0)

Back to the top