summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpelder2007-07-24 16:18:07 (EDT)
committer pelder2007-07-24 16:18:07 (EDT)
commit2c92151598764cb6ed342ded53a81ee66277e2f3 (patch)
treed97075caa1b8d51a39101626c4a58dc46e611e66
parent07b0694a78bfb41f0f056b6bb12c418a61c90a07 (diff)
downloadorg.eclipse.jet-2c92151598764cb6ed342ded53a81ee66277e2f3.zip
org.eclipse.jet-2c92151598764cb6ed342ded53a81ee66277e2f3.tar.gz
org.eclipse.jet-2c92151598764cb6ed342ded53a81ee66277e2f3.tar.bz2
[197327] BodyContentWriter has hot spots in AbstractDocument code unrelated to JET function
-rw-r--r--plugins/org.eclipse.jet/src/org/eclipse/jet/BodyContentWriter.java245
1 files changed, 241 insertions, 4 deletions
diff --git a/plugins/org.eclipse.jet/src/org/eclipse/jet/BodyContentWriter.java b/plugins/org.eclipse.jet/src/org/eclipse/jet/BodyContentWriter.java
index 889593c..97999d0 100644
--- a/plugins/org.eclipse.jet/src/org/eclipse/jet/BodyContentWriter.java
+++ b/plugins/org.eclipse.jet/src/org/eclipse/jet/BodyContentWriter.java
@@ -27,7 +27,13 @@ import org.eclipse.jface.text.AbstractDocument;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.BadPositionCategoryException;
import org.eclipse.jface.text.DefaultLineTracker;
+import org.eclipse.jface.text.DefaultPositionUpdater;
+import org.eclipse.jface.text.DocumentRewriteSession;
import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IDocumentListener;
+import org.eclipse.jface.text.ILineTracker;
+import org.eclipse.jface.text.ILineTrackerExtension;
+import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextStore;
import org.eclipse.jface.text.Position;
@@ -41,16 +47,247 @@ public class BodyContentWriter implements BufferedJET2Writer
{
/**
- * IDocument implementationt that is optimized for appending.
+ * A line track that assumes that the document in being written too way more than
+ * line information is required. It constructs a line tracker only when line information
+ * is required. Once used, the line tracker is discarded, rather than maintaining it.
+ */
+ private static class LazyLineTracker implements ILineTracker, ILineTrackerExtension {
+
+
+ private final IDocument document;
+
+ public LazyLineTracker(IDocument document) {
+ this.document = document;
+ }
+
+ /**
+ * Return a delegate line tracker that does the actual work...
+ * @return
+ */
+ private ILineTracker getDelegate() {
+ final ILineTracker lineTracker = new DefaultLineTracker();
+ lineTracker.set(document.get());
+ return lineTracker;
+ }
+
+ public int computeNumberOfLines(String text)
+ {
+ return getDelegate().computeNumberOfLines(text);
+ }
+
+ public String[] getLegalLineDelimiters()
+ {
+ return getDelegate().getLegalLineDelimiters();
+ }
+
+ public String getLineDelimiter(int line) throws BadLocationException
+ {
+ return getDelegate().getLineDelimiter(line);
+ }
+
+ public IRegion getLineInformation(int line) throws BadLocationException
+ {
+ return getDelegate().getLineInformation(line);
+ }
+
+ public IRegion getLineInformationOfOffset(int offset) throws BadLocationException
+ {
+ return getDelegate().getLineInformationOfOffset(offset);
+ }
+
+ public int getLineLength(int line) throws BadLocationException
+ {
+ return getDelegate().getLineLength(line);
+ }
+
+ public int getLineNumberOfOffset(int offset) throws BadLocationException
+ {
+ return getLineNumberOfOffset(offset);
+ }
+
+ public int getLineOffset(int line) throws BadLocationException
+ {
+ return getLineOffset(line);
+ }
+
+ public int getNumberOfLines()
+ {
+ return getNumberOfLines();
+ }
+
+ public int getNumberOfLines(int offset, int length) throws BadLocationException
+ {
+ return getNumberOfLines(offset, length);
+ }
+
+ public void replace(int offset, int length, String text) throws BadLocationException
+ {
+ // do nothing...
+ }
+
+ public void set(String text)
+ {
+ // do nothing...
+ }
+
+ public void startRewriteSession(DocumentRewriteSession session) throws IllegalStateException
+ {
+ // do nothing...
+ }
+
+ public void stopRewriteSession(DocumentRewriteSession session, String text)
+ {
+ // do nothing...
+ }
+
+ }
+ /**
+ * IDocument implementationt that is optimized for appending, and that provides not
+ * document change notifications.
*/
private static class StringBufferDocument extends AbstractDocument
{
+
+ private boolean defaultPositionCategoryInitialized = false;
+ private static final Position[] EMPTY_POSITION_ARRAY = new Position[0];
+ private ILineTracker lazyLineTracker;
+ private final ITextStore textStore = new StringBufferTextStore();
+
public StringBufferDocument() {
super();
- setTextStore(new StringBufferTextStore());
- setLineTracker(new DefaultLineTracker());
+ // no need to setTextStore() - we override getStore()
+ // no need to setLineTracker() - we override getTracker()
completeInitialization();
}
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.AbstractDocument#getTracker()
+ */
+ protected ILineTracker getTracker()
+ {
+ if(lazyLineTracker == null) {
+ lazyLineTracker = new LazyLineTracker(this);
+ }
+ return lazyLineTracker;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.AbstractDocument#getStore()
+ */
+ protected ITextStore getStore()
+ {
+ // override to avoid overhead of base versions validity checking (Assert.isNotNull).
+ return textStore;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.AbstractDocument#completeInitialization()
+ */
+ protected void completeInitialization()
+ {
+ super.completeInitialization();
+ // uninstall the default position updater (which should be the only one
+ removePositionUpdater(getPositionUpdaters()[0]);
+ // and the default position category
+ try
+ {
+ // by-pass the overridden version to actually remove the category
+ super.removePositionCategory(DEFAULT_CATEGORY);
+ }
+ catch (BadPositionCategoryException e)
+ {
+ // won't happen
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.AbstractDocument#addPosition(java.lang.String, org.eclipse.jface.text.Position)
+ */
+ public void addPosition(String category, Position position) throws BadLocationException, BadPositionCategoryException
+ {
+ if(!defaultPositionCategoryInitialized && DEFAULT_CATEGORY.equals(category)) {
+ addPositionCategory(DEFAULT_CATEGORY);
+ addPositionUpdater(new DefaultPositionUpdater(DEFAULT_CATEGORY));
+ defaultPositionCategoryInitialized = true;
+ }
+ super.addPosition(category, position);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.AbstractDocument#computeIndexInCategory(java.lang.String, int)
+ */
+ public int computeIndexInCategory(String category, int offset) throws BadLocationException, BadPositionCategoryException
+ {
+ if(!defaultPositionCategoryInitialized && DEFAULT_CATEGORY.equals(category)) {
+ return 0;
+ }
+ return super.computeIndexInCategory(category, offset);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.AbstractDocument#getPositions(java.lang.String)
+ */
+ public Position[] getPositions(String category) throws BadPositionCategoryException
+ {
+ if(!defaultPositionCategoryInitialized && DEFAULT_CATEGORY.equals(category)) {
+ return EMPTY_POSITION_ARRAY;
+ }
+ return super.getPositions(category);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.AbstractDocument#removePosition(java.lang.String, org.eclipse.jface.text.Position)
+ */
+ public void removePosition(String category, Position position) throws BadPositionCategoryException
+ {
+ if(!defaultPositionCategoryInitialized && DEFAULT_CATEGORY.equals(category)) {
+ return;
+ }
+ super.removePosition(category, position);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.AbstractDocument#removePositionCategory(java.lang.String)
+ */
+ public void removePositionCategory(String category) throws BadPositionCategoryException
+ {
+ if(!defaultPositionCategoryInitialized && DEFAULT_CATEGORY.equals(category)) {
+ // the category has been removed, mark as initialized so we won't automatically
+ // add it again.
+ defaultPositionCategoryInitialized = true;
+ return;
+ }
+ super.removePositionCategory(category);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.AbstractDocument#addDocumentListener(org.eclipse.jface.text.IDocumentListener)
+ */
+ public void addDocumentListener(IDocumentListener listener)
+ {
+ // this document does not support change notifications.
+ throw new UnsupportedOperationException();
+ }
+
+ public void set(String text, long modificationStamp)
+ {
+ // implement set without any change notifications
+ getStore().set(text);
+ getTracker().set(text);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.text.AbstractDocument#replace(int, int, java.lang.String, long)
+ */
+ public void replace(int pos, int length, String text, long modificationStamp) throws BadLocationException
+ {
+ // implement replace without any change notifications
+ if ((0 > pos) || (0 > length) || (pos + length > getLength()))
+ throw new BadLocationException();
+
+ getStore().replace(pos, length, text);
+ getTracker().replace(pos, length, text);
+ }
}
/**
@@ -59,7 +296,7 @@ public class BodyContentWriter implements BufferedJET2Writer
private static class StringBufferTextStore implements ITextStore
{
- private final StringBuffer buffer = new StringBuffer();
+ private final StringBuffer buffer = new StringBuffer(1024);
/* (non-Javadoc)
* @see org.eclipse.jface.text.ITextStore#get(int)