1 11 package org.eclipse.jface.text; 12 13 import org.eclipse.text.edits.CopyTargetEdit; 14 import org.eclipse.text.edits.DeleteEdit; 15 import org.eclipse.text.edits.InsertEdit; 16 import org.eclipse.text.edits.MalformedTreeException; 17 import org.eclipse.text.edits.MoveTargetEdit; 18 import org.eclipse.text.edits.ReplaceEdit; 19 import org.eclipse.text.edits.TextEdit; 20 import org.eclipse.text.edits.TextEditProcessor; 21 import org.eclipse.text.edits.TextEditVisitor; 22 import org.eclipse.text.edits.UndoEdit; 23 24 29 public final class RewriteSessionEditProcessor extends TextEditProcessor { 30 31 private static final int THRESHOLD= 1000; 32 33 36 private static final class SizeVisitor extends TextEditVisitor { 37 int fSize= 0; 38 39 public boolean visit(CopyTargetEdit edit) { 40 fSize += edit.getLength(); 41 return super.visit(edit); 42 } 43 44 public boolean visit(DeleteEdit edit) { 45 fSize += edit.getLength(); 46 return super.visit(edit); 47 } 48 49 public boolean visit(InsertEdit edit) { 50 fSize += edit.getText().length(); 51 return super.visit(edit); 52 } 53 54 public boolean visit(MoveTargetEdit edit) { 55 fSize += edit.getLength(); 56 return super.visit(edit); 57 } 58 59 public boolean visit(ReplaceEdit edit) { 60 fSize += Math.max(edit.getLength(), edit.getText().length()); 61 return super.visit(edit); 62 } 63 } 64 65 75 public RewriteSessionEditProcessor(IDocument document, TextEdit root, int style) { 76 super(document, root, style); 77 } 78 79 82 public UndoEdit performEdits() throws MalformedTreeException, BadLocationException { 83 IDocument document= getDocument(); 84 if (!(document instanceof IDocumentExtension4)) 85 return super.performEdits(); 86 87 IDocumentExtension4 extension= (IDocumentExtension4) document; 88 boolean isLargeEdit= isLargeEdit(getRoot()); 89 DocumentRewriteSessionType type= isLargeEdit ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL; 90 91 DocumentRewriteSession session= extension.startRewriteSession(type); 92 try { 93 return super.performEdits(); 94 } finally { 95 extension.stopRewriteSession(session); 96 } 97 } 98 99 108 public static boolean isLargeEdit(TextEdit edit) { 109 SizeVisitor sizeVisitor= new SizeVisitor(); 110 edit.accept(sizeVisitor); 111 return sizeVisitor.fSize > THRESHOLD; 112 } 113 114 } 115 | Popular Tags |