Search in sources :

Example 1 with IFormattingDelegate

use of org.eclipse.wst.sse.core.internal.format.IFormattingDelegate in project webtools.sourceediting by eclipse.

the class AbstractStructuredCleanupProcessor method cleanupModel.

public void cleanupModel(IStructuredModel structuredModel, int start, int length, Object context) {
    if (structuredModel != null) {
        if ((start >= 0) && (length <= structuredModel.getStructuredDocument().getLength())) {
            Vector activeNodes = getActiveNodes(structuredModel, start, length);
            if (activeNodes.size() > 0) {
                Node firstNode = (Node) activeNodes.firstElement();
                Node lastNode = (Node) activeNodes.lastElement();
                boolean done = false;
                Node eachNode = firstNode;
                Node nextNode = null;
                // 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;
                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;
                        // We don't want an infinite loop here.
                        if (eachNode == null)
                            done = true;
                    }
                    // format source
                    if (getFormatSourcePreference(structuredModel)) {
                        // format the document
                        IFormattingDelegate delegate = getFormattingDelegate();
                        if (context != null && delegate != null) {
                            delegate.format(context);
                        } else {
                            IStructuredFormatProcessor formatProcessor = getFormatProcessor();
                            formatProcessor.formatModel(structuredModel);
                        }
                    }
                } 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();
                    }
                }
            }
        }
    }
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) DocumentRewriteSession(org.eclipse.jface.text.DocumentRewriteSession) IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) Node(org.w3c.dom.Node) Vector(java.util.Vector) IFormattingDelegate(org.eclipse.wst.sse.core.internal.format.IFormattingDelegate)

Aggregations

Vector (java.util.Vector)1 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)1 IFormattingDelegate (org.eclipse.wst.sse.core.internal.format.IFormattingDelegate)1 IStructuredFormatProcessor (org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor)1 Node (org.w3c.dom.Node)1