diff options
author | amywu | 2007-10-16 19:16:45 +0000 |
---|---|---|
committer | amywu | 2007-10-16 19:16:45 +0000 |
commit | eb0283b101afff9ac81829ee5bf43adfd66f5001 (patch) | |
tree | a265c9b85d0ffa7597c9502ff7b2577078d29f88 /bundles | |
parent | c3e4cedd06558318111ddac51f104561d2e8c587 (diff) | |
download | webtools.sourceediting-eb0283b101afff9ac81829ee5bf43adfd66f5001.tar.gz webtools.sourceediting-eb0283b101afff9ac81829ee5bf43adfd66f5001.tar.xz webtools.sourceediting-eb0283b101afff9ac81829ee5bf43adfd66f5001.zip |
[123621] Converting Line delimeters in cleanup document loses syntax highlighting
Diffstat (limited to 'bundles')
2 files changed, 109 insertions, 73 deletions
diff --git a/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/cleanup/AbstractStructuredCleanupProcessor.java b/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/cleanup/AbstractStructuredCleanupProcessor.java index 70af9bc3db..7efb85499f 100644 --- a/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/cleanup/AbstractStructuredCleanupProcessor.java +++ b/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/cleanup/AbstractStructuredCleanupProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2001, 2006 IBM Corporation and others. + * Copyright (c) 2001, 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -23,7 +23,10 @@ import java.util.Vector; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.DocumentRewriteSession; +import org.eclipse.jface.text.DocumentRewriteSessionType; import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IDocumentExtension4; import org.eclipse.jface.text.IRegion; import org.eclipse.jface.text.TextUtilities; import org.eclipse.wst.sse.core.StructuredModelManager; @@ -92,7 +95,7 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC return; IStructuredModel structuredModel = null; - //OutputStream outputStream = null; + // OutputStream outputStream = null; try { // setup structuredModel // Note: We are getting model for edit. Will save model if model @@ -106,7 +109,7 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC if (!structuredModel.isSharedForEdit() && structuredModel.isSaveNeeded()) structuredModel.save(); } finally { - //ensureClosed(outputStream, null); + // ensureClosed(outputStream, null); // release from model manager if (structuredModel != null) structuredModel.releaseFromEdit(); @@ -119,7 +122,7 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC if (start >= 0 && length >= 0 && start + length <= document.getLength()) { IStructuredModel structuredModel = null; - //OutputStream outputStream = null; + // OutputStream outputStream = null; try { // setup structuredModel // Note: We are getting model for edit. Will save model if @@ -133,7 +136,7 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC if (!structuredModel.isSharedForEdit() && structuredModel.isSaveNeeded()) structuredModel.save(); } finally { - //ensureClosed(outputStream, null); + // ensureClosed(outputStream, null); // release from model manager if (structuredModel != null) structuredModel.releaseFromEdit(); @@ -143,7 +146,7 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC public void cleanupFile(IFile file) throws IOException, CoreException { IStructuredModel structuredModel = null; - //OutputStream outputStream = null; + // OutputStream outputStream = null; try { // setup structuredModel structuredModel = StructuredModelManager.getModelManager().getModelForRead(file); @@ -152,11 +155,11 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC cleanupModel(structuredModel, 0, structuredModel.getStructuredDocument().getLength()); // save output to file - //outputStream = new + // outputStream = new // FileOutputStream(file.getLocation().toString()); structuredModel.save(file); } finally { - //ensureClosed(outputStream, null); + // ensureClosed(outputStream, null); // release from model manager if (structuredModel != null) structuredModel.releaseFromRead(); @@ -165,7 +168,7 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC public void cleanupFile(IFile file, int start, int length) throws IOException, CoreException { IStructuredModel structuredModel = null; - //OutputStream outputStream = null; + // OutputStream outputStream = null; try { // setup structuredModel structuredModel = StructuredModelManager.getModelManager().getModelForRead(file); @@ -174,11 +177,11 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC cleanupModel(structuredModel, start, length); // save output to file - //outputStream = new + // outputStream = new // FileOutputStream(file.getLocation().toString()); structuredModel.save(file); } finally { - //ensureClosed(outputStream, null); + // ensureClosed(outputStream, null); // release from model manager if (structuredModel != null) structuredModel.releaseFromRead(); @@ -188,7 +191,7 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC public void cleanupFileName(String fileName) throws IOException, CoreException { IStructuredModel structuredModel = null; InputStream inputStream = null; - //OutputStream outputStream = null; + // OutputStream outputStream = null; try { // setup structuredModel inputStream = new FileInputStream(fileName); @@ -198,10 +201,10 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC cleanupModel(structuredModel, 0, structuredModel.getStructuredDocument().getLength()); // save output to file - //outputStream = new FileOutputStream(fileName); + // outputStream = new FileOutputStream(fileName); structuredModel.save(); } finally { - //ensureClosed(outputStream, inputStream); + // ensureClosed(outputStream, inputStream); // release from model manager if (structuredModel != null) structuredModel.releaseFromRead(); @@ -211,7 +214,7 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC public void cleanupFileName(String fileName, int start, int length) throws IOException, CoreException { IStructuredModel structuredModel = null; InputStream inputStream = null; - //OutputStream outputStream = null; + // OutputStream outputStream = null; try { // setup structuredModel inputStream = new FileInputStream(fileName); @@ -221,10 +224,10 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC cleanupModel(structuredModel, start, length); // save output to file - //outputStream = new FileOutputStream(fileName); + // outputStream = new FileOutputStream(fileName); structuredModel.save(); } finally { - //ensureClosed(outputStream, inputStream); + // ensureClosed(outputStream, inputStream); // release from model manager if (structuredModel != null) structuredModel.releaseFromRead(); @@ -249,61 +252,88 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC boolean done = false; Node eachNode = firstNode; Node nextNode = null; - while (!done) { - // update "done" - done = (eachNode == lastNode); - // get next sibling before cleanup because eachNode - // may - // be deleted, - // for example when it's an empty text node - nextNode = eachNode.getNextSibling(); - - // cleanup selected node(s) - cleanupNode(eachNode); - - // update each node - if (nextNode != null && nextNode.getParentNode() == null) - // nextNode is deleted during cleanup - eachNode = eachNode.getNextSibling(); - else - eachNode = nextNode; - - // This should not be needed, but just in case - // something went wrong with with eachNode. - // We don't want an infinite loop here. - if (eachNode == null) - done = true; + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=123621 + // if doing any sort of cleanup, set up rewrite session/modelchanged + IDocumentExtension4 docExt4 = null; + if (structuredModel.getStructuredDocument() instanceof IDocumentExtension4) { + docExt4 = (IDocumentExtension4) structuredModel.getStructuredDocument(); } + DocumentRewriteSession rewriteSession = null; - // format source - if (getFormatSourcePreference(structuredModel)) { - // format the document - IStructuredFormatProcessor formatProcessor = getFormatProcessor(); - formatProcessor.formatModel(structuredModel); - } + try { + // whenever formatting model, fire + // abouttochange/modelchanged + structuredModel.aboutToChangeModel(); + rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED); + + while (!done) { + // update "done" + done = (eachNode == lastNode); + + // get next sibling before cleanup because eachNode + // may + // be deleted, + // for example when it's an empty text node + nextNode = eachNode.getNextSibling(); + + // cleanup selected node(s) + cleanupNode(eachNode); + + // update each node + if (nextNode != null && nextNode.getParentNode() == null) + // nextNode is deleted during cleanup + eachNode = eachNode.getNextSibling(); + else + eachNode = nextNode; + + // This should not be needed, but just in case + // something went wrong with with eachNode. + // We don't want an infinite loop here. + if (eachNode == null) + done = true; + } - // convert EOL codes - if (getConvertEOLCodesPreference(structuredModel)) { - IDocument document = structuredModel.getStructuredDocument(); - String endOfLineCode = getEOLCodePreference(structuredModel); - String endOfLineCodeString = null; - if (endOfLineCode.compareTo(CommonEncodingPreferenceNames.LF) == 0) - endOfLineCodeString = CommonEncodingPreferenceNames.STRING_LF; - else if (endOfLineCode.compareTo(CommonEncodingPreferenceNames.CR) == 0) - endOfLineCodeString = CommonEncodingPreferenceNames.STRING_CR; - else if (endOfLineCode.compareTo(CommonEncodingPreferenceNames.CRLF) == 0) - endOfLineCodeString = CommonEncodingPreferenceNames.STRING_CRLF; - if (endOfLineCodeString != null) { - convertLineDelimiters(document, endOfLineCodeString); - // DMW: 8/24/2002 setting line delimiter in - // document allows - // subsequent editing to insert the same line - // delimiter. - if (document instanceof IStructuredDocument) { - ((IStructuredDocument) document).setLineDelimiter(endOfLineCodeString); + // format source + if (getFormatSourcePreference(structuredModel)) { + // format the document + IStructuredFormatProcessor formatProcessor = getFormatProcessor(); + formatProcessor.formatModel(structuredModel); + } + + // convert EOL codes + if (getConvertEOLCodesPreference(structuredModel)) { + IDocument document = structuredModel.getStructuredDocument(); + String endOfLineCode = getEOLCodePreference(structuredModel); + String endOfLineCodeString = null; + if (endOfLineCode.compareTo(CommonEncodingPreferenceNames.LF) == 0) + endOfLineCodeString = CommonEncodingPreferenceNames.STRING_LF; + else if (endOfLineCode.compareTo(CommonEncodingPreferenceNames.CR) == 0) + endOfLineCodeString = CommonEncodingPreferenceNames.STRING_CR; + else if (endOfLineCode.compareTo(CommonEncodingPreferenceNames.CRLF) == 0) + endOfLineCodeString = CommonEncodingPreferenceNames.STRING_CRLF; + if (endOfLineCodeString != null) { + convertLineDelimiters(document, endOfLineCodeString); + // DMW: 8/24/2002 setting line delimiter in + // document allows + // subsequent editing to insert the same line + // delimiter. + if (document instanceof IStructuredDocument) { + ((IStructuredDocument) document).setLineDelimiter(endOfLineCodeString); + } + structuredModel.setDirtyState(true); } - structuredModel.setDirtyState(true); + } + } + finally { + // we need two finally's, just in case first fails + try { + if ((docExt4 != null) && (rewriteSession != null)) + docExt4.stopRewriteSession(rewriteSession); + } + finally { + // always make sure to fire changedmodel when done + structuredModel.changedModel(); } } } @@ -334,7 +364,7 @@ public abstract class AbstractStructuredCleanupProcessor implements IStructuredC protected void convertLineDelimiters(IDocument document, String newDelimiter) { final int lineCount = document.getNumberOfLines(); - Map partitioners = TextUtilities.removeDocumentPartitioners(document); + Map partitioners = TextUtilities.removeDocumentPartitioners(document); try { for (int i = 0; i < lineCount; i++) { final String delimiter = document.getLineDelimiter(i); diff --git a/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/format/AbstractStructuredFormatProcessor.java b/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/format/AbstractStructuredFormatProcessor.java index bd18d11222..4b554be07a 100644 --- a/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/format/AbstractStructuredFormatProcessor.java +++ b/bundles/org.eclipse.wst.sse.core/src/org/eclipse/wst/sse/core/internal/format/AbstractStructuredFormatProcessor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2001, 2006 IBM Corporation and others. + * Copyright (c) 2001, 2007 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -41,6 +41,11 @@ public abstract class AbstractStructuredFormatProcessor implements IStructuredFo protected IStructuredFormatContraints fFormatContraints = null; protected IProgressMonitor fProgressMonitor = null; public boolean refreshFormatPreferences = true; // special flag for JUnit + /* + * Max length of text to be formatted to be considered a "small change" + * Used for document rewrite session type. + */ + private final int MAX_SMALL_FORMAT_SIZE = 1000; protected void ensureClosed(OutputStream outputStream, InputStream inputStream) { @@ -161,11 +166,11 @@ public abstract class AbstractStructuredFormatProcessor implements IStructuredFo // Note: We are getting model for edit. Will save model if // model changed. structuredModel = StructuredModelManager.getModelManager().getExistingModelForEdit(document); - + if (structuredModel != null) { // format formatModel(structuredModel, start, length); - + // save model if needed if (!structuredModel.isSharedForEdit() && structuredModel.isSaveNeeded()) structuredModel.save(); @@ -315,7 +320,8 @@ public abstract class AbstractStructuredFormatProcessor implements IStructuredFo try { // whenever formatting model, fire abouttochange/modelchanged structuredModel.aboutToChangeModel(); - rewriteSession = (docExt4 == null) ? null : docExt4.startRewriteSession(DocumentRewriteSessionType.UNRESTRICTED); + DocumentRewriteSessionType rewriteType = (length > MAX_SMALL_FORMAT_SIZE) ? DocumentRewriteSessionType.UNRESTRICTED : DocumentRewriteSessionType.UNRESTRICTED_SMALL; + rewriteSession = (docExt4 == null || docExt4.getActiveRewriteSession() != null) ? null : docExt4.startRewriteSession(rewriteType); if ((start == 0) && (length == structuredModel.getStructuredDocument().getLength())) setFormatWithSiblingIndent(structuredModel, false); @@ -373,7 +379,7 @@ public abstract class AbstractStructuredFormatProcessor implements IStructuredFo structuredModel.changedModel(); } } - + if (Logger.DEBUG_FORMAT) { long endTime = System.currentTimeMillis(); System.out.println("formatModel time: " + (endTime - startTime)); //$NON-NLS-1$ |