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
|
|
63 import dwtx.jface.text.Assert; // packageimport
|
|
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
|
|
159 import java.util.HashMap;
|
|
160 import java.util.HashSet;
|
|
161 import java.util.Iterator;
|
|
162 import java.util.List;
|
|
163 import java.util.ListIterator;
|
|
164 import java.util.Map;
|
|
165 import java.util.Set;
|
|
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 */
|
|
183 public final static String[] DELIMITERS= new String[] { "\n", "\r", "\r\n" }; //$NON-NLS-3$ //$NON-NLS-2$ //$NON-NLS-1$
|
|
184
|
|
185 /**
|
|
186 * Default line delimiters used by these text functions.
|
|
187 *
|
|
188 * @deprecated use DELIMITERS instead
|
|
189 */
|
|
190 public final static String[] fgDelimiters= DELIMITERS;
|
|
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
|
|
224 int[] result= { -1, -1 };
|
|
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
|
|
236 int index= text.indexOf(searchStrings[i], offset);
|
|
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;
|
|
245 } else if (index is result[0] && length > searchStrings[result[1]].length()) {
|
|
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++) {
|
|
273 if (text.endsWith(searchStrings[i])) {
|
|
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++) {
|
|
295 if (text.startsWith(searchStrings[i])) {
|
|
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++) {
|
|
314 if (text.equals(compareStrings[i]))
|
|
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 */
|
|
330 public static DocumentEvent mergeUnprocessedDocumentEvents(IDocument unprocessedDocument, List documentEvents) throws BadLocationException {
|
|
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));
|
|
364 text.insert(0, string);
|
|
365 text.insert(0, eventText);
|
|
366
|
|
367 length= offset + length - eventOffset;
|
|
368 offset= eventOffset;
|
|
369
|
|
370 // events overlap each other
|
|
371 } else {
|
|
372 final int start= Math.max(0, eventOffset - offset);
|
|
373 final int end= Math.min(text.length(), eventLength + eventOffset - offset);
|
|
374 text.replace(start, end, eventText);
|
|
375
|
|
376 offset= Math.min(offset, eventOffset);
|
|
377 final int totalDelta= delta + eventText.length() - eventLength;
|
|
378 length= text.length() - totalDelta;
|
|
379 }
|
|
380 }
|
|
381
|
|
382 return new DocumentEvent(document, offset, length, text.toString());
|
|
383 }
|
|
384
|
|
385 /**
|
|
386 * Returns a document event which is an accumulation of a list of document events,
|
|
387 * <code>null</code> if the list of document events is empty.
|
|
388 * The document events being merged must all refer to the same document, to which
|
|
389 * the document changes have been already applied.
|
|
390 *
|
|
391 * @param documentEvents the list of document events to merge
|
|
392 * @return returns the merged document event
|
|
393 * @throws BadLocationException might be thrown if document is not in the correct state with respect to document events
|
|
394 */
|
|
395 public static DocumentEvent mergeProcessedDocumentEvents(List documentEvents) throws BadLocationException {
|
|
396
|
|
397 if (documentEvents.size() is 0)
|
|
398 return null;
|
|
399
|
|
400 final ListIterator iterator= documentEvents.listIterator(documentEvents.size());
|
134
|
401 final DocumentEvent firstEvent= cast(DocumentEvent) iterator.previous();
|
129
|
402
|
|
403 // current merged event
|
|
404 final IDocument document= firstEvent.getDocument();
|
|
405 int offset= firstEvent.getOffset();
|
|
406 int length= firstEvent.getLength();
|
|
407 int textLength= firstEvent.getText() is null ? 0 : firstEvent.getText().length();
|
|
408
|
|
409 while (iterator.hasPrevious()) {
|
|
410
|
|
411 final int delta= length - textLength;
|
|
412
|
134
|
413 final DocumentEvent event= cast(DocumentEvent) iterator.previous();
|
129
|
414 final int eventOffset= event.getOffset();
|
|
415 final int eventLength= event.getLength();
|
|
416 final int eventTextLength= event.getText() is null ? 0 : event.getText().length();
|
|
417
|
|
418 // event is right from merged event
|
|
419 if (eventOffset > offset + textLength + delta) {
|
|
420 length= (eventOffset - delta) - (offset + textLength) + length + eventLength;
|
|
421 textLength= (eventOffset - delta) + eventTextLength - offset;
|
|
422
|
|
423 // event is left from merged event
|
|
424 } else if (eventOffset + eventTextLength < offset) {
|
|
425 length= offset - (eventOffset + eventTextLength) + length + eventLength;
|
|
426 textLength= offset + textLength - eventOffset;
|
|
427 offset= eventOffset;
|
|
428
|
|
429 // events overlap each other
|
|
430 } else {
|
|
431 final int start= Math.max(0, eventOffset - offset);
|
|
432 final int end= Math.min(length, eventTextLength + eventOffset - offset);
|
|
433 length += eventLength - (end - start);
|
|
434
|
|
435 offset= Math.min(offset, eventOffset);
|
|
436 final int totalDelta= delta + eventLength - eventTextLength;
|
|
437 textLength= length - totalDelta;
|
|
438 }
|
|
439 }
|
|
440
|
|
441 final String text= document.get(offset, textLength);
|
|
442 return new DocumentEvent(document, offset, length, text);
|
|
443 }
|
|
444
|
|
445 /**
|
|
446 * Removes all connected document partitioners from the given document and stores them
|
|
447 * under their partitioning name in a map. This map is returned. After this method has been called
|
|
448 * the given document is no longer connected to any document partitioner.
|
|
449 *
|
|
450 * @param document the document
|
|
451 * @return the map containing the removed partitioners
|
|
452 */
|
|
453 public static Map removeDocumentPartitioners(IDocument document) {
|
|
454 Map partitioners= new HashMap();
|
|
455 if (document instanceof IDocumentExtension3) {
|
134
|
456 IDocumentExtension3 extension3= cast(IDocumentExtension3) document;
|
129
|
457 String[] partitionings= extension3.getPartitionings();
|
|
458 for (int i= 0; i < partitionings.length; i++) {
|
|
459 IDocumentPartitioner partitioner= extension3.getDocumentPartitioner(partitionings[i]);
|
|
460 if (partitioner !is null) {
|
|
461 extension3.setDocumentPartitioner(partitionings[i], null);
|
|
462 partitioner.disconnect();
|
|
463 partitioners.put(partitionings[i], partitioner);
|
|
464 }
|
|
465 }
|
|
466 } else {
|
|
467 IDocumentPartitioner partitioner= document.getDocumentPartitioner();
|
|
468 if (partitioner !is null) {
|
|
469 document.setDocumentPartitioner(null);
|
|
470 partitioner.disconnect();
|
|
471 partitioners.put(IDocumentExtension3.DEFAULT_PARTITIONING, partitioner);
|
|
472 }
|
|
473 }
|
|
474 return partitioners;
|
|
475 }
|
|
476
|
|
477 /**
|
|
478 * Connects the given document with all document partitioners stored in the given map under
|
|
479 * their partitioning name. This method cleans the given map.
|
|
480 *
|
|
481 * @param document the document
|
|
482 * @param partitioners the map containing the partitioners to be connected
|
|
483 * @since 3.0
|
|
484 */
|
|
485 public static void addDocumentPartitioners(IDocument document, Map partitioners) {
|
|
486 if (document instanceof IDocumentExtension3) {
|
134
|
487 IDocumentExtension3 extension3= cast(IDocumentExtension3) document;
|
129
|
488 Iterator e= partitioners.keySet().iterator();
|
|
489 while (e.hasNext()) {
|
134
|
490 String partitioning= cast(String) e.next();
|
|
491 IDocumentPartitioner partitioner= cast(IDocumentPartitioner) partitioners.get(partitioning);
|
129
|
492 partitioner.connect(document);
|
|
493 extension3.setDocumentPartitioner(partitioning, partitioner);
|
|
494 }
|
|
495 partitioners.clear();
|
|
496 } else {
|
134
|
497 IDocumentPartitioner partitioner= cast(IDocumentPartitioner) partitioners.get(IDocumentExtension3.DEFAULT_PARTITIONING);
|
129
|
498 partitioner.connect(document);
|
|
499 document.setDocumentPartitioner(partitioner);
|
|
500 }
|
|
501 }
|
|
502
|
|
503 /**
|
|
504 * Returns the content type at the given offset of the given document.
|
|
505 *
|
|
506 * @param document the document
|
|
507 * @param partitioning the partitioning to be used
|
|
508 * @param offset the offset
|
|
509 * @param preferOpenPartitions <code>true</code> if precedence should be
|
|
510 * given to a open partition ending at <code>offset</code> over a
|
|
511 * closed partition starting at <code>offset</code>
|
|
512 * @return the content type at the given offset of the document
|
|
513 * @throws BadLocationException if offset is invalid in the document
|
|
514 * @since 3.0
|
|
515 */
|
|
516 public static String getContentType(IDocument document, String partitioning, int offset, bool preferOpenPartitions) throws BadLocationException {
|
|
517 if (document instanceof IDocumentExtension3) {
|
134
|
518 IDocumentExtension3 extension3= cast(IDocumentExtension3) document;
|
129
|
519 try {
|
|
520 return extension3.getContentType(partitioning, offset, preferOpenPartitions);
|
|
521 } catch (BadPartitioningException x) {
|
|
522 return IDocument.DEFAULT_CONTENT_TYPE;
|
|
523 }
|
|
524 }
|
|
525
|
|
526 return document.getContentType(offset);
|
|
527 }
|
|
528
|
|
529 /**
|
|
530 * Returns the partition of the given offset of the given document.
|
|
531 *
|
|
532 * @param document the document
|
|
533 * @param partitioning the partitioning to be used
|
|
534 * @param offset the offset
|
|
535 * @param preferOpenPartitions <code>true</code> if precedence should be
|
|
536 * given to a open partition ending at <code>offset</code> over a
|
|
537 * closed partition starting at <code>offset</code>
|
|
538 * @return the content type at the given offset of this viewer's input
|
|
539 * document
|
|
540 * @throws BadLocationException if offset is invalid in the given document
|
|
541 * @since 3.0
|
|
542 */
|
|
543 public static ITypedRegion getPartition(IDocument document, String partitioning, int offset, bool preferOpenPartitions) throws BadLocationException {
|
|
544 if (document instanceof IDocumentExtension3) {
|
134
|
545 IDocumentExtension3 extension3= cast(IDocumentExtension3) document;
|
129
|
546 try {
|
|
547 return extension3.getPartition(partitioning, offset, preferOpenPartitions);
|
|
548 } catch (BadPartitioningException x) {
|
|
549 return new TypedRegion(0, document.getLength(), IDocument.DEFAULT_CONTENT_TYPE);
|
|
550 }
|
|
551 }
|
|
552
|
|
553 return document.getPartition(offset);
|
|
554 }
|
|
555
|
|
556 /**
|
|
557 * Computes and returns the partitioning for the given region of the given
|
|
558 * document for the given partitioning name.
|
|
559 *
|
|
560 * @param document the document
|
|
561 * @param partitioning the partitioning name
|
|
562 * @param offset the region offset
|
|
563 * @param length the region length
|
|
564 * @param includeZeroLengthPartitions whether to include zero-length partitions
|
|
565 * @return the partitioning for the given region of the given document for
|
|
566 * the given partitioning name
|
|
567 * @throws BadLocationException if the given region is invalid for the given
|
|
568 * document
|
|
569 * @since 3.0
|
|
570 */
|
|
571 public static ITypedRegion[] computePartitioning(IDocument document, String partitioning, int offset, int length, bool includeZeroLengthPartitions) throws BadLocationException {
|
|
572 if (document instanceof IDocumentExtension3) {
|
134
|
573 IDocumentExtension3 extension3= cast(IDocumentExtension3) document;
|
129
|
574 try {
|
|
575 return extension3.computePartitioning(partitioning, offset, length, includeZeroLengthPartitions);
|
|
576 } catch (BadPartitioningException x) {
|
|
577 return new ITypedRegion[0];
|
|
578 }
|
|
579 }
|
|
580
|
|
581 return document.computePartitioning(offset, length);
|
|
582 }
|
|
583
|
|
584 /**
|
|
585 * Computes and returns the partition managing position categories for the
|
|
586 * given document or <code>null</code> if this was impossible.
|
|
587 *
|
|
588 * @param document the document
|
|
589 * @return the partition managing position categories or <code>null</code>
|
|
590 * @since 3.0
|
|
591 */
|
|
592 public static String[] computePartitionManagingCategories(IDocument document) {
|
|
593 if (document instanceof IDocumentExtension3) {
|
134
|
594 IDocumentExtension3 extension3= cast(IDocumentExtension3) document;
|
129
|
595 String[] partitionings= extension3.getPartitionings();
|
|
596 if (partitionings !is null) {
|
|
597 Set categories= new HashSet();
|
|
598 for (int i= 0; i < partitionings.length; i++) {
|
|
599 IDocumentPartitioner p= extension3.getDocumentPartitioner(partitionings[i]);
|
|
600 if (p instanceof IDocumentPartitionerExtension2) {
|
134
|
601 IDocumentPartitionerExtension2 extension2= cast(IDocumentPartitionerExtension2) p;
|
129
|
602 String[] c= extension2.getManagingPositionCategories();
|
|
603 if (c !is null) {
|
|
604 for (int j= 0; j < c.length; j++)
|
|
605 categories.add(c[j]);
|
|
606 }
|
|
607 }
|
|
608 }
|
|
609 String[] result= new String[categories.size()];
|
|
610 categories.toArray(result);
|
|
611 return result;
|
|
612 }
|
|
613 }
|
|
614 return null;
|
|
615 }
|
|
616
|
|
617 /**
|
|
618 * 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
|
|
619 * a legal line delimiter or the first one of the legal line delimiters. The default line delimiter should be used when performing document
|
|
620 * manipulations that span multiple lines.
|
|
621 *
|
|
622 * @param document the document
|
|
623 * @return the document's default line delimiter
|
|
624 * @since 3.0
|
|
625 */
|
|
626 public static String getDefaultLineDelimiter(IDocument document) {
|
|
627
|
|
628 if (document instanceof IDocumentExtension4)
|
134
|
629 return (cast(IDocumentExtension4)document).getDefaultLineDelimiter();
|
129
|
630
|
|
631 String lineDelimiter= null;
|
|
632
|
|
633 try {
|
|
634 lineDelimiter= document.getLineDelimiter(0);
|
|
635 } catch (BadLocationException x) {
|
|
636 }
|
|
637
|
|
638 if (lineDelimiter !is null)
|
|
639 return lineDelimiter;
|
|
640
|
|
641 String sysLineDelimiter= System.getProperty("line.separator"); //$NON-NLS-1$
|
|
642 String[] delimiters= document.getLegalLineDelimiters();
|
|
643 Assert.isTrue(delimiters.length > 0);
|
|
644 for (int i= 0; i < delimiters.length; i++) {
|
|
645 if (delimiters[i].equals(sysLineDelimiter)) {
|
|
646 lineDelimiter= sysLineDelimiter;
|
|
647 break;
|
|
648 }
|
|
649 }
|
|
650
|
|
651 if (lineDelimiter is null)
|
|
652 lineDelimiter= delimiters[0];
|
|
653
|
|
654 return lineDelimiter;
|
|
655 }
|
|
656
|
|
657 /**
|
|
658 * Returns <code>true</code> if the two regions overlap. Returns <code>false</code> if one of the
|
|
659 * arguments is <code>null</code>.
|
|
660 *
|
|
661 * @param left the left region
|
|
662 * @param right the right region
|
|
663 * @return <code>true</code> if the two regions overlap, <code>false</code> otherwise
|
|
664 * @since 3.0
|
|
665 */
|
|
666 public static bool overlaps(IRegion left, IRegion right) {
|
|
667
|
|
668 if (left is null || right is null)
|
|
669 return false;
|
|
670
|
|
671 int rightEnd= right.getOffset() + right.getLength();
|
|
672 int leftEnd= left.getOffset()+ left.getLength();
|
|
673
|
|
674 if (right.getLength() > 0) {
|
|
675 if (left.getLength() > 0)
|
|
676 return left.getOffset() < rightEnd && right.getOffset() < leftEnd;
|
|
677 return right.getOffset() <= left.getOffset() && left.getOffset() < rightEnd;
|
|
678 }
|
|
679
|
|
680 if (left.getLength() > 0)
|
|
681 return left.getOffset() <= right.getOffset() && right.getOffset() < leftEnd;
|
|
682
|
|
683 return left.getOffset() is right.getOffset();
|
|
684 }
|
|
685
|
|
686 /**
|
|
687 * Returns a copy of the given string array.
|
|
688 *
|
|
689 * @param array the string array to be copied
|
|
690 * @return a copy of the given string array or <code>null</code> when <code>array</code> is <code>null</code>
|
|
691 * @since 3.1
|
|
692 */
|
|
693 public static String[] copy(String[] array) {
|
|
694 if (array !is null) {
|
|
695 String[] copy= new String[array.length];
|
|
696 System.arraycopy(array, 0, copy, 0, array.length);
|
|
697 return copy;
|
|
698 }
|
|
699 return null;
|
|
700 }
|
|
701
|
|
702 /**
|
|
703 * Returns a copy of the given integer array.
|
|
704 *
|
|
705 * @param array the integer array to be copied
|
|
706 * @return a copy of the given integer array or <code>null</code> when <code>array</code> is <code>null</code>
|
|
707 * @since 3.1
|
|
708 */
|
|
709 public static int[] copy(int[] array) {
|
|
710 if (array !is null) {
|
|
711 int[] copy= new int[array.length];
|
|
712 System.arraycopy(array, 0, copy, 0, array.length);
|
|
713 return copy;
|
|
714 }
|
|
715 return null;
|
|
716 }
|
|
717 }
|