Search in sources :

Example 96 with IStructuredDocumentRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.

the class CSSModelUtil method diagnoseNode.

static boolean diagnoseNode(ICSSNode parent, IStructuredDocument structuredDocument) {
    // check this
    Vector errors = new Vector();
    if (parent instanceof CSSStructuredDocumentRegionContainer) {
        CSSStructuredDocumentRegionContainer node = (CSSStructuredDocumentRegionContainer) parent;
        // $NON-NLS-1$
        String nodeText = CSSUtil.getClassString(node) + ": ";
        IStructuredDocumentRegion flatNode = node.getFirstStructuredDocumentRegion();
        if (flatNode == null && (!(node instanceof CSSStyleDeclarationImpl || node instanceof CSSStyleSheetImpl) || node.getFirstChild() != null)) {
            // $NON-NLS-1$
            errors.add(nodeText + "first flat node is null.");
        } else if (flatNode != null) {
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(flatNode.getStart());
            if (flatNode != modelNode) {
                // $NON-NLS-1$
                errors.add(nodeText + "first flat node is not in model.");
            }
        }
        flatNode = node.getLastStructuredDocumentRegion();
        if (flatNode == null && (!(node instanceof CSSStyleDeclarationImpl || node instanceof CSSStyleSheetImpl) || node.getFirstChild() != null)) {
            // $NON-NLS-1$
            errors.add(nodeText + "last flat node is null.");
        } else if (flatNode != null) {
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(flatNode.getStart());
            if (flatNode != modelNode) {
                // $NON-NLS-1$
                errors.add(nodeText + "last flat node is not in model.");
            }
        }
    } else if (parent instanceof CSSRegionContainer) {
        CSSRegionContainer node = (CSSRegionContainer) parent;
        // $NON-NLS-1$
        String nodeText = CSSUtil.getClassString(node) + ": ";
        ITextRegion region = node.getFirstRegion();
        IStructuredDocumentRegion parentRegion = node.getDocumentRegion();
        if (region == null && (!(node instanceof MediaListImpl) || node.getFirstChild() != null)) {
            // $NON-NLS-1$
            errors.add(nodeText + "first region is null.");
        } else if (region != null) {
            int offset = parentRegion.getStartOffset(region);
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
            ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
            if (region != modelRegion) {
                // $NON-NLS-1$
                errors.add(nodeText + "first region is not in model.");
            }
        }
        region = node.getLastRegion();
        if (region == null && (!(node instanceof MediaListImpl) || node.getFirstChild() != null)) {
            // $NON-NLS-1$
            errors.add(nodeText + "last region is null.");
        } else if (region != null) {
            int offset = parentRegion.getStartOffset(region);
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
            ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
            if (region != modelRegion) {
                // $NON-NLS-1$
                errors.add(nodeText + "last region is not in model.");
            }
        }
    }
    ICSSNodeList attrs = parent.getAttributes();
    int nAttrs = attrs.getLength();
    for (int i = 0; i < nAttrs; i++) {
        ICSSAttr attr = (ICSSAttr) attrs.item(i);
        CSSRegionContainer node = (CSSRegionContainer) attr;
        // $NON-NLS-2$//$NON-NLS-1$
        String nodeText = CSSUtil.getClassString(node) + "(" + attr.getName() + "): ";
        ITextRegion region = node.getFirstRegion();
        IStructuredDocumentRegion parentRegion = node.getDocumentRegion();
        if (region == null && 0 < attr.getValue().length()) {
            // $NON-NLS-1$
            errors.add(nodeText + "first region is null.");
        } else if (region != null) {
            int offset = parentRegion.getStartOffset(region);
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
            ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
            if (region != modelRegion) {
                // $NON-NLS-1$
                errors.add(nodeText + "first region is not in model.");
            }
        }
        region = node.getLastRegion();
        if (region == null && 0 < attr.getValue().length()) {
            // $NON-NLS-1$
            errors.add(nodeText + "last region is null.");
        } else if (region != null) {
            int offset = parentRegion.getStartOffset(region);
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
            ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
            if (region != modelRegion) {
                // $NON-NLS-1$
                errors.add(nodeText + "last region is not in model.");
            }
        }
    }
    if (!errors.isEmpty()) {
        Iterator i = errors.iterator();
        while (i.hasNext()) {
            CSSUtil.debugOut((String) i.next());
        }
        return false;
    } else {
        return true;
    }
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ICSSNodeList(org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) Vector(java.util.Vector)

Example 97 with IStructuredDocumentRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.

the class CSSModelUtil method isBraceClosed.

/**
 */
static boolean isBraceClosed(ICSSNode node) {
    boolean bClosed = true;
    if (!(node instanceof CSSStructuredDocumentRegionContainer)) {
        return bClosed;
    }
    IStructuredDocumentRegion first = ((CSSStructuredDocumentRegionContainer) node).getFirstStructuredDocumentRegion();
    IStructuredDocumentRegion last = ((CSSStructuredDocumentRegionContainer) node).getLastStructuredDocumentRegion();
    if (first == null || last == null) {
        return bClosed;
    }
    if (last.getStart() < first.getStart()) {
        return bClosed;
    }
    IStructuredDocumentRegion flatNode = first;
    int nOpen = 0;
    int nClose = 0;
    do {
        String type = CSSUtil.getStructuredDocumentRegionType(flatNode);
        if (type == CSSRegionContexts.CSS_LBRACE) {
            nOpen++;
        } else if (type == CSSRegionContexts.CSS_RBRACE) {
            nClose++;
        }
        flatNode = flatNode.getNext();
    } while (flatNode != null && flatNode != last);
    if ((nOpen == 0 && nClose == 0) || nClose < nOpen) {
        bClosed = false;
    }
    return bClosed;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)

Example 98 with IStructuredDocumentRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.

the class CSSModelUtil method isInterruption.

/**
 * only for insertion..
 */
static boolean isInterruption(CSSStructuredDocumentRegionContainer target, IStructuredDocumentRegion flatNode) {
    if (target == null || flatNode == null) {
        return false;
    }
    int start = flatNode.getStart();
    IStructuredDocumentRegion firstNode = target.getFirstStructuredDocumentRegion();
    IStructuredDocumentRegion lastNode = target.getLastStructuredDocumentRegion();
    if (firstNode != null && firstNode.getStart() < start && lastNode != null && start < lastNode.getStart()) {
        return true;
    }
    return false;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)

Example 99 with IStructuredDocumentRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.

the class ElementNodeCleanupHandler method insertRequiredAttrs.

private IDOMNode insertRequiredAttrs(IDOMNode node) {
    boolean insertRequiredAttrs = getCleanupPreferences().getInsertRequiredAttrs();
    IDOMNode newNode = node;
    if (insertRequiredAttrs) {
        List requiredAttrs = getRequiredAttrs(newNode);
        if (requiredAttrs.size() > 0) {
            NamedNodeMap currentAttrs = node.getAttributes();
            List insertAttrs = new ArrayList();
            if (currentAttrs.getLength() == 0)
                insertAttrs.addAll(requiredAttrs);
            else {
                for (int i = 0; i < requiredAttrs.size(); i++) {
                    String requiredAttrName = ((CMAttributeDeclaration) requiredAttrs.get(i)).getAttrName();
                    boolean found = false;
                    for (int j = 0; j < currentAttrs.getLength(); j++) {
                        String currentAttrName = currentAttrs.item(j).getNodeName();
                        if (requiredAttrName.compareToIgnoreCase(currentAttrName) == 0) {
                            found = true;
                            break;
                        }
                    }
                    if (!found)
                        insertAttrs.add(requiredAttrs.get(i));
                }
            }
            if (insertAttrs.size() > 0) {
                IStructuredDocumentRegion startStructuredDocumentRegion = newNode.getStartStructuredDocumentRegion();
                int index = startStructuredDocumentRegion.getEndOffset();
                ITextRegion lastRegion = startStructuredDocumentRegion.getLastRegion();
                if (lastRegion.getType() == DOMRegionContext.XML_TAG_CLOSE) {
                    index--;
                    lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
                } else if (lastRegion.getType() == DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
                    index = index - 2;
                    lastRegion = startStructuredDocumentRegion.getRegionAtCharacterOffset(index - 1);
                }
                MultiTextEdit multiTextEdit = new MultiTextEdit();
                try {
                    for (int i = insertAttrs.size() - 1; i >= 0; i--) {
                        CMAttributeDeclaration attrDecl = (CMAttributeDeclaration) insertAttrs.get(i);
                        String requiredAttributeName = attrDecl.getAttrName();
                        String defaultValue = attrDecl.getDefaultValue();
                        if (defaultValue == null)
                            // $NON-NLS-1$
                            defaultValue = "";
                        // $NON-NLS-1$
                        String nameAndDefaultValue = " ";
                        if (i == 0 && lastRegion.getLength() > lastRegion.getTextLength())
                            // $NON-NLS-1$
                            nameAndDefaultValue = "";
                        // $NON-NLS-1$ //$NON-NLS-2$
                        nameAndDefaultValue += requiredAttributeName + "=\"" + defaultValue + "\"";
                        multiTextEdit.addChild(new InsertEdit(index, nameAndDefaultValue));
                    // BUG3381: MultiTextEdit applies all child
                    // TextEdit's basing on offsets
                    // in the document before the first TextEdit, not
                    // after each
                    // child TextEdit. Therefore, do not need to
                    // advance the index.
                    // index += nameAndDefaultValue.length();
                    }
                    multiTextEdit.apply(newNode.getStructuredDocument());
                } catch (BadLocationException e) {
                    // log or now, unless we find reason not to
                    Logger.log(Logger.INFO, e.getMessage());
                }
            }
        }
    }
    return newNode;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) InsertEdit(org.eclipse.text.edits.InsertEdit) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ArrayList(java.util.ArrayList) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 100 with IStructuredDocumentRegion

use of org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion in project webtools.sourceediting by eclipse.

the class ElementNodeCleanupHandler method compressEmptyElementTag.

/**
 * <p>Compress empty element tags if the prefence is set to do so</p>
 *
 * @copyof org.eclipse.wst.xml.core.internal.cleanup.ElementNodeCleanupHandler#compressEmptyElementTag
 *
 * @param node the {@link IDOMNode} to possible compress
 * @return the compressed node if the given node should be compressed, else the node as it was given
 */
private IDOMNode compressEmptyElementTag(IDOMNode node) {
    boolean compressEmptyElementTags = getCleanupPreferences().getCompressEmptyElementTags();
    IDOMNode newNode = node;
    IStructuredDocumentRegion startTagStructuredDocumentRegion = newNode.getFirstStructuredDocumentRegion();
    IStructuredDocumentRegion endTagStructuredDocumentRegion = newNode.getLastStructuredDocumentRegion();
    // only compress tags if they are empty
    if ((compressEmptyElementTags && startTagStructuredDocumentRegion != endTagStructuredDocumentRegion && startTagStructuredDocumentRegion != null)) {
        // only compress end tags if its XHTML or not a container
        if (isXMLTag((IDOMElement) newNode) || !newNode.isContainer()) {
            ITextRegionList regions = startTagStructuredDocumentRegion.getRegions();
            ITextRegion lastRegion = regions.get(regions.size() - 1);
            // format children and end tag if not empty element tag
            if (lastRegion.getType() != DOMRegionContext.XML_EMPTY_TAG_CLOSE) {
                NodeList childNodes = newNode.getChildNodes();
                if (childNodes == null || childNodes.getLength() == 0 || (childNodes.getLength() == 1 && (childNodes.item(0)).getNodeType() == Node.TEXT_NODE && ((childNodes.item(0)).getNodeValue().trim().length() == 0))) {
                    IDOMModel structuredModel = newNode.getModel();
                    IStructuredDocument structuredDocument = structuredModel.getStructuredDocument();
                    int startTagStartOffset = newNode.getStartOffset();
                    int offset = endTagStructuredDocumentRegion.getStart();
                    int length = endTagStructuredDocumentRegion.getLength();
                    // $NON-NLS-1$
                    structuredDocument.replaceText(structuredDocument, offset, length, "");
                    // save
                    newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
                    offset = startTagStructuredDocumentRegion.getStart() + lastRegion.getStart();
                    // $NON-NLS-1$
                    structuredDocument.replaceText(structuredDocument, offset, 0, "/");
                    // save
                    newNode = (IDOMNode) structuredModel.getIndexedRegion(startTagStartOffset);
                }
            }
        }
    }
    return newNode;
}
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) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) NodeList(org.w3c.dom.NodeList) IStructuredDocument(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument) IDOMElement(org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement)

Aggregations

IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)439 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)174 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)99 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)87 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)70 List (java.util.List)40 BadLocationException (org.eclipse.jface.text.BadLocationException)39 ArrayList (java.util.ArrayList)38 Iterator (java.util.Iterator)35 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)35 Node (org.w3c.dom.Node)30 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)26 RegionIterator (org.eclipse.wst.css.core.internal.util.RegionIterator)19 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)19 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)17 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)15 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)15 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)14 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)13 NodeList (org.w3c.dom.NodeList)13