129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2006 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.AbstractLineTracker;
|
|
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.TextUtilities; // packageimport
|
|
19 import dwtx.jface.text.IInformationControlCreatorExtension; // packageimport
|
|
20 import dwtx.jface.text.AbstractInformationControlManager; // packageimport
|
|
21 import dwtx.jface.text.ITextViewerExtension2; // packageimport
|
|
22 import dwtx.jface.text.IDocumentPartitioner; // packageimport
|
|
23 import dwtx.jface.text.DefaultIndentLineAutoEditStrategy; // packageimport
|
|
24 import dwtx.jface.text.ITextSelection; // packageimport
|
|
25 import dwtx.jface.text.Document; // packageimport
|
|
26 import dwtx.jface.text.FindReplaceDocumentAdapterContentProposalProvider; // packageimport
|
|
27 import dwtx.jface.text.ITextListener; // packageimport
|
|
28 import dwtx.jface.text.BadPartitioningException; // packageimport
|
|
29 import dwtx.jface.text.ITextViewerExtension5; // packageimport
|
|
30 import dwtx.jface.text.IDocumentPartitionerExtension3; // packageimport
|
|
31 import dwtx.jface.text.IUndoManager; // packageimport
|
|
32 import dwtx.jface.text.ITextHoverExtension2; // packageimport
|
|
33 import dwtx.jface.text.IRepairableDocument; // packageimport
|
|
34 import dwtx.jface.text.IRewriteTarget; // packageimport
|
|
35 import dwtx.jface.text.DefaultPositionUpdater; // packageimport
|
|
36 import dwtx.jface.text.RewriteSessionEditProcessor; // packageimport
|
|
37 import dwtx.jface.text.TextViewerHoverManager; // packageimport
|
|
38 import dwtx.jface.text.DocumentRewriteSession; // packageimport
|
|
39 import dwtx.jface.text.TextViewer; // packageimport
|
|
40 import dwtx.jface.text.ITextViewerExtension8; // packageimport
|
|
41 import dwtx.jface.text.RegExMessages; // packageimport
|
|
42 import dwtx.jface.text.IDelayedInputChangeProvider; // packageimport
|
|
43 import dwtx.jface.text.ITextOperationTargetExtension; // packageimport
|
|
44 import dwtx.jface.text.IWidgetTokenOwner; // packageimport
|
|
45 import dwtx.jface.text.IViewportListener; // packageimport
|
|
46 import dwtx.jface.text.GapTextStore; // packageimport
|
|
47 import dwtx.jface.text.MarkSelection; // packageimport
|
|
48 import dwtx.jface.text.IDocumentPartitioningListenerExtension; // packageimport
|
|
49 import dwtx.jface.text.IDocumentAdapterExtension; // packageimport
|
|
50 import dwtx.jface.text.IInformationControlExtension; // packageimport
|
|
51 import dwtx.jface.text.IDocumentPartitioningListenerExtension2; // packageimport
|
|
52 import dwtx.jface.text.DefaultDocumentAdapter; // packageimport
|
|
53 import dwtx.jface.text.ITextViewerExtension3; // packageimport
|
|
54 import dwtx.jface.text.IInformationControlCreator; // packageimport
|
|
55 import dwtx.jface.text.TypedRegion; // packageimport
|
|
56 import dwtx.jface.text.ISynchronizable; // packageimport
|
|
57 import dwtx.jface.text.IMarkRegionTarget; // packageimport
|
|
58 import dwtx.jface.text.TextViewerUndoManager; // packageimport
|
|
59 import dwtx.jface.text.IRegion; // packageimport
|
|
60 import dwtx.jface.text.IInformationControlExtension2; // packageimport
|
|
61 import dwtx.jface.text.IDocumentExtension4; // packageimport
|
|
62 import dwtx.jface.text.IDocumentExtension2; // packageimport
|
|
63 import dwtx.jface.text.IDocumentPartitionerExtension2; // packageimport
|
|
64 import dwtx.jface.text.Assert; // packageimport
|
|
65 import dwtx.jface.text.DefaultInformationControl; // packageimport
|
|
66 import dwtx.jface.text.IWidgetTokenOwnerExtension; // packageimport
|
|
67 import dwtx.jface.text.DocumentClone; // packageimport
|
|
68 import dwtx.jface.text.DefaultUndoManager; // packageimport
|
|
69 import dwtx.jface.text.IFindReplaceTarget; // packageimport
|
|
70 import dwtx.jface.text.IAutoEditStrategy; // packageimport
|
|
71 import dwtx.jface.text.ILineTrackerExtension; // packageimport
|
|
72 import dwtx.jface.text.IUndoManagerExtension; // packageimport
|
|
73 import dwtx.jface.text.TextSelection; // packageimport
|
|
74 import dwtx.jface.text.DefaultAutoIndentStrategy; // packageimport
|
|
75 import dwtx.jface.text.IAutoIndentStrategy; // packageimport
|
|
76 import dwtx.jface.text.IPainter; // packageimport
|
|
77 import dwtx.jface.text.IInformationControl; // packageimport
|
|
78 import dwtx.jface.text.IInformationControlExtension3; // packageimport
|
|
79 import dwtx.jface.text.ITextViewerExtension6; // packageimport
|
|
80 import dwtx.jface.text.IInformationControlExtension4; // packageimport
|
|
81 import dwtx.jface.text.DefaultLineTracker; // packageimport
|
|
82 import dwtx.jface.text.IDocumentInformationMappingExtension; // packageimport
|
|
83 import dwtx.jface.text.IRepairableDocumentExtension; // packageimport
|
|
84 import dwtx.jface.text.ITextHover; // packageimport
|
|
85 import dwtx.jface.text.FindReplaceDocumentAdapter; // packageimport
|
|
86 import dwtx.jface.text.ILineTracker; // packageimport
|
|
87 import dwtx.jface.text.Line; // packageimport
|
|
88 import dwtx.jface.text.ITextViewerExtension; // packageimport
|
|
89 import dwtx.jface.text.IDocumentAdapter; // packageimport
|
|
90 import dwtx.jface.text.TextEvent; // packageimport
|
|
91 import dwtx.jface.text.BadLocationException; // packageimport
|
|
92 import dwtx.jface.text.AbstractDocument; // 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.ArrayList;
|
|
160 import java.util.Iterator;
|
|
161 import java.util.List;
|
|
162
|
|
163 /**
|
|
164 * Abstract implementation of <code>ILineTracker</code>. It lets the definition of line
|
|
165 * delimiters to subclasses. Assuming that '\n' is the only line delimiter, this abstract
|
|
166 * implementation defines the following line scheme:
|
|
167 * <ul>
|
|
168 * <li> "" -> [0,0]
|
|
169 * <li> "a" -> [0,1]
|
|
170 * <li> "\n" -> [0,1], [1,0]
|
|
171 * <li> "a\n" -> [0,2], [2,0]
|
|
172 * <li> "a\nb" -> [0,2], [2,1]
|
|
173 * <li> "a\nbc\n" -> [0,2], [2,3], [5,0]
|
|
174 * </ul>
|
|
175 * <p>
|
|
176 * This class must be subclassed.
|
|
177 * </p>
|
|
178 */
|
|
179 public abstract class AbstractLineTracker : ILineTracker, ILineTrackerExtension {
|
|
180
|
|
181 /**
|
|
182 * Tells whether this class is in debug mode.
|
|
183 *
|
|
184 * @since 3.1
|
|
185 */
|
|
186 private static final bool DEBUG= false;
|
|
187
|
|
188 /**
|
|
189 * Combines the information of the occurrence of a line delimiter. <code>delimiterIndex</code>
|
|
190 * is the index where a line delimiter starts, whereas <code>delimiterLength</code>,
|
|
191 * indicates the length of the delimiter.
|
|
192 */
|
|
193 protected static class DelimiterInfo {
|
|
194 public int delimiterIndex;
|
|
195 public int delimiterLength;
|
|
196 public String delimiter;
|
|
197 }
|
|
198
|
|
199 /**
|
|
200 * Representation of replace and set requests.
|
|
201 *
|
|
202 * @since 3.1
|
|
203 */
|
|
204 protected static class Request {
|
|
205 public final int offset;
|
|
206 public final int length;
|
|
207 public final String text;
|
|
208
|
|
209 public Request(int offset, int length, String text) {
|
|
210 this.offset= offset;
|
|
211 this.length= length;
|
|
212 this.text= text;
|
|
213 }
|
|
214
|
|
215 public Request(String text) {
|
|
216 this.offset= -1;
|
|
217 this.length= -1;
|
|
218 this.text= text;
|
|
219 }
|
|
220
|
|
221 public bool isReplaceRequest() {
|
|
222 return this.offset > -1 && this.length > -1;
|
|
223 }
|
|
224 }
|
|
225
|
|
226 /**
|
|
227 * The active rewrite session.
|
|
228 *
|
|
229 * @since 3.1
|
|
230 */
|
|
231 private DocumentRewriteSession fActiveRewriteSession;
|
|
232 /**
|
|
233 * The list of pending requests.
|
|
234 *
|
|
235 * @since 3.1
|
|
236 */
|
|
237 private List fPendingRequests;
|
|
238 /**
|
|
239 * The implementation that this tracker delegates to.
|
|
240 *
|
|
241 * @since 3.2
|
|
242 */
|
|
243 private ILineTracker fDelegate= new ListLineTracker() {
|
|
244 public String[] getLegalLineDelimiters() {
|
|
245 return AbstractLineTracker.this.getLegalLineDelimiters();
|
|
246 }
|
|
247
|
|
248 protected DelimiterInfo nextDelimiterInfo(String text, int offset) {
|
|
249 return AbstractLineTracker.this.nextDelimiterInfo(text, offset);
|
|
250 }
|
|
251 };
|
|
252 /**
|
|
253 * Whether the delegate needs conversion when the line structure is modified.
|
|
254 */
|
|
255 private bool fNeedsConversion= true;
|
|
256
|
|
257 /**
|
|
258 * Creates a new line tracker.
|
|
259 */
|
|
260 protected AbstractLineTracker() {
|
|
261 }
|
|
262
|
|
263 /*
|
|
264 * @see dwtx.jface.text.ILineTracker#computeNumberOfLines(java.lang.String)
|
|
265 */
|
|
266 public int computeNumberOfLines(String text) {
|
|
267 return fDelegate.computeNumberOfLines(text);
|
|
268 }
|
|
269
|
|
270 /*
|
|
271 * @see dwtx.jface.text.ILineTracker#getLineDelimiter(int)
|
|
272 */
|
|
273 public String getLineDelimiter(int line) throws BadLocationException {
|
|
274 checkRewriteSession();
|
|
275 return fDelegate.getLineDelimiter(line);
|
|
276 }
|
|
277
|
|
278 /*
|
|
279 * @see dwtx.jface.text.ILineTracker#getLineInformation(int)
|
|
280 */
|
|
281 public IRegion getLineInformation(int line) throws BadLocationException {
|
|
282 checkRewriteSession();
|
|
283 return fDelegate.getLineInformation(line);
|
|
284 }
|
|
285
|
|
286 /*
|
|
287 * @see dwtx.jface.text.ILineTracker#getLineInformationOfOffset(int)
|
|
288 */
|
|
289 public IRegion getLineInformationOfOffset(int offset) throws BadLocationException {
|
|
290 checkRewriteSession();
|
|
291 return fDelegate.getLineInformationOfOffset(offset);
|
|
292 }
|
|
293
|
|
294 /*
|
|
295 * @see dwtx.jface.text.ILineTracker#getLineLength(int)
|
|
296 */
|
|
297 public int getLineLength(int line) throws BadLocationException {
|
|
298 checkRewriteSession();
|
|
299 return fDelegate.getLineLength(line);
|
|
300 }
|
|
301
|
|
302 /*
|
|
303 * @see dwtx.jface.text.ILineTracker#getLineNumberOfOffset(int)
|
|
304 */
|
|
305 public int getLineNumberOfOffset(int offset) throws BadLocationException {
|
|
306 checkRewriteSession();
|
|
307 return fDelegate.getLineNumberOfOffset(offset);
|
|
308 }
|
|
309
|
|
310 /*
|
|
311 * @see dwtx.jface.text.ILineTracker#getLineOffset(int)
|
|
312 */
|
|
313 public int getLineOffset(int line) throws BadLocationException {
|
|
314 checkRewriteSession();
|
|
315 return fDelegate.getLineOffset(line);
|
|
316 }
|
|
317
|
|
318 /*
|
|
319 * @see dwtx.jface.text.ILineTracker#getNumberOfLines()
|
|
320 */
|
|
321 public int getNumberOfLines() {
|
|
322 try {
|
|
323 checkRewriteSession();
|
|
324 } catch (BadLocationException x) {
|
|
325 // TODO there is currently no way to communicate that exception back to the document
|
|
326 }
|
|
327 return fDelegate.getNumberOfLines();
|
|
328 }
|
|
329
|
|
330 /*
|
|
331 * @see dwtx.jface.text.ILineTracker#getNumberOfLines(int, int)
|
|
332 */
|
|
333 public int getNumberOfLines(int offset, int length) throws BadLocationException {
|
|
334 checkRewriteSession();
|
|
335 return fDelegate.getNumberOfLines(offset, length);
|
|
336 }
|
|
337
|
|
338 /*
|
|
339 * @see dwtx.jface.text.ILineTracker#set(java.lang.String)
|
|
340 */
|
|
341 public void set(String text) {
|
|
342 if (hasActiveRewriteSession()) {
|
|
343 fPendingRequests.clear();
|
|
344 fPendingRequests.add(new Request(text));
|
|
345 return;
|
|
346 }
|
|
347
|
|
348 fDelegate.set(text);
|
|
349 }
|
|
350
|
|
351 /*
|
|
352 * @see dwtx.jface.text.ILineTracker#replace(int, int, java.lang.String)
|
|
353 */
|
|
354 public void replace(int offset, int length, String text) throws BadLocationException {
|
|
355 if (hasActiveRewriteSession()) {
|
|
356 fPendingRequests.add(new Request(offset, length, text));
|
|
357 return;
|
|
358 }
|
|
359
|
|
360 checkImplementation();
|
|
361
|
|
362 fDelegate.replace(offset, length, text);
|
|
363 }
|
|
364
|
|
365 /**
|
|
366 * Converts the implementation to be a {@link TreeLineTracker} if it isn't yet.
|
|
367 *
|
|
368 * @since 3.2
|
|
369 */
|
|
370 private void checkImplementation() {
|
|
371 if (fNeedsConversion) {
|
|
372 fNeedsConversion= false;
|
|
373 fDelegate= new TreeLineTracker((ListLineTracker) fDelegate) {
|
|
374 protected DelimiterInfo nextDelimiterInfo(String text, int offset) {
|
|
375 return AbstractLineTracker.this.nextDelimiterInfo(text, offset);
|
|
376 }
|
|
377
|
|
378 public String[] getLegalLineDelimiters() {
|
|
379 return AbstractLineTracker.this.getLegalLineDelimiters();
|
|
380 }
|
|
381 };
|
|
382 }
|
|
383 }
|
|
384
|
|
385 /**
|
|
386 * Returns the information about the first delimiter found in the given text starting at the
|
|
387 * given offset.
|
|
388 *
|
|
389 * @param text the text to be searched
|
|
390 * @param offset the offset in the given text
|
|
391 * @return the information of the first found delimiter or <code>null</code>
|
|
392 */
|
|
393 protected abstract DelimiterInfo nextDelimiterInfo(String text, int offset);
|
|
394
|
|
395 /*
|
|
396 * @see dwtx.jface.text.ILineTrackerExtension#startRewriteSession(dwtx.jface.text.DocumentRewriteSession)
|
|
397 * @since 3.1
|
|
398 */
|
|
399 public final void startRewriteSession(DocumentRewriteSession session) {
|
|
400 if (fActiveRewriteSession !is null)
|
|
401 throw new IllegalStateException();
|
|
402 fActiveRewriteSession= session;
|
|
403 fPendingRequests= new ArrayList(20);
|
|
404 }
|
|
405
|
|
406 /*
|
|
407 * @see dwtx.jface.text.ILineTrackerExtension#stopRewriteSession(dwtx.jface.text.DocumentRewriteSession, java.lang.String)
|
|
408 * @since 3.1
|
|
409 */
|
|
410 public final void stopRewriteSession(DocumentRewriteSession session, String text) {
|
|
411 if (fActiveRewriteSession is session) {
|
|
412 fActiveRewriteSession= null;
|
|
413 fPendingRequests= null;
|
|
414 set(text);
|
|
415 }
|
|
416 }
|
|
417
|
|
418 /**
|
|
419 * Tells whether there's an active rewrite session.
|
|
420 *
|
|
421 * @return <code>true</code> if there is an active rewrite session, <code>false</code>
|
|
422 * otherwise
|
|
423 * @since 3.1
|
|
424 */
|
|
425 protected final bool hasActiveRewriteSession() {
|
|
426 return fActiveRewriteSession !is null;
|
|
427 }
|
|
428
|
|
429 /**
|
|
430 * Flushes the active rewrite session.
|
|
431 *
|
|
432 * @throws BadLocationException in case the recorded requests cannot be processed correctly
|
|
433 * @since 3.1
|
|
434 */
|
|
435 protected final void flushRewriteSession() throws BadLocationException {
|
|
436 if (DEBUG)
|
|
437 System.out.println("AbstractLineTracker: Flushing rewrite session: " + fActiveRewriteSession); //$NON-NLS-1$
|
|
438
|
|
439 Iterator e= fPendingRequests.iterator();
|
|
440
|
|
441 fPendingRequests= null;
|
|
442 fActiveRewriteSession= null;
|
|
443
|
|
444 while (e.hasNext()) {
|
|
445 Request request= (Request) e.next();
|
|
446 if (request.isReplaceRequest())
|
|
447 replace(request.offset, request.length, request.text);
|
|
448 else
|
|
449 set(request.text);
|
|
450 }
|
|
451 }
|
|
452
|
|
453 /**
|
|
454 * Checks the presence of a rewrite session and flushes it.
|
|
455 *
|
|
456 * @throws BadLocationException in case flushing does not succeed
|
|
457 * @since 3.1
|
|
458 */
|
|
459 protected final void checkRewriteSession() throws BadLocationException {
|
|
460 if (hasActiveRewriteSession())
|
|
461 flushRewriteSession();
|
|
462 }
|
|
463 }
|