Bug 558656 - URLHyperlinkDetector: exclude trailing periods

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>
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 78acc4d..8e79f41 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.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 @@
 		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 @@
 			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 6221db3..2ad0898 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 @@
 				}
 			}
 			// 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)