Mercurial > projects > dwt-addons
annotate dwtx/jface/text/hyperlink/URLHyperlinkDetector.d @ 158:25f1f92fa3df
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Tue, 26 Aug 2008 02:46:34 +0200 |
parents | 26688fec6d23 |
children |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Benjamin Muskalla <b.muskalla@gmx.net> - https://bugs.eclipse.org/bugs/show_bug.cgi?id=156433 | |
11 * Port to the D programming language: | |
12 * Frank Benoit <benoit@tionex.de> | |
13 *******************************************************************************/ | |
14 module dwtx.jface.text.hyperlink.URLHyperlinkDetector; | |
15 | |
131 | 16 import dwtx.jface.text.hyperlink.IHyperlinkPresenterExtension; // packageimport |
17 import dwtx.jface.text.hyperlink.MultipleHyperlinkPresenter; // packageimport | |
18 import dwtx.jface.text.hyperlink.HyperlinkManager; // packageimport | |
19 import dwtx.jface.text.hyperlink.URLHyperlink; // packageimport | |
20 import dwtx.jface.text.hyperlink.IHyperlinkDetectorExtension2; // packageimport | |
21 import dwtx.jface.text.hyperlink.IHyperlinkDetector; // packageimport | |
22 import dwtx.jface.text.hyperlink.IHyperlinkPresenter; // packageimport | |
23 import dwtx.jface.text.hyperlink.DefaultHyperlinkPresenter; // packageimport | |
24 import dwtx.jface.text.hyperlink.AbstractHyperlinkDetector; // packageimport | |
25 import dwtx.jface.text.hyperlink.IHyperlinkDetectorExtension; // packageimport | |
26 import dwtx.jface.text.hyperlink.HyperlinkMessages; // packageimport | |
27 import dwtx.jface.text.hyperlink.IHyperlink; // packageimport | |
28 | |
129 | 29 import dwt.dwthelper.utils; |
30 | |
158 | 31 import dwtx.dwtxhelper.MalformedURLException; |
32 import dwtx.dwtxhelper.URL; | |
33 import dwtx.dwtxhelper.StringTokenizer; | |
129 | 34 |
35 import dwtx.jface.text.BadLocationException; | |
36 import dwtx.jface.text.IDocument; | |
37 import dwtx.jface.text.IRegion; | |
38 import dwtx.jface.text.ITextViewer; | |
39 import dwtx.jface.text.Region; | |
40 | |
41 | |
42 /** | |
43 * URL hyperlink detector. | |
44 * | |
45 * @since 3.1 | |
46 */ | |
47 public class URLHyperlinkDetector : AbstractHyperlinkDetector { | |
48 | |
49 | |
50 /** | |
51 * Creates a new URL hyperlink detector. | |
52 * | |
53 * @since 3.2 | |
54 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
55 public this() { |
129 | 56 } |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
57 |
129 | 58 /** |
59 * Creates a new URL hyperlink detector. | |
60 * | |
61 * @param textViewer the text viewer in which to detect the hyperlink | |
62 * @deprecated As of 3.2, replaced by {@link URLHyperlinkDetector} | |
63 */ | |
133
7d818bd32d63
Fix ctors to this with gvim regexp
Frank Benoit <benoit@tionex.de>
parents:
131
diff
changeset
|
64 public this(ITextViewer textViewer) { |
129 | 65 } |
66 | |
67 /* | |
68 * @see dwtx.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(dwtx.jface.text.ITextViewer, dwtx.jface.text.IRegion, bool) | |
69 */ | |
70 public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, bool canShowMultipleHyperlinks) { | |
71 if (region is null || textViewer is null) | |
72 return null; | |
73 | |
74 IDocument document= textViewer.getDocument(); | |
75 | |
76 int offset= region.getOffset(); | |
77 | |
78 String urlString= null; | |
79 if (document is null) | |
80 return null; | |
81 | |
82 IRegion lineInfo; | |
83 String line; | |
84 try { | |
85 lineInfo= document.getLineInformationOfOffset(offset); | |
86 line= document.get(lineInfo.getOffset(), lineInfo.getLength()); | |
87 } catch (BadLocationException ex) { | |
88 return null; | |
89 } | |
90 | |
91 int offsetInLine= offset - lineInfo.getOffset(); | |
92 | |
93 bool startDoubleQuote= false; | |
94 int urlOffsetInLine= 0; | |
95 int urlLength= 0; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
96 |
129 | 97 int urlSeparatorOffset= line.indexOf("://"); //$NON-NLS-1$ |
98 while (urlSeparatorOffset >= 0) { | |
99 | |
100 // URL protocol (left to "://") | |
101 urlOffsetInLine= urlSeparatorOffset; | |
102 char ch; | |
103 do { | |
104 urlOffsetInLine--; | |
105 ch= ' '; | |
106 if (urlOffsetInLine > -1) | |
107 ch= line.charAt(urlOffsetInLine); | |
108 startDoubleQuote= ch is '"'; | |
109 } while (Character.isUnicodeIdentifierStart(ch)); | |
110 urlOffsetInLine++; | |
111 | |
112 // Right to "://" | |
113 StringTokenizer tokenizer= new StringTokenizer(line.substring(urlSeparatorOffset + 3), " \t\n\r\f<>", false); //$NON-NLS-1$ | |
114 if (!tokenizer.hasMoreTokens()) | |
115 return null; | |
116 | |
117 urlLength= tokenizer.nextToken().length() + 3 + urlSeparatorOffset - urlOffsetInLine; | |
118 if (offsetInLine >= urlOffsetInLine && offsetInLine <= urlOffsetInLine + urlLength) | |
119 break; | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
120 |
129 | 121 urlSeparatorOffset= line.indexOf("://", urlSeparatorOffset + 1); //$NON-NLS-1$ |
122 } | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
123 |
129 | 124 if (urlSeparatorOffset < 0) |
125 return null; | |
126 | |
127 if (startDoubleQuote) { | |
128 int endOffset= -1; | |
129 int nextDoubleQuote= line.indexOf('"', urlOffsetInLine); | |
130 int nextWhitespace= line.indexOf(' ', urlOffsetInLine); | |
131 if (nextDoubleQuote !is -1 && nextWhitespace !is -1) | |
132 endOffset= Math.min(nextDoubleQuote, nextWhitespace); | |
133 else if (nextDoubleQuote !is -1) | |
134 endOffset= nextDoubleQuote; | |
135 else if (nextWhitespace !is -1) | |
136 endOffset= nextWhitespace; | |
137 if (endOffset !is -1) | |
138 urlLength= endOffset - urlOffsetInLine; | |
139 } | |
140 | |
141 // Set and validate URL string | |
142 try { | |
143 urlString= line.substring(urlOffsetInLine, urlOffsetInLine + urlLength); | |
144 new URL(urlString); | |
145 } catch (MalformedURLException ex) { | |
146 urlString= null; | |
147 return null; | |
148 } | |
149 | |
150 IRegion urlRegion= new Region(lineInfo.getOffset() + urlOffsetInLine, urlLength); | |
140
26688fec6d23
Following dsss compile errors
Frank Benoit <benoit@tionex.de>
parents:
133
diff
changeset
|
151 return [new URLHyperlink(urlRegion, urlString)]; |
129 | 152 } |
153 | |
154 } |