Lines 8-13
Link Here
|
8 |
* Contributors: |
8 |
* Contributors: |
9 |
* Tasktop Technologies - initial API and implementation |
9 |
* Tasktop Technologies - initial API and implementation |
10 |
* David Green - fix for bug 266693 |
10 |
* David Green - fix for bug 266693 |
|
|
11 |
* Abner Ballardo - fix for bug 288427 |
11 |
*******************************************************************************/ |
12 |
*******************************************************************************/ |
12 |
|
13 |
|
13 |
package org.eclipse.mylyn.internal.tasks.ui.editors; |
14 |
package org.eclipse.mylyn.internal.tasks.ui.editors; |
Lines 19-25
Link Here
|
19 |
import java.util.regex.Matcher; |
20 |
import java.util.regex.Matcher; |
20 |
import java.util.regex.Pattern; |
21 |
import java.util.regex.Pattern; |
21 |
|
22 |
|
22 |
import org.eclipse.jface.text.IRegion; |
|
|
23 |
import org.eclipse.jface.text.ITextViewer; |
23 |
import org.eclipse.jface.text.ITextViewer; |
24 |
import org.eclipse.jface.text.Region; |
24 |
import org.eclipse.jface.text.Region; |
25 |
import org.eclipse.jface.text.hyperlink.IHyperlink; |
25 |
import org.eclipse.jface.text.hyperlink.IHyperlink; |
Lines 39-44
Link Here
|
39 |
// so we do the same here |
39 |
// so we do the same here |
40 |
private static final Pattern URL_PATTERN = Pattern.compile("([a-zA-Z][a-zA-Z+.-]{0,10}://[a-zA-Z0-9%._~!$&?#'()*+,;:@/=-]*[a-zA-Z0-9%_~!$&?#'(*+;:@/=-])"); //$NON-NLS-1$ |
40 |
private static final Pattern URL_PATTERN = Pattern.compile("([a-zA-Z][a-zA-Z+.-]{0,10}://[a-zA-Z0-9%._~!$&?#'()*+,;:@/=-]*[a-zA-Z0-9%_~!$&?#'(*+;:@/=-])"); //$NON-NLS-1$ |
41 |
|
41 |
|
|
|
42 |
private static final String CLOSED_PARENTHESIS_PATTERN = "[^)]"; //$NON-NLS-1$ |
43 |
|
44 |
private static final String OPEN_PARENTHESIS_PATTERN = "[^(]"; //$NON-NLS-1$ |
45 |
|
46 |
private static final String EMPTY_STRING = ""; //$NON-NLS-1$ |
47 |
|
42 |
public TaskUrlHyperlinkDetector() { |
48 |
public TaskUrlHyperlinkDetector() { |
43 |
} |
49 |
} |
44 |
|
50 |
|
Lines 49-67
Link Here
|
49 |
Matcher m = URL_PATTERN.matcher(content); |
55 |
Matcher m = URL_PATTERN.matcher(content); |
50 |
while (m.find()) { |
56 |
while (m.find()) { |
51 |
if (isInRegion(indexInContent, m)) { |
57 |
if (isInRegion(indexInContent, m)) { |
52 |
String urlString = m.group(1); |
58 |
String urlString = getUrlString(content, m); |
53 |
TaskUrlHyperlink link = null; |
59 |
TaskUrlHyperlink link = null; |
54 |
if (getAdapter(TaskRepository.class) != null) { |
60 |
if (getAdapter(TaskRepository.class) != null) { |
55 |
try { |
61 |
try { |
56 |
new URL(urlString); |
62 |
new URL(urlString); |
57 |
link = new TaskUrlHyperlink(determineRegion(contentOffset, m), urlString); |
63 |
link = createTaskUrlHyperlink(contentOffset, m, urlString); |
58 |
} catch (MalformedURLException e) { |
64 |
} catch (MalformedURLException e) { |
59 |
// ignore |
65 |
// ignore |
60 |
} |
66 |
} |
61 |
|
67 |
|
62 |
} else { |
68 |
} else { |
63 |
if (TasksUiInternal.isTaskUrl(urlString)) { |
69 |
if (TasksUiInternal.isTaskUrl(urlString)) { |
64 |
link = new TaskUrlHyperlink(determineRegion(contentOffset, m), urlString); |
70 |
link = createTaskUrlHyperlink(contentOffset, m, urlString); |
65 |
} |
71 |
} |
66 |
} |
72 |
} |
67 |
|
73 |
|
Lines 76-87
Link Here
|
76 |
return links; |
82 |
return links; |
77 |
} |
83 |
} |
78 |
|
84 |
|
|
|
85 |
private String getUrlString(String content, Matcher m) { |
86 |
String urlString = m.group(1); |
87 |
int parenthesisDiff = urlString.replaceAll(OPEN_PARENTHESIS_PATTERN, EMPTY_STRING).length() |
88 |
- urlString.replaceAll(CLOSED_PARENTHESIS_PATTERN, EMPTY_STRING).length(); |
89 |
|
90 |
if (parenthesisDiff > 0) { |
91 |
for (int i = m.end(); i - m.end() < parenthesisDiff; i++) { |
92 |
if (i >= content.length() || content.charAt(i) != ')') { |
93 |
break; |
94 |
} |
95 |
urlString += ')'; |
96 |
} |
97 |
} |
98 |
return urlString; |
99 |
} |
100 |
|
79 |
private static boolean isInRegion(int offsetInText, Matcher m) { |
101 |
private static boolean isInRegion(int offsetInText, Matcher m) { |
80 |
return (offsetInText == -1) || (offsetInText >= m.start() && offsetInText <= m.end()); |
102 |
return (offsetInText == -1) || (offsetInText >= m.start() && offsetInText <= m.end()); |
81 |
} |
103 |
} |
82 |
|
104 |
|
83 |
private static IRegion determineRegion(int textOffset, Matcher m) { |
105 |
private static TaskUrlHyperlink createTaskUrlHyperlink(int textOffset, Matcher m, String urlString) { |
84 |
return new Region(textOffset + m.start(), m.end() - m.start()); |
106 |
return new TaskUrlHyperlink(new Region(textOffset + m.start(), urlString.length()), urlString); |
85 |
} |
107 |
} |
86 |
|
108 |
|
87 |
} |
109 |
} |