001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.text;
011:
012: import org.eclipse.text.edits.CopyTargetEdit;
013: import org.eclipse.text.edits.DeleteEdit;
014: import org.eclipse.text.edits.InsertEdit;
015: import org.eclipse.text.edits.MalformedTreeException;
016: import org.eclipse.text.edits.MoveTargetEdit;
017: import org.eclipse.text.edits.ReplaceEdit;
018: import org.eclipse.text.edits.TextEdit;
019: import org.eclipse.text.edits.TextEditProcessor;
020: import org.eclipse.text.edits.TextEditVisitor;
021: import org.eclipse.text.edits.UndoEdit;
022:
023: /**
024: * A text edit processor that brackets the application of edits into a document rewrite session.
025: *
026: * @since 3.3
027: */
028: public final class RewriteSessionEditProcessor extends
029: TextEditProcessor {
030: /** The threshold for <em>large</em> text edits. */
031: private static final int THRESHOLD = 1000;
032:
033: /**
034: * Text edit visitor that estimates the compound size of an edit tree in characters.
035: */
036: private static final class SizeVisitor extends TextEditVisitor {
037: int fSize = 0;
038:
039: public boolean visit(CopyTargetEdit edit) {
040: fSize += edit.getLength();
041: return super .visit(edit);
042: }
043:
044: public boolean visit(DeleteEdit edit) {
045: fSize += edit.getLength();
046: return super .visit(edit);
047: }
048:
049: public boolean visit(InsertEdit edit) {
050: fSize += edit.getText().length();
051: return super .visit(edit);
052: }
053:
054: public boolean visit(MoveTargetEdit edit) {
055: fSize += edit.getLength();
056: return super .visit(edit);
057: }
058:
059: public boolean visit(ReplaceEdit edit) {
060: fSize += Math
061: .max(edit.getLength(), edit.getText().length());
062: return super .visit(edit);
063: }
064: }
065:
066: /**
067: * Constructs a new edit processor for the given document.
068: *
069: * @param document the document to manipulate
070: * @param root the root of the text edit tree describing the modifications. By passing a text
071: * edit a a text edit processor the ownership of the edit is transfered to the text edit
072: * processors. Clients must not modify the edit (e.g adding new children) any longer.
073: * @param style {@link TextEdit#NONE}, {@link TextEdit#CREATE_UNDO} or
074: * {@link TextEdit#UPDATE_REGIONS})
075: */
076: public RewriteSessionEditProcessor(IDocument document,
077: TextEdit root, int style) {
078: super (document, root, style);
079: }
080:
081: /*
082: * @see org.eclipse.text.edits.TextEditProcessor#performEdits()
083: */
084: public UndoEdit performEdits() throws MalformedTreeException,
085: BadLocationException {
086: IDocument document = getDocument();
087: if (!(document instanceof IDocumentExtension4))
088: return super .performEdits();
089:
090: IDocumentExtension4 extension = (IDocumentExtension4) document;
091: boolean isLargeEdit = isLargeEdit(getRoot());
092: DocumentRewriteSessionType type = isLargeEdit ? DocumentRewriteSessionType.UNRESTRICTED
093: : DocumentRewriteSessionType.UNRESTRICTED_SMALL;
094:
095: DocumentRewriteSession session = extension
096: .startRewriteSession(type);
097: try {
098: return super .performEdits();
099: } finally {
100: extension.stopRewriteSession(session);
101: }
102: }
103:
104: /**
105: * Returns <code>true</code> if the passed edit is considered <em>large</em>,
106: * <code>false</code> otherwise.
107: *
108: * @param edit the edit to check
109: * @return <code>true</code> if <code>edit</code> is considered <em>large</em>,
110: * <code>false</code> otherwise
111: * @since 3.3
112: */
113: public static boolean isLargeEdit(TextEdit edit) {
114: SizeVisitor sizeVisitor = new SizeVisitor();
115: edit.accept(sizeVisitor);
116: return sizeVisitor.fSize > THRESHOLD;
117: }
118:
119: }
|