129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 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.RewriteSessionEditProcessor;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import dwtx.text.edits.CopyTargetEdit;
|
|
18 import dwtx.text.edits.DeleteEdit;
|
|
19 import dwtx.text.edits.InsertEdit;
|
|
20 import dwtx.text.edits.MalformedTreeException;
|
|
21 import dwtx.text.edits.MoveTargetEdit;
|
|
22 import dwtx.text.edits.ReplaceEdit;
|
|
23 import dwtx.text.edits.TextEdit;
|
|
24 import dwtx.text.edits.TextEditProcessor;
|
|
25 import dwtx.text.edits.TextEditVisitor;
|
|
26 import dwtx.text.edits.UndoEdit;
|
|
27
|
|
28 /**
|
|
29 * A text edit processor that brackets the application of edits into a document rewrite session.
|
|
30 *
|
|
31 * @since 3.3
|
|
32 */
|
|
33 public final class RewriteSessionEditProcessor : TextEditProcessor {
|
|
34 /** The threshold for <em>large</em> text edits. */
|
|
35 private static final int THRESHOLD= 1000;
|
|
36
|
|
37 /**
|
|
38 * Text edit visitor that estimates the compound size of an edit tree in characters.
|
|
39 */
|
|
40 private static final class SizeVisitor : TextEditVisitor {
|
|
41 int fSize= 0;
|
|
42
|
|
43 public bool visit(CopyTargetEdit edit) {
|
|
44 fSize += edit.getLength();
|
|
45 return super.visit(edit);
|
|
46 }
|
|
47
|
|
48 public bool visit(DeleteEdit edit) {
|
|
49 fSize += edit.getLength();
|
|
50 return super.visit(edit);
|
|
51 }
|
|
52
|
|
53 public bool visit(InsertEdit edit) {
|
|
54 fSize += edit.getText().length();
|
|
55 return super.visit(edit);
|
|
56 }
|
|
57
|
|
58 public bool visit(MoveTargetEdit edit) {
|
|
59 fSize += edit.getLength();
|
|
60 return super.visit(edit);
|
|
61 }
|
|
62
|
|
63 public bool visit(ReplaceEdit edit) {
|
|
64 fSize += Math.max(edit.getLength(), edit.getText().length());
|
|
65 return super.visit(edit);
|
|
66 }
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * Constructs a new edit processor for the given document.
|
|
71 *
|
|
72 * @param document the document to manipulate
|
|
73 * @param root the root of the text edit tree describing the modifications. By passing a text
|
|
74 * edit a a text edit processor the ownership of the edit is transfered to the text edit
|
|
75 * processors. Clients must not modify the edit (e.g adding new children) any longer.
|
|
76 * @param style {@link TextEdit#NONE}, {@link TextEdit#CREATE_UNDO} or
|
|
77 * {@link TextEdit#UPDATE_REGIONS})
|
|
78 */
|
|
79 public RewriteSessionEditProcessor(IDocument document, TextEdit root, int style) {
|
|
80 super(document, root, style);
|
|
81 }
|
|
82
|
|
83 /*
|
|
84 * @see dwtx.text.edits.TextEditProcessor#performEdits()
|
|
85 */
|
|
86 public UndoEdit performEdits() throws MalformedTreeException, BadLocationException {
|
|
87 IDocument document= getDocument();
|
|
88 if (!(document instanceof IDocumentExtension4))
|
|
89 return super.performEdits();
|
|
90
|
|
91 IDocumentExtension4 extension= (IDocumentExtension4) document;
|
|
92 bool isLargeEdit= isLargeEdit(getRoot());
|
|
93 DocumentRewriteSessionType type= isLargeEdit ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
|
|
94
|
|
95 DocumentRewriteSession session= extension.startRewriteSession(type);
|
|
96 try {
|
|
97 return super.performEdits();
|
|
98 } finally {
|
|
99 extension.stopRewriteSession(session);
|
|
100 }
|
|
101 }
|
|
102
|
|
103 /**
|
|
104 * Returns <code>true</code> if the passed edit is considered <em>large</em>,
|
|
105 * <code>false</code> otherwise.
|
|
106 *
|
|
107 * @param edit the edit to check
|
|
108 * @return <code>true</code> if <code>edit</code> is considered <em>large</em>,
|
|
109 * <code>false</code> otherwise
|
|
110 * @since 3.3
|
|
111 */
|
|
112 public static bool isLargeEdit(TextEdit edit) {
|
|
113 SizeVisitor sizeVisitor= new SizeVisitor();
|
|
114 edit.accept(sizeVisitor);
|
|
115 return sizeVisitor.fSize > THRESHOLD;
|
|
116 }
|
|
117
|
|
118 }
|