Mercurial > projects > dwt-addons
annotate dwtx/jface/text/TextUtilities.d @ 161:f8d52b926852
...
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Wed, 27 Aug 2008 14:49:30 +0200 |
parents | f70d9508c95c |
children |
rev | line source |
---|---|
129 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2008 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.text.TextUtilities; | |
14 | |
131 | 15 import dwtx.jface.text.IDocumentPartitioningListener; // packageimport |
16 import dwtx.jface.text.DefaultTextHover; // packageimport | |
17 import dwtx.jface.text.AbstractInformationControl; // packageimport | |
18 import dwtx.jface.text.IInformationControlCreatorExtension; // packageimport | |
19 import dwtx.jface.text.AbstractInformationControlManager; // packageimport | |
20 import dwtx.jface.text.ITextViewerExtension2; // packageimport | |
21 import dwtx.jface.text.IDocumentPartitioner; // packageimport | |
22 import dwtx.jface.text.DefaultIndentLineAutoEditStrategy; // packageimport | |
23 import dwtx.jface.text.ITextSelection; // packageimport | |
24 import dwtx.jface.text.Document; // packageimport | |
25 import dwtx.jface.text.FindReplaceDocumentAdapterContentProposalProvider; // packageimport | |
26 import dwtx.jface.text.ITextListener; // packageimport | |
27 import dwtx.jface.text.BadPartitioningException; // packageimport | |
28 import dwtx.jface.text.ITextViewerExtension5; // packageimport | |
29 import dwtx.jface.text.IDocumentPartitionerExtension3; // packageimport | |
30 import dwtx.jface.text.IUndoManager; // packageimport | |
31 import dwtx.jface.text.ITextHoverExtension2; // packageimport | |
32 import dwtx.jface.text.IRepairableDocument; // packageimport | |
33 import dwtx.jface.text.IRewriteTarget; // packageimport | |
34 import dwtx.jface.text.DefaultPositionUpdater; // packageimport | |
35 import dwtx.jface.text.RewriteSessionEditProcessor; // packageimport | |
36 import dwtx.jface.text.TextViewerHoverManager; // packageimport | |
37 import dwtx.jface.text.DocumentRewriteSession; // packageimport | |
38 import dwtx.jface.text.TextViewer; // packageimport | |
39 import dwtx.jface.text.ITextViewerExtension8; // packageimport | |
40 import dwtx.jface.text.RegExMessages; // packageimport | |
41 import dwtx.jface.text.IDelayedInputChangeProvider; // packageimport | |
42 import dwtx.jface.text.ITextOperationTargetExtension; // packageimport | |
43 import dwtx.jface.text.IWidgetTokenOwner; // packageimport | |
44 import dwtx.jface.text.IViewportListener; // packageimport | |
45 import dwtx.jface.text.GapTextStore; // packageimport | |
46 import dwtx.jface.text.MarkSelection; // packageimport | |
47 import dwtx.jface.text.IDocumentPartitioningListenerExtension; // packageimport | |
48 import dwtx.jface.text.IDocumentAdapterExtension; // packageimport | |
49 import dwtx.jface.text.IInformationControlExtension; // packageimport | |
50 import dwtx.jface.text.IDocumentPartitioningListenerExtension2; // packageimport | |
51 import dwtx.jface.text.DefaultDocumentAdapter; // packageimport | |
52 import dwtx.jface.text.ITextViewerExtension3; // packageimport | |
53 import dwtx.jface.text.IInformationControlCreator; // packageimport | |
54 import dwtx.jface.text.TypedRegion; // packageimport | |
55 import dwtx.jface.text.ISynchronizable; // packageimport | |
56 import dwtx.jface.text.IMarkRegionTarget; // packageimport | |
57 import dwtx.jface.text.TextViewerUndoManager; // packageimport | |
58 import dwtx.jface.text.IRegion; // packageimport | |
59 import dwtx.jface.text.IInformationControlExtension2; // packageimport | |
60 import dwtx.jface.text.IDocumentExtension4; // packageimport | |
61 import dwtx.jface.text.IDocumentExtension2; // packageimport | |
62 import dwtx.jface.text.IDocumentPartitionerExtension2; // packageimport | |
161 | 63 // import dwtx.jface.text.Assert; // packageimport |
131 | 64 import dwtx.jface.text.DefaultInformationControl; // packageimport |
65 import dwtx.jface.text.IWidgetTokenOwnerExtension; // packageimport | |
66 import dwtx.jface.text.DocumentClone; // packageimport | |
67 import dwtx.jface.text.DefaultUndoManager; // packageimport | |
68 import dwtx.jface.text.IFindReplaceTarget; // packageimport | |
69 import dwtx.jface.text.IAutoEditStrategy; // packageimport | |
70 import dwtx.jface.text.ILineTrackerExtension; // packageimport | |
71 import dwtx.jface.text.IUndoManagerExtension; // packageimport | |
72 import dwtx.jface.text.TextSelection; // packageimport | |
73 import dwtx.jface.text.DefaultAutoIndentStrategy; // packageimport | |
74 import dwtx.jface.text.IAutoIndentStrategy; // packageimport | |
75 import dwtx.jface.text.IPainter; // packageimport | |
76 import dwtx.jface.text.IInformationControl; // packageimport | |
77 import dwtx.jface.text.IInformationControlExtension3; // packageimport | |
78 import dwtx.jface.text.ITextViewerExtension6; // packageimport | |
79 import dwtx.jface.text.IInformationControlExtension4; // packageimport | |
80 import dwtx.jface.text.DefaultLineTracker; // packageimport | |
81 import dwtx.jface.text.IDocumentInformationMappingExtension; // packageimport | |
82 import dwtx.jface.text.IRepairableDocumentExtension; // packageimport | |
83 import dwtx.jface.text.ITextHover; // packageimport | |
84 import dwtx.jface.text.FindReplaceDocumentAdapter; // packageimport | |
85 import dwtx.jface.text.ILineTracker; // packageimport | |
86 import dwtx.jface.text.Line; // packageimport | |
87 import dwtx.jface.text.ITextViewerExtension; // packageimport | |
88 import dwtx.jface.text.IDocumentAdapter; // packageimport | |
89 import dwtx.jface.text.TextEvent; // packageimport | |
90 import dwtx.jface.text.BadLocationException; // packageimport | |
91 import dwtx.jface.text.AbstractDocument; // packageimport | |
92 import dwtx.jface.text.AbstractLineTracker; // packageimport | |
93 import dwtx.jface.text.TreeLineTracker; // packageimport | |
94 import dwtx.jface.text.ITextPresentationListener; // packageimport | |
95 import dwtx.jface.text.Region; // packageimport | |
96 import dwtx.jface.text.ITextViewer; // packageimport | |
97 import dwtx.jface.text.IDocumentInformationMapping; // packageimport | |
98 import dwtx.jface.text.MarginPainter; // packageimport | |
99 import dwtx.jface.text.IPaintPositionManager; // packageimport | |
100 import dwtx.jface.text.TextPresentation; // packageimport | |
101 import dwtx.jface.text.IFindReplaceTargetExtension; // packageimport | |
102 import dwtx.jface.text.ISlaveDocumentManagerExtension; // packageimport | |
103 import dwtx.jface.text.ISelectionValidator; // packageimport | |
104 import dwtx.jface.text.IDocumentExtension; // packageimport | |
105 import dwtx.jface.text.PropagatingFontFieldEditor; // packageimport | |
106 import dwtx.jface.text.ConfigurableLineTracker; // packageimport | |
107 import dwtx.jface.text.SlaveDocumentEvent; // packageimport | |
108 import dwtx.jface.text.IDocumentListener; // packageimport | |
109 import dwtx.jface.text.PaintManager; // packageimport | |
110 import dwtx.jface.text.IFindReplaceTargetExtension3; // packageimport | |
111 import dwtx.jface.text.ITextDoubleClickStrategy; // packageimport | |
112 import dwtx.jface.text.IDocumentExtension3; // packageimport | |
113 import dwtx.jface.text.Position; // packageimport | |
114 import dwtx.jface.text.TextMessages; // packageimport | |
115 import dwtx.jface.text.CopyOnWriteTextStore; // packageimport | |
116 import dwtx.jface.text.WhitespaceCharacterPainter; // packageimport | |
117 import dwtx.jface.text.IPositionUpdater; // packageimport | |
118 import dwtx.jface.text.DefaultTextDoubleClickStrategy; // packageimport | |
119 import dwtx.jface.text.ListLineTracker; // packageimport | |
120 import dwtx.jface.text.ITextInputListener; // packageimport | |
121 import dwtx.jface.text.BadPositionCategoryException; // packageimport | |
122 import dwtx.jface.text.IWidgetTokenKeeperExtension; // packageimport | |
123 import dwtx.jface.text.IInputChangedListener; // packageimport | |
124 import dwtx.jface.text.ITextOperationTarget; // packageimport | |
125 import dwtx.jface.text.IDocumentInformationMappingExtension2; // packageimport | |
126 import dwtx.jface.text.ITextViewerExtension7; // packageimport | |
127 import dwtx.jface.text.IInformationControlExtension5; // packageimport | |
128 import dwtx.jface.text.IDocumentRewriteSessionListener; // packageimport | |
129 import dwtx.jface.text.JFaceTextUtil; // packageimport | |
130 import dwtx.jface.text.AbstractReusableInformationControlCreator; // packageimport | |
131 import dwtx.jface.text.TabsToSpacesConverter; // packageimport | |
132 import dwtx.jface.text.CursorLinePainter; // packageimport | |
133 import dwtx.jface.text.ITextHoverExtension; // packageimport | |
134 import dwtx.jface.text.IEventConsumer; // packageimport | |
135 import dwtx.jface.text.IDocument; // packageimport | |
136 import dwtx.jface.text.IWidgetTokenKeeper; // packageimport | |
137 import dwtx.jface.text.DocumentCommand; // packageimport | |
138 import dwtx.jface.text.TypedPosition; // packageimport | |
139 import dwtx.jface.text.IEditingSupportRegistry; // packageimport | |
140 import dwtx.jface.text.IDocumentPartitionerExtension; // packageimport | |
141 import dwtx.jface.text.AbstractHoverInformationControlManager; // packageimport | |
142 import dwtx.jface.text.IEditingSupport; // packageimport | |
143 import dwtx.jface.text.IMarkSelection; // packageimport | |
144 import dwtx.jface.text.ISlaveDocumentManager; // packageimport | |
145 import dwtx.jface.text.DocumentEvent; // packageimport | |
146 import dwtx.jface.text.DocumentPartitioningChangedEvent; // packageimport | |
147 import dwtx.jface.text.ITextStore; // packageimport | |
148 import dwtx.jface.text.JFaceTextMessages; // packageimport | |
149 import dwtx.jface.text.DocumentRewriteSessionEvent; // packageimport | |
150 import dwtx.jface.text.SequentialRewriteTextStore; // packageimport | |
151 import dwtx.jface.text.DocumentRewriteSessionType; // packageimport | |
152 import dwtx.jface.text.TextAttribute; // packageimport | |
153 import dwtx.jface.text.ITextViewerExtension4; // packageimport | |
154 import dwtx.jface.text.ITypedRegion; // packageimport | |
155 | |
156 | |
129 | 157 import dwt.dwthelper.utils; |
158 | |
153
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
146
diff
changeset
|
159 import dwtx.dwtxhelper.Collection; |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
146
diff
changeset
|
160 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
146
diff
changeset
|
161 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
146
diff
changeset
|
162 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
146
diff
changeset
|
163 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
146
diff
changeset
|
164 |
f70d9508c95c
Fix java Collection imports
Frank Benoit <benoit@tionex.de>
parents:
146
diff
changeset
|
165 |
129 | 166 |
167 import dwtx.core.runtime.Assert; | |
168 | |
169 | |
170 /** | |
171 * A collection of text functions. | |
172 * <p> | |
173 * This class is neither intended to be instantiated nor subclassed. | |
174 * </p> | |
175 * @noinstantiate This class is not intended to be instantiated by clients. | |
176 * @noextend This class is not intended to be subclassed by clients. | |
177 */ | |
178 public class TextUtilities { | |
179 | |
180 /** | |
181 * Default line delimiters used by the text functions of this class. | |
182 */ | |
146 | 183 public const static String[] DELIMITERS= [ "\n", "\r", "\r\n" ]; //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$ |
129 | 184 |
185 /** | |
186 * Default line delimiters used by these text functions. | |
187 * | |
188 * @deprecated use DELIMITERS instead | |
189 */ | |
146 | 190 public const static String[] fgDelimiters= DELIMITERS; |
129 | 191 |
192 | |
193 | |
194 /** | |
195 * Determines which one of default line delimiters appears first in the list. If none of them the | |
196 * hint is returned. | |
197 * | |
198 * @param text the text to be checked | |
199 * @param hint the line delimiter hint | |
200 * @return the line delimiter | |
201 */ | |
202 public static String determineLineDelimiter(String text, String hint) { | |
203 try { | |
204 int[] info= indexOf(DELIMITERS, text, 0); | |
205 return DELIMITERS[info[1]]; | |
206 } catch (ArrayIndexOutOfBoundsException x) { | |
207 } | |
208 return hint; | |
209 } | |
210 | |
211 /** | |
212 * Returns the starting position and the index of the first matching search string | |
213 * in the given text that is greater than the given offset. If more than one search | |
214 * string matches with the same starting position then the longest one is returned. | |
215 * | |
216 * @param searchStrings the strings to search for | |
217 * @param text the text to be searched | |
218 * @param offset the offset at which to start the search | |
219 * @return an <code>int[]</code> with two elements where the first is the starting offset, the second the index of the found | |
220 * search string in the given <code>searchStrings</code> array, returns <code>[-1, -1]</code> if no match exists | |
221 */ | |
222 public static int[] indexOf(String[] searchStrings, String text, int offset) { | |
223 | |
161 | 224 int[] result= [ -1, -1 ]; |
129 | 225 int zeroIndex= -1; |
226 | |
227 for (int i= 0; i < searchStrings.length; i++) { | |
228 | |
229 int length= searchStrings[i].length(); | |
230 | |
231 if (length is 0) { | |
232 zeroIndex= i; | |
233 continue; | |
234 } | |
235 | |
161 | 236 int index= .indexOf( text, searchStrings[i], offset); |
129 | 237 if (index >= 0) { |
238 | |
239 if (result[0] is -1) { | |
240 result[0]= index; | |
241 result[1]= i; | |
242 } else if (index < result[0]) { | |
243 result[0]= index; | |
244 result[1]= i; | |
161 | 245 } else if (index is result[0] && length > searchStrings[result[1]].length) { |
129 | 246 result[0]= index; |
247 result[1]= i; | |
248 } | |
249 } | |
250 } | |
251 | |
252 if (zeroIndex > -1 && result[0] is -1) { | |
253 result[0]= 0; | |
254 result[1]= zeroIndex; | |
255 } | |
256 | |
257 return result; | |
258 } | |
259 | |
260 /** | |
261 * Returns the index of the longest search string with which the given text ends or | |
262 * <code>-1</code> if none matches. | |
263 * | |
264 * @param searchStrings the strings to search for | |
265 * @param text the text to search | |
266 * @return the index in <code>searchStrings</code> of the longest string with which <code>text</code> ends or <code>-1</code> | |
267 */ | |
268 public static int endsWith(String[] searchStrings, String text) { | |
269 | |
270 int index= -1; | |
271 | |
272 for (int i= 0; i < searchStrings.length; i++) { | |
161 | 273 if (.endsWith(text, searchStrings[i])) { |
129 | 274 if (index is -1 || searchStrings[i].length() > searchStrings[index].length()) |
275 index= i; | |
276 } | |
277 } | |
278 | |
279 return index; | |
280 } | |
281 | |
282 /** | |
283 * Returns the index of the longest search string with which the given text starts or <code>-1</code> | |
284 * if none matches. | |
285 * | |
286 * @param searchStrings the strings to search for | |
287 * @param text the text to search | |
288 * @return the index in <code>searchStrings</code> of the longest string with which <code>text</code> starts or <code>-1</code> | |
289 */ | |
290 public static int startsWith(String[] searchStrings, String text) { | |
291 | |
292 int index= -1; | |
293 | |
294 for (int i= 0; i < searchStrings.length; i++) { | |
161 | 295 if (.startsWith(text, searchStrings[i])) { |
129 | 296 if (index is -1 || searchStrings[i].length() > searchStrings[index].length()) |
297 index= i; | |
298 } | |
299 } | |
300 | |
301 return index; | |
302 } | |
303 | |
304 /** | |
305 * Returns the index of the first compare string that equals the given text or <code>-1</code> | |
306 * if none is equal. | |
307 * | |
308 * @param compareStrings the strings to compare with | |
309 * @param text the text to check | |
310 * @return the index of the first equal compare string or <code>-1</code> | |
311 */ | |
312 public static int equals(String[] compareStrings, String text) { | |
313 for (int i= 0; i < compareStrings.length; i++) { | |
161 | 314 if (.equals(text,compareStrings[i])) |
129 | 315 return i; |
316 } | |
317 return -1; | |
318 } | |
319 | |
320 /** | |
321 * Returns a document event which is an accumulation of a list of document events, | |
322 * <code>null</code> if the list of documentEvents is empty. | |
323 * The document of the document events are ignored. | |
324 * | |
325 * @param unprocessedDocument the document to which the document events would be applied | |
326 * @param documentEvents the list of document events to merge | |
327 * @return returns the merged document event | |
328 * @throws BadLocationException might be thrown if document is not in the correct state with respect to document events | |
329 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
330 public static DocumentEvent mergeUnprocessedDocumentEvents(IDocument unprocessedDocument, List documentEvents) { |
129 | 331 |
332 if (documentEvents.size() is 0) | |
333 return null; | |
334 | |
335 final Iterator iterator= documentEvents.iterator(); | |
134 | 336 final DocumentEvent firstEvent= cast(DocumentEvent) iterator.next(); |
129 | 337 |
338 // current merged event | |
339 final IDocument document= unprocessedDocument; | |
340 int offset= firstEvent.getOffset(); | |
341 int length= firstEvent.getLength(); | |
342 final StringBuffer text= new StringBuffer(firstEvent.getText() is null ? "" : firstEvent.getText()); //$NON-NLS-1$ | |
343 | |
344 while (iterator.hasNext()) { | |
345 | |
346 final int delta= text.length() - length; | |
347 | |
134 | 348 final DocumentEvent event= cast(DocumentEvent) iterator.next(); |
129 | 349 final int eventOffset= event.getOffset(); |
350 final int eventLength= event.getLength(); | |
351 final String eventText= event.getText() is null ? "" : event.getText(); //$NON-NLS-1$ | |
352 | |
353 // event is right from merged event | |
354 if (eventOffset > offset + length + delta) { | |
355 final String string= document.get(offset + length, (eventOffset - delta) - (offset + length)); | |
356 text.append(string); | |
357 text.append(eventText); | |
358 | |
359 length= (eventOffset - delta) + eventLength - offset; | |
360 | |
361 // event is left from merged event | |
362 } else if (eventOffset + eventLength < offset) { | |
363 final String string= document.get(eventOffset + eventLength, offset - (eventOffset + eventLength)); | |
161 | 364 text.select(0,0); |
365 text.replace(string); | |
366 text.select(0,0); | |
367 text.replace(eventText); | |
129 | 368 |
369 length= offset + length - eventOffset; | |
370 offset= eventOffset; | |
371 | |
372 // events overlap each other | |
373 } else { | |
374 final int start= Math.max(0, eventOffset - offset); | |
375 final int end= Math.min(text.length(), eventLength + eventOffset - offset); | |
161 | 376 text.select(start, end); |
377 text.replace(eventText); | |
129 | 378 |
379 offset= Math.min(offset, eventOffset); | |
161 | 380 final int totalDelta= delta + eventText.length - eventLength; |
129 | 381 length= text.length() - totalDelta; |
382 } | |
383 } | |
384 | |
385 return new DocumentEvent(document, offset, length, text.toString()); | |
386 } | |
387 | |
388 /** | |
389 * Returns a document event which is an accumulation of a list of document events, | |
390 * <code>null</code> if the list of document events is empty. | |
391 * The document events being merged must all refer to the same document, to which | |
392 * the document changes have been already applied. | |
393 * | |
394 * @param documentEvents the list of document events to merge | |
395 * @return returns the merged document event | |
396 * @throws BadLocationException might be thrown if document is not in the correct state with respect to document events | |
397 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
398 public static DocumentEvent mergeProcessedDocumentEvents(List documentEvents) { |
129 | 399 |
400 if (documentEvents.size() is 0) | |
401 return null; | |
402 | |
403 final ListIterator iterator= documentEvents.listIterator(documentEvents.size()); | |
134 | 404 final DocumentEvent firstEvent= cast(DocumentEvent) iterator.previous(); |
129 | 405 |
406 // current merged event | |
407 final IDocument document= firstEvent.getDocument(); | |
408 int offset= firstEvent.getOffset(); | |
409 int length= firstEvent.getLength(); | |
161 | 410 int textLength= firstEvent.getText() is null ? 0 : firstEvent.getText().length; |
129 | 411 |
412 while (iterator.hasPrevious()) { | |
413 | |
414 final int delta= length - textLength; | |
415 | |
134 | 416 final DocumentEvent event= cast(DocumentEvent) iterator.previous(); |
129 | 417 final int eventOffset= event.getOffset(); |
418 final int eventLength= event.getLength(); | |
161 | 419 final int eventTextLength= event.getText() is null ? 0 : event.getText().length; |
129 | 420 |
421 // event is right from merged event | |
422 if (eventOffset > offset + textLength + delta) { | |
423 length= (eventOffset - delta) - (offset + textLength) + length + eventLength; | |
424 textLength= (eventOffset - delta) + eventTextLength - offset; | |
425 | |
426 // event is left from merged event | |
427 } else if (eventOffset + eventTextLength < offset) { | |
428 length= offset - (eventOffset + eventTextLength) + length + eventLength; | |
429 textLength= offset + textLength - eventOffset; | |
430 offset= eventOffset; | |
431 | |
432 // events overlap each other | |
433 } else { | |
434 final int start= Math.max(0, eventOffset - offset); | |
435 final int end= Math.min(length, eventTextLength + eventOffset - offset); | |
436 length += eventLength - (end - start); | |
437 | |
438 offset= Math.min(offset, eventOffset); | |
439 final int totalDelta= delta + eventLength - eventTextLength; | |
440 textLength= length - totalDelta; | |
441 } | |
442 } | |
443 | |
444 final String text= document.get(offset, textLength); | |
445 return new DocumentEvent(document, offset, length, text); | |
446 } | |
447 | |
448 /** | |
449 * Removes all connected document partitioners from the given document and stores them | |
450 * under their partitioning name in a map. This map is returned. After this method has been called | |
451 * the given document is no longer connected to any document partitioner. | |
452 * | |
453 * @param document the document | |
454 * @return the map containing the removed partitioners | |
455 */ | |
456 public static Map removeDocumentPartitioners(IDocument document) { | |
457 Map partitioners= new HashMap(); | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
458 if (cast(IDocumentExtension3)document ) { |
134 | 459 IDocumentExtension3 extension3= cast(IDocumentExtension3) document; |
129 | 460 String[] partitionings= extension3.getPartitionings(); |
461 for (int i= 0; i < partitionings.length; i++) { | |
462 IDocumentPartitioner partitioner= extension3.getDocumentPartitioner(partitionings[i]); | |
463 if (partitioner !is null) { | |
464 extension3.setDocumentPartitioner(partitionings[i], null); | |
465 partitioner.disconnect(); | |
161 | 466 partitioners.put(stringcast(partitionings[i]), cast(Object)partitioner); |
129 | 467 } |
468 } | |
469 } else { | |
470 IDocumentPartitioner partitioner= document.getDocumentPartitioner(); | |
471 if (partitioner !is null) { | |
472 document.setDocumentPartitioner(null); | |
473 partitioner.disconnect(); | |
161 | 474 partitioners.put(stringcast(IDocumentExtension3.DEFAULT_PARTITIONING), cast(Object)partitioner); |
129 | 475 } |
476 } | |
477 return partitioners; | |
478 } | |
479 | |
480 /** | |
481 * Connects the given document with all document partitioners stored in the given map under | |
482 * their partitioning name. This method cleans the given map. | |
483 * | |
484 * @param document the document | |
485 * @param partitioners the map containing the partitioners to be connected | |
486 * @since 3.0 | |
487 */ | |
488 public static void addDocumentPartitioners(IDocument document, Map partitioners) { | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
489 if (cast(IDocumentExtension3)document ) { |
134 | 490 IDocumentExtension3 extension3= cast(IDocumentExtension3) document; |
129 | 491 Iterator e= partitioners.keySet().iterator(); |
492 while (e.hasNext()) { | |
161 | 493 String partitioning= stringcast( e.next() ); |
134 | 494 IDocumentPartitioner partitioner= cast(IDocumentPartitioner) partitioners.get(partitioning); |
129 | 495 partitioner.connect(document); |
496 extension3.setDocumentPartitioner(partitioning, partitioner); | |
497 } | |
498 partitioners.clear(); | |
499 } else { | |
134 | 500 IDocumentPartitioner partitioner= cast(IDocumentPartitioner) partitioners.get(IDocumentExtension3.DEFAULT_PARTITIONING); |
129 | 501 partitioner.connect(document); |
502 document.setDocumentPartitioner(partitioner); | |
503 } | |
504 } | |
505 | |
506 /** | |
507 * Returns the content type at the given offset of the given document. | |
508 * | |
509 * @param document the document | |
510 * @param partitioning the partitioning to be used | |
511 * @param offset the offset | |
512 * @param preferOpenPartitions <code>true</code> if precedence should be | |
513 * given to a open partition ending at <code>offset</code> over a | |
514 * closed partition starting at <code>offset</code> | |
515 * @return the content type at the given offset of the document | |
516 * @throws BadLocationException if offset is invalid in the document | |
517 * @since 3.0 | |
518 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
519 public static String getContentType(IDocument document, String partitioning, int offset, bool preferOpenPartitions) { |
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
520 if (cast(IDocumentExtension3)document ) { |
134 | 521 IDocumentExtension3 extension3= cast(IDocumentExtension3) document; |
129 | 522 try { |
523 return extension3.getContentType(partitioning, offset, preferOpenPartitions); | |
524 } catch (BadPartitioningException x) { | |
525 return IDocument.DEFAULT_CONTENT_TYPE; | |
526 } | |
527 } | |
528 | |
529 return document.getContentType(offset); | |
530 } | |
531 | |
532 /** | |
533 * Returns the partition of the given offset of the given document. | |
534 * | |
535 * @param document the document | |
536 * @param partitioning the partitioning to be used | |
537 * @param offset the offset | |
538 * @param preferOpenPartitions <code>true</code> if precedence should be | |
539 * given to a open partition ending at <code>offset</code> over a | |
540 * closed partition starting at <code>offset</code> | |
541 * @return the content type at the given offset of this viewer's input | |
542 * document | |
543 * @throws BadLocationException if offset is invalid in the given document | |
544 * @since 3.0 | |
545 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
546 public static ITypedRegion getPartition(IDocument document, String partitioning, int offset, bool preferOpenPartitions) { |
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
547 if (cast(IDocumentExtension3)document ) { |
134 | 548 IDocumentExtension3 extension3= cast(IDocumentExtension3) document; |
129 | 549 try { |
550 return extension3.getPartition(partitioning, offset, preferOpenPartitions); | |
551 } catch (BadPartitioningException x) { | |
552 return new TypedRegion(0, document.getLength(), IDocument.DEFAULT_CONTENT_TYPE); | |
553 } | |
554 } | |
555 | |
556 return document.getPartition(offset); | |
557 } | |
558 | |
559 /** | |
560 * Computes and returns the partitioning for the given region of the given | |
561 * document for the given partitioning name. | |
562 * | |
563 * @param document the document | |
564 * @param partitioning the partitioning name | |
565 * @param offset the region offset | |
566 * @param length the region length | |
567 * @param includeZeroLengthPartitions whether to include zero-length partitions | |
568 * @return the partitioning for the given region of the given document for | |
569 * the given partitioning name | |
570 * @throws BadLocationException if the given region is invalid for the given | |
571 * document | |
572 * @since 3.0 | |
573 */ | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
574 public static ITypedRegion[] computePartitioning(IDocument document, String partitioning, int offset, int length, bool includeZeroLengthPartitions) { |
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
575 if (cast(IDocumentExtension3)document ) { |
134 | 576 IDocumentExtension3 extension3= cast(IDocumentExtension3) document; |
129 | 577 try { |
578 return extension3.computePartitioning(partitioning, offset, length, includeZeroLengthPartitions); | |
579 } catch (BadPartitioningException x) { | |
580 return new ITypedRegion[0]; | |
581 } | |
582 } | |
583 | |
584 return document.computePartitioning(offset, length); | |
585 } | |
586 | |
587 /** | |
588 * Computes and returns the partition managing position categories for the | |
589 * given document or <code>null</code> if this was impossible. | |
590 * | |
591 * @param document the document | |
592 * @return the partition managing position categories or <code>null</code> | |
593 * @since 3.0 | |
594 */ | |
595 public static String[] computePartitionManagingCategories(IDocument document) { | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
596 if (cast(IDocumentExtension3)document ) { |
134 | 597 IDocumentExtension3 extension3= cast(IDocumentExtension3) document; |
129 | 598 String[] partitionings= extension3.getPartitionings(); |
599 if (partitionings !is null) { | |
600 Set categories= new HashSet(); | |
601 for (int i= 0; i < partitionings.length; i++) { | |
602 IDocumentPartitioner p= extension3.getDocumentPartitioner(partitionings[i]); | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
603 if (cast(IDocumentPartitionerExtension2)p ) { |
134 | 604 IDocumentPartitionerExtension2 extension2= cast(IDocumentPartitionerExtension2) p; |
129 | 605 String[] c= extension2.getManagingPositionCategories(); |
606 if (c !is null) { | |
607 for (int j= 0; j < c.length; j++) | |
608 categories.add(c[j]); | |
609 } | |
610 } | |
611 } | |
161 | 612 return stringcast(categories.toArray()); |
129 | 613 } |
614 } | |
615 return null; | |
616 } | |
617 | |
618 /** | |
619 * Returns the default line delimiter for the given document. This is either the delimiter of the first line, or the platform line delimiter if it is | |
620 * a legal line delimiter or the first one of the legal line delimiters. The default line delimiter should be used when performing document | |
621 * manipulations that span multiple lines. | |
622 * | |
623 * @param document the document | |
624 * @return the document's default line delimiter | |
625 * @since 3.0 | |
626 */ | |
627 public static String getDefaultLineDelimiter(IDocument document) { | |
628 | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
629 if (cast(IDocumentExtension4)document ) |
134 | 630 return (cast(IDocumentExtension4)document).getDefaultLineDelimiter(); |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
631 |
129 | 632 String lineDelimiter= null; |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
633 |
129 | 634 try { |
635 lineDelimiter= document.getLineDelimiter(0); | |
636 } catch (BadLocationException x) { | |
637 } | |
638 | |
639 if (lineDelimiter !is null) | |
640 return lineDelimiter; | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
641 |
129 | 642 String sysLineDelimiter= System.getProperty("line.separator"); //$NON-NLS-1$ |
643 String[] delimiters= document.getLegalLineDelimiters(); | |
644 Assert.isTrue(delimiters.length > 0); | |
645 for (int i= 0; i < delimiters.length; i++) { | |
161 | 646 if (.equals(delimiters[i], sysLineDelimiter)) { |
129 | 647 lineDelimiter= sysLineDelimiter; |
648 break; | |
649 } | |
650 } | |
136
6dcb0baaa031
Regex removal of throws decls, some instanceof
Frank Benoit <benoit@tionex.de>
parents:
134
diff
changeset
|
651 |
129 | 652 if (lineDelimiter is null) |
653 lineDelimiter= delimiters[0]; | |
654 | |
655 return lineDelimiter; | |
656 } | |
657 | |
658 /** | |
659 * Returns <code>true</code> if the two regions overlap. Returns <code>false</code> if one of the | |
660 * arguments is <code>null</code>. | |
661 * | |
662 * @param left the left region | |
663 * @param right the right region | |
664 * @return <code>true</code> if the two regions overlap, <code>false</code> otherwise | |
665 * @since 3.0 | |
666 */ | |
667 public static bool overlaps(IRegion left, IRegion right) { | |
668 | |
669 if (left is null || right is null) | |
670 return false; | |
671 | |
672 int rightEnd= right.getOffset() + right.getLength(); | |
673 int leftEnd= left.getOffset()+ left.getLength(); | |
674 | |
675 if (right.getLength() > 0) { | |
676 if (left.getLength() > 0) | |
677 return left.getOffset() < rightEnd && right.getOffset() < leftEnd; | |
678 return right.getOffset() <= left.getOffset() && left.getOffset() < rightEnd; | |
679 } | |
680 | |
681 if (left.getLength() > 0) | |
682 return left.getOffset() <= right.getOffset() && right.getOffset() < leftEnd; | |
683 | |
684 return left.getOffset() is right.getOffset(); | |
685 } | |
686 | |
687 /** | |
688 * Returns a copy of the given string array. | |
689 * | |
690 * @param array the string array to be copied | |
691 * @return a copy of the given string array or <code>null</code> when <code>array</code> is <code>null</code> | |
692 * @since 3.1 | |
693 */ | |
694 public static String[] copy(String[] array) { | |
695 if (array !is null) { | |
696 String[] copy= new String[array.length]; | |
697 System.arraycopy(array, 0, copy, 0, array.length); | |
698 return copy; | |
699 } | |
700 return null; | |
701 } | |
702 | |
703 /** | |
704 * Returns a copy of the given integer array. | |
705 * | |
706 * @param array the integer array to be copied | |
707 * @return a copy of the given integer array or <code>null</code> when <code>array</code> is <code>null</code> | |
708 * @since 3.1 | |
709 */ | |
710 public static int[] copy(int[] array) { | |
711 if (array !is null) { | |
712 int[] copy= new int[array.length]; | |
713 System.arraycopy(array, 0, copy, 0, array.length); | |
714 return copy; | |
715 } | |
716 return null; | |
717 } | |
718 } |