Search in sources :

Example 96 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class NodeFormatter method formatIndentationBeforeNode.

protected void formatIndentationBeforeNode(IDOMNode node, IStructuredFormatContraints formatContraints) {
    // [111674] If inside xml:space="preserve" element, we bail
    if (formatContraints.getInPreserveSpaceElement())
        return;
    if (node != null) {
        IDOMNode previousSibling = (IDOMNode) node.getPreviousSibling();
        IStructuredDocument doc = node.getModel().getStructuredDocument();
        String lineDelimiter = getLineDelimiter(node, doc);
        String lineIndent = formatContraints.getCurrentIndent();
        if (node.getParentNode() != null) {
            if (node.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
                if (previousSibling != null)
                    if (previousSibling.getNodeType() == Node.TEXT_NODE)
                        getFormatter(previousSibling).format(previousSibling, formatContraints);
                    else {
                        insertBeforeNode(node, lineDelimiter + lineIndent);
                    }
            } else {
                if (previousSibling == null || previousSibling.getNodeType() != Node.TEXT_NODE) {
                    // 261968 - formatting tag without closing bracket:
                    // <t1><t1
                    // 265673 - Null ptr in formatIndentationBeforeNode
                    int prevEndNodeOffset = -1;
                    int prevEndRegionOffset = -1;
                    if (previousSibling != null) {
                        prevEndNodeOffset = previousSibling.getEndOffset();
                        IStructuredDocumentRegion endRegion = previousSibling.getEndStructuredDocumentRegion();
                        if (endRegion != null) {
                            prevEndRegionOffset = endRegion.getTextEndOffset();
                        }
                    }
                    if ((previousSibling == null) || (prevEndNodeOffset != -1 && prevEndNodeOffset == prevEndRegionOffset)) {
                        insertBeforeNode(node, lineDelimiter + lineIndent);
                    }
                } else {
                    if (previousSibling.getNodeValue().length() == 0) {
                        // replace
                        replaceNodeValue(previousSibling, lineDelimiter + lineIndent);
                    } else {
                        // append indentation
                        if (!previousSibling.getNodeValue().endsWith(lineDelimiter + lineIndent)) {
                            if (previousSibling.getNodeValue().endsWith(lineDelimiter)) {
                                insertAfterNode(previousSibling, lineIndent);
                            } else
                                getFormatter(previousSibling).format(previousSibling, formatContraints);
                        }
                    }
                }
            }
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)

Example 97 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class NodeFormatter method getParentIndentNode.

protected IDOMNode getParentIndentNode(IDOMNode node) {
    IDOMNode result = null;
    IDOMNode parentNode = (IDOMNode) node.getParentNode();
    if (parentNode.getNodeType() == Node.DOCUMENT_NODE)
        result = parentNode;
    else {
        ITextRegion region = parentNode.getLastStructuredDocumentRegion().getFirstRegion();
        if (region.getType() == DOMRegionContext.XML_END_TAG_OPEN)
            result = parentNode;
        else
            result = getParentIndentNode(parentNode);
    }
    return result;
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 98 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class TextNodeFormatter method formatNode.

protected void formatNode(IDOMNode node, IStructuredFormatContraints formatContraints) {
    // [111674] If inside xml:space="preserve" element, we bail
    if (formatContraints.getInPreserveSpaceElement())
        return;
    if (node != null) {
        IStructuredDocument doc = node.getStructuredDocument();
        int lineWidth = getFormatPreferences().getLineWidth();
        int currentAvailableLineWidth = computeAvailableLineWidth(doc, node.getStartOffset(), lineWidth);
        String nodeText = getNodeText(node);
        String compressedText = compressSpaces(nodeText, formatContraints);
        IDOMNode parentNode = (IDOMNode) node.getParentNode();
        if (((enoughSpace(parentNode, currentAvailableLineWidth, compressedText)) && (noSiblingsAndNoFollowingComment(node)) && !firstStructuredDocumentRegionContainsLineDelimiters(parentNode)) || node.getStartOffset() == 0) {
            handleNoReflow(node, doc, compressedText, parentNode);
        } else {
            // not enough space, need to reflow text
            String nodeIndentation = formatContraints.getCurrentIndent();
            currentAvailableLineWidth = lineWidth - getIndentationLength(nodeIndentation);
            List vector = reflowText(compressedText, currentAvailableLineWidth);
            int vectorSize = vector.size();
            StringBuffer reflowedTextBuffer = new StringBuffer();
            String lineDelimiter = getLineDelimiter(doc, node.getStartOffset());
            // handle first line specially to check for allowWhitespace
            if (vectorSize > 0) {
                // determines whether or not to allow whitespace if there
                // is an entity or cdata before it
                boolean allowWhitespace = true;
                // [206072] StringIndexOutOfBoundsException
                if (nodeText.length() == 0 || !Character.isWhitespace(nodeText.charAt(0))) {
                    Node previousSibling = node.getPreviousSibling();
                    if (previousSibling != null && (previousSibling.getNodeType() == Node.ENTITY_REFERENCE_NODE || previousSibling.getNodeType() == Node.CDATA_SECTION_NODE))
                        allowWhitespace = false;
                }
                String theString = (String) vector.get(0);
                if (allowWhitespace) {
                    reflowedTextBuffer.append(lineDelimiter);
                    if (theString.trim().length() > 0)
                        reflowedTextBuffer.append(nodeIndentation).append(theString);
                } else {
                    reflowedTextBuffer.append(theString);
                }
            }
            // do the rest of the lines
            for (int i = 1; i < vectorSize; i++) {
                String theString = (String) vector.get(i);
                if (theString.trim().length() > 0)
                    reflowedTextBuffer.append(lineDelimiter).append(nodeIndentation).append(theString);
                else
                    reflowedTextBuffer.append(lineDelimiter);
            }
            String reflowedText = reflowedTextBuffer.toString();
            if (node.getNextSibling() == null) {
                if (isEndTagMissing(parentNode)) {
                // don't add indentation to end if parent end tag is
                // missing
                } else {
                    // add parent's indentation to end
                    nodeIndentation = getNodeIndent(parentNode);
                    if (!reflowedText.endsWith(lineDelimiter + nodeIndentation)) {
                        reflowedText = StringUtils.appendIfNotEndWith(reflowedText, lineDelimiter);
                        reflowedText = StringUtils.appendIfNotEndWith(reflowedText, nodeIndentation);
                    }
                }
            } else {
                if (!reflowedText.endsWith(lineDelimiter + nodeIndentation)) {
                    // not already ended with the expected indentation
                    Node nextSibling = node.getNextSibling();
                    if (nextSibling.getNodeType() == Node.COMMENT_NODE) {
                        // add indentation to end if
                        // currentTextEndsWithLineDelimiter
                        // or followed by multiLineComment
                        int indexOfLastLineDelimiter = StringUtils.indexOfLastLineDelimiter(nodeText);
                        boolean currentTextEndsWithLineDelimiter = indexOfLastLineDelimiter != -1;
                        if (currentTextEndsWithLineDelimiter) {
                            // no more non blank character after the last
                            // line delimiter
                            currentTextEndsWithLineDelimiter = StringUtils.indexOfNonblank(nodeText, indexOfLastLineDelimiter) == -1;
                        }
                        String nodeValue = nextSibling.getNodeValue();
                        boolean multiLineComment = StringUtils.containsLineDelimiter(nodeValue);
                        if (currentTextEndsWithLineDelimiter || multiLineComment) {
                            reflowedText = StringUtils.appendIfNotEndWith(reflowedText, lineDelimiter);
                            reflowedText = StringUtils.appendIfNotEndWith(reflowedText, nodeIndentation);
                        }
                    } else if (nextSibling.getNodeType() == Node.ENTITY_REFERENCE_NODE || nextSibling.getNodeType() == Node.CDATA_SECTION_NODE) {
                        int textLength = nodeText.length();
                        if (textLength > 0 && Character.isWhitespace(nodeText.charAt(textLength - 1))) {
                            reflowedText = StringUtils.appendIfNotEndWith(reflowedText, lineDelimiter);
                            reflowedText = StringUtils.appendIfNotEndWith(reflowedText, nodeIndentation);
                        }
                    } else {
                        // not a comment, just add add indentation to end
                        reflowedText = StringUtils.appendIfNotEndWith(reflowedText, lineDelimiter);
                        reflowedText = StringUtils.appendIfNotEndWith(reflowedText, nodeIndentation);
                    }
                }
            }
            replaceNodeValue(node, reflowedText);
        }
    }
}
Also used : IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) List(java.util.List)

Example 99 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class ElementNodeFormatter method formatNode.

protected void formatNode(IDOMNode node, IStructuredFormatContraints formatContraints) {
    if (node != null) {
        // format indentation before node
        formatIndentationBeforeNode(node, formatContraints);
        // format start tag
        IDOMNode newNode = node;
        int startTagStartOffset = node.getStartOffset();
        IDOMModel structuredModel = node.getModel();
        boolean currentlyInXmlSpacePreserve = formatContraints.getInPreserveSpaceElement();
        formatStartTag(node, formatContraints);
        // save new node
        newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
        IStructuredDocumentRegion flatNode = newNode.getFirstStructuredDocumentRegion();
        if (flatNode != null) {
            ITextRegionList regions = flatNode.getRegions();
            ITextRegion lastRegion = regions.get(regions.size() - 1);
            // format children and end tag if not empty start tag
            if (lastRegion.getType() != DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
                // format children
                formatChildren(newNode, formatContraints);
                // save new node
                newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
                // format end tag
                formatEndTag(newNode, formatContraints);
            }
        }
        formatContraints.setInPreserveSpaceElement(currentlyInXmlSpacePreserve);
        // only indent if not at last node
        if (newNode != null && newNode.getNextSibling() != null)
            // format indentation after node
            formatIndentationAfterNode(newNode, formatContraints);
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 100 with IDOMNode

use of org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode in project webtools.sourceediting by eclipse.

the class ElementNodeFormatter method formatEndTag.

protected void formatEndTag(IDOMNode node, IStructuredFormatContraints formatContraints) {
    if (!isEndTagMissing(node)) {
        // end tag exists
        IStructuredDocument structuredDocument = node.getModel().getStructuredDocument();
        String lineDelimiter = structuredDocument.getLineDelimiter();
        String nodeIndentation = getNodeIndent(node);
        IDOMNode lastChild = (IDOMNode) node.getLastChild();
        if (lastChild != null && lastChild.getNodeType() != Node.TEXT_NODE) {
            if (isEndTagMissing(lastChild)) {
                // find deepest child
                IDOMNode deepestChild = (IDOMNode) lastChild.getLastChild();
                while (deepestChild != null && deepestChild.getLastChild() != null && isEndTagMissing(deepestChild)) {
                    lastChild = deepestChild;
                    deepestChild = (IDOMNode) deepestChild.getLastChild();
                }
                if (deepestChild != null) {
                    if (deepestChild.getNodeType() == Node.TEXT_NODE) {
                        // Special indentation handling if lastChild's end
                        // tag is missing and deepestChild is a text node.
                        String nodeText = deepestChild.getNodeValue();
                        if (!nodeText.endsWith(lineDelimiter + nodeIndentation)) {
                            nodeText = StringUtils.appendIfNotEndWith(nodeText, lineDelimiter);
                            nodeText = StringUtils.appendIfNotEndWith(nodeText, nodeIndentation);
                        }
                        replaceNodeValue(deepestChild, nodeText);
                    } else
                        insertAfterNode(lastChild, lineDelimiter + nodeIndentation);
                }
            } else
                // indent end tag
                insertAfterNode(lastChild, lineDelimiter + nodeIndentation);
        } else if (lastChild == null && firstStructuredDocumentRegionContainsLineDelimiters(node)) {
            // BUG174243 do not indent end tag if node has empty content
            // (otherwise new text node would be introduced)
            ModelQueryAdapter adapter = (ModelQueryAdapter) ((IDOMDocument) node.getOwnerDocument()).getAdapterFor(ModelQueryAdapter.class);
            CMElementDeclaration elementDeclaration = (CMElementDeclaration) adapter.getModelQuery().getCMNode(node);
            if ((elementDeclaration == null) || (elementDeclaration.getContentType() != CMElementDeclaration.EMPTY)) {
                // indent end tag
                replace(structuredDocument, node.getFirstStructuredDocumentRegion().getEndOffset(), 0, lineDelimiter + nodeIndentation);
            }
        }
        // format end tag name
        IStructuredDocumentRegion endTagStructuredDocumentRegion = node.getLastStructuredDocumentRegion();
        if (endTagStructuredDocumentRegion.getRegions().size() >= 3) {
            ITextRegion endTagNameRegion = endTagStructuredDocumentRegion.getRegions().get(1);
            removeRegionSpaces(node, endTagStructuredDocumentRegion, endTagNameRegion);
        }
    }
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)

Aggregations

IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)250 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)91 Node (org.w3c.dom.Node)63 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)57 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)44 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)43 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)38 List (java.util.List)35 ArrayList (java.util.ArrayList)34 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)30 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)30 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)27 Element (org.w3c.dom.Element)27 NodeList (org.w3c.dom.NodeList)23 BadLocationException (org.eclipse.jface.text.BadLocationException)22 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)22 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)20 IDOMElement (org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)19 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)18 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)18