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