Search in sources :

Example 6 with IStructuredFormatter

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

the class HTMLFormatter method insertBreakBefore.

/**
 */
protected void insertBreakBefore(IDOMNode node, HTMLFormatContraints contraints) {
    if (node == null)
        return;
    if (node.getNodeType() == Node.TEXT_NODE)
        return;
    Node parent = node.getParentNode();
    if (parent == null)
        return;
    Node prev = node.getPreviousSibling();
    String spaces = null;
    if (prev != null && prev.getNodeType() == Node.TEXT_NODE) {
        if (contraints != null && contraints.getFormatWithSiblingIndent()) {
            IDOMNode text = (IDOMNode) prev;
            IStructuredFormatter formatter = HTMLFormatterFactory.getInstance().createFormatter(text, getFormatPreferences());
            if (formatter instanceof HTMLTextFormatter) {
                HTMLTextFormatter textFormatter = (HTMLTextFormatter) formatter;
                textFormatter.formatText(text, contraints, HTMLTextFormatter.FORMAT_TAIL);
            }
        }
        return;
    } else {
        spaces = getBreakSpaces(node);
    }
    if (spaces == null || spaces.length() == 0)
        return;
    replaceSource(node.getModel(), node.getStartOffset(), 0, spaces);
    setWidth(contraints, spaces);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IStructuredFormatter(org.eclipse.wst.sse.core.internal.format.IStructuredFormatter)

Example 7 with IStructuredFormatter

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

the class HTMLFormatter method formatChildNodes.

/**
 */
protected void formatChildNodes(IDOMNode node, HTMLFormatContraints contraints) {
    if (node == null)
        return;
    if (!node.hasChildNodes())
        return;
    // concat adjacent texts
    node.normalize();
    // disable sibling indent during formatting all the children
    boolean indent = false;
    if (contraints != null) {
        indent = contraints.getFormatWithSiblingIndent();
        contraints.setFormatWithSiblingIndent(false);
    }
    boolean insertBreak = true;
    IDOMNode child = (IDOMNode) node.getFirstChild();
    while (child != null) {
        if (child.getParentNode() != node)
            break;
        IDOMNode next = (IDOMNode) child.getNextSibling();
        if (insertBreak && canInsertBreakBefore(child)) {
            insertBreakBefore(child, contraints);
        }
        IStructuredFormatter formatter = HTMLFormatterFactory.getInstance().createFormatter(child, getFormatPreferences());
        if (formatter != null) {
            if (formatter instanceof HTMLFormatter) {
                HTMLFormatter htmlFormatter = (HTMLFormatter) formatter;
                htmlFormatter.formatNode(child, contraints);
            } else {
                formatter.format(child);
            }
        }
        if (canInsertBreakAfter(child)) {
            insertBreakAfter(child, contraints);
            // not to insert twice
            insertBreak = false;
        } else {
            insertBreak = true;
        }
        child = next;
    }
    if (contraints != null)
        contraints.setFormatWithSiblingIndent(indent);
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IStructuredFormatter(org.eclipse.wst.sse.core.internal.format.IStructuredFormatter)

Aggregations

IStructuredFormatter (org.eclipse.wst.sse.core.internal.format.IStructuredFormatter)7 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)5 Node (org.w3c.dom.Node)3 IStructuredFormatContraints (org.eclipse.wst.sse.core.internal.format.IStructuredFormatContraints)1