Search in sources :

Example 1 with IStructuredFormatProcessor

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

the class FormatHandler method processorAvailable.

/* (non-Javadoc)
	 * @see org.eclipse.wst.sse.ui.internal.actions.ResourceActionDelegate#processorAvailable(org.eclipse.core.resources.IResource)
	 */
protected boolean processorAvailable(IResource resource) {
    boolean result = false;
    if (resource.isAccessible()) {
        try {
            if (resource instanceof IFile) {
                IFile file = (IFile) resource;
                IStructuredFormatProcessor formatProcessor = null;
                IContentDescription contentDescription = file.getContentDescription();
                if (contentDescription != null) {
                    IContentType contentType = contentDescription.getContentType();
                    formatProcessor = getFormatProcessor(contentType.getId());
                }
                if (formatProcessor != null)
                    result = true;
            } else if (resource instanceof IContainer) {
                IContainer container = (IContainer) resource;
                IResource[] members;
                members = container.members();
                for (int i = 0; i < members.length; i++) {
                    boolean available = processorAvailable(members[i]);
                    if (available) {
                        result = true;
                        break;
                    }
                }
            }
        } catch (CoreException e) {
            Logger.logException(e);
        }
    }
    return result;
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IContentType(org.eclipse.core.runtime.content.IContentType) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IContainer(org.eclipse.core.resources.IContainer)

Example 2 with IStructuredFormatProcessor

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

the class HTMLNodeActionManager method reformat.

public void reformat(Node newElement, boolean deep) {
    try {
        // tell the model that we are about to make a big model change
        fModel.aboutToChangeModel();
        // format selected node
        IStructuredFormatProcessor formatProcessor = new HTMLFormatProcessorImpl();
        formatProcessor.formatNode(newElement);
    } finally {
        // tell the model that we are done with the big model change
        fModel.changedModel();
    }
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) HTMLFormatProcessorImpl(org.eclipse.wst.html.core.internal.format.HTMLFormatProcessorImpl)

Example 3 with IStructuredFormatProcessor

use of org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor 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)

Example 4 with IStructuredFormatProcessor

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

the class FormatActionDelegate method processorAvailable.

/* (non-Javadoc)
	 * @see org.eclipse.wst.sse.ui.internal.actions.ResourceActionDelegate#processorAvailable(org.eclipse.core.resources.IResource)
	 */
protected boolean processorAvailable(IResource resource) {
    boolean result = false;
    if (resource.isAccessible()) {
        try {
            if (resource instanceof IFile) {
                IFile file = (IFile) resource;
                IStructuredFormatProcessor formatProcessor = null;
                IContentDescription contentDescription = file.getContentDescription();
                if (contentDescription != null) {
                    IContentType contentType = contentDescription.getContentType();
                    formatProcessor = getFormatProcessor(contentType.getId());
                }
                if (formatProcessor != null)
                    result = true;
            } else if (resource instanceof IContainer) {
                IContainer container = (IContainer) resource;
                IResource[] members;
                members = container.members();
                for (int i = 0; i < members.length; i++) {
                    boolean available = processorAvailable(members[i]);
                    if (available) {
                        result = true;
                        break;
                    }
                }
            }
        } catch (CoreException e) {
            Logger.logException(e);
        }
    }
    return result;
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) IContentType(org.eclipse.core.runtime.content.IContentType) IContentDescription(org.eclipse.core.runtime.content.IContentDescription) IContainer(org.eclipse.core.resources.IContainer)

Example 5 with IStructuredFormatProcessor

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

the class XSDCommonUIUtils method formatChild.

public static void formatChild(Node child) {
    if (child instanceof IDOMNode) {
        IDOMModel model = ((IDOMNode) child).getModel();
        try {
            // tell the model that we are about to make a big model change
            model.aboutToChangeModel();
            IStructuredFormatProcessor formatProcessor = new FormatProcessorXML();
            formatProcessor.formatNode(child);
        } finally {
            // tell the model that we are done with the big model change
            model.changedModel();
        }
    }
}
Also used : IStructuredFormatProcessor(org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) FormatProcessorXML(org.eclipse.wst.xml.core.internal.provisional.format.FormatProcessorXML)

Aggregations

IStructuredFormatProcessor (org.eclipse.wst.sse.core.internal.format.IStructuredFormatProcessor)16 FormatProcessorXML (org.eclipse.wst.xml.core.internal.provisional.format.FormatProcessorXML)8 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)6 IContentType (org.eclipse.core.runtime.content.IContentType)5 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)5 CoreException (org.eclipse.core.runtime.CoreException)4 IContentDescription (org.eclipse.core.runtime.content.IContentDescription)4 IOException (java.io.IOException)2 IContainer (org.eclipse.core.resources.IContainer)2 IFile (org.eclipse.core.resources.IFile)2 IStatus (org.eclipse.core.runtime.IStatus)2 MultiStatus (org.eclipse.core.runtime.MultiStatus)2 Status (org.eclipse.core.runtime.Status)2 MalformedInputExceptionWithDetail (org.eclipse.wst.sse.core.internal.exceptions.MalformedInputExceptionWithDetail)2 Iterator (java.util.Iterator)1 Vector (java.util.Vector)1 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)1 IContentTypeManager (org.eclipse.core.runtime.content.IContentTypeManager)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1