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.text.edits.UndoCollector;
|
|
14
|
131
|
15 import dwtx.text.edits.MultiTextEdit; // packageimport
|
|
16 import dwtx.text.edits.CopySourceEdit; // packageimport
|
|
17 import dwtx.text.edits.MoveSourceEdit; // packageimport
|
|
18 import dwtx.text.edits.CopyingRangeMarker; // packageimport
|
|
19 import dwtx.text.edits.ReplaceEdit; // packageimport
|
|
20 import dwtx.text.edits.EditDocument; // packageimport
|
|
21 import dwtx.text.edits.DeleteEdit; // packageimport
|
|
22 import dwtx.text.edits.MoveTargetEdit; // packageimport
|
|
23 import dwtx.text.edits.CopyTargetEdit; // packageimport
|
|
24 import dwtx.text.edits.TextEditCopier; // packageimport
|
|
25 import dwtx.text.edits.ISourceModifier; // packageimport
|
|
26 import dwtx.text.edits.TextEditMessages; // packageimport
|
|
27 import dwtx.text.edits.TextEditProcessor; // packageimport
|
|
28 import dwtx.text.edits.MalformedTreeException; // packageimport
|
|
29 import dwtx.text.edits.TreeIterationInfo; // packageimport
|
|
30 import dwtx.text.edits.TextEditVisitor; // packageimport
|
|
31 import dwtx.text.edits.TextEditGroup; // packageimport
|
|
32 import dwtx.text.edits.TextEdit; // packageimport
|
|
33 import dwtx.text.edits.RangeMarker; // packageimport
|
|
34 import dwtx.text.edits.UndoEdit; // packageimport
|
|
35 import dwtx.text.edits.InsertEdit; // packageimport
|
|
36
|
|
37
|
129
|
38 import dwt.dwthelper.utils;
|
|
39
|
|
40
|
|
41 import dwtx.core.runtime.Assert;
|
|
42 import dwtx.jface.text.BadLocationException;
|
|
43 import dwtx.jface.text.DocumentEvent;
|
|
44 import dwtx.jface.text.IDocument;
|
|
45 import dwtx.jface.text.IDocumentListener;
|
|
46
|
|
47
|
|
48 class UndoCollector : IDocumentListener {
|
|
49
|
|
50 protected UndoEdit undo;
|
159
|
51
|
|
52 package UndoEdit undo_package(){
|
|
53 return undo;
|
|
54 }
|
|
55
|
129
|
56 private int fOffset;
|
|
57 private int fLength;
|
130
|
58
|
129
|
59 /**
|
|
60 * @since 3.1
|
|
61 */
|
|
62 private String fLastCurrentText;
|
|
63
|
130
|
64 public this(TextEdit root) {
|
129
|
65 fOffset= root.getOffset();
|
|
66 fLength= root.getLength();
|
|
67 }
|
|
68
|
|
69 public void connect(IDocument document) {
|
|
70 document.addDocumentListener(this);
|
|
71 undo= new UndoEdit();
|
|
72 }
|
|
73
|
|
74 public void disconnect(IDocument document) {
|
|
75 if (undo !is null) {
|
|
76 document.removeDocumentListener(this);
|
|
77 undo.defineRegion(fOffset, fLength);
|
|
78 }
|
|
79 }
|
|
80
|
|
81 public void documentChanged(DocumentEvent event) {
|
|
82 fLength+= getDelta(event);
|
|
83 }
|
|
84
|
|
85 private static int getDelta(DocumentEvent event) {
|
|
86 String text= event.getText();
|
|
87 return text is null ? -event.getLength() : (text.length() - event.getLength());
|
|
88 }
|
|
89
|
|
90 public void documentAboutToBeChanged(DocumentEvent event) {
|
|
91 int offset= event.getOffset();
|
|
92 int currentLength= event.getLength();
|
|
93 String currentText= null;
|
|
94 try {
|
|
95 currentText= event.getDocument().get(offset, currentLength);
|
|
96 } catch (BadLocationException cannotHappen) {
|
|
97 Assert.isTrue(false, "Can't happen"); //$NON-NLS-1$
|
|
98 }
|
130
|
99
|
129
|
100 /*
|
|
101 * see https://bugs.eclipse.org/bugs/show_bug.cgi?id=93634
|
|
102 * If the same string is replaced on many documents (e.g. rename
|
|
103 * package), the size of the undo can be reduced by using the same
|
|
104 * String instance in all edits, instead of using the unique String
|
|
105 * returned from IDocument.get(int, int).
|
|
106 */
|
|
107 if (fLastCurrentText !is null && fLastCurrentText.equals(currentText))
|
|
108 currentText= fLastCurrentText;
|
|
109 else
|
|
110 fLastCurrentText= currentText;
|
|
111
|
|
112 String newText= event.getText();
|
|
113 undo.add(new ReplaceEdit(offset, newText !is null ? newText.length() : 0, currentText));
|
|
114 }
|
|
115 }
|