Search in sources :

Example 76 with ITextRegionList

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

the class AbstractContentAssistProcessor method addAttributeValueProposals.

protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest) {
    IDOMNode node = (IDOMNode) contentAssistRequest.getNode();
    // Find the attribute region and name for which this position should
    // have a value proposed
    IStructuredDocumentRegion open = node.getFirstStructuredDocumentRegion();
    ITextRegionList openRegions = open.getRegions();
    int i = openRegions.indexOf(contentAssistRequest.getRegion());
    if (i < 0) {
        return;
    }
    ITextRegion nameRegion = null;
    while (i >= 0) {
        nameRegion = openRegions.get(i--);
        if (nameRegion.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
            break;
        }
    }
    // the name region is REQUIRED to do anything useful
    if (nameRegion != null) {
        // Retrieve the declaration
        CMElementDeclaration elementDecl = getCMElementDeclaration(node);
        // String attributeName = nameRegion.getText();
        String attributeName = open.getText(nameRegion);
        CMAttributeDeclaration attrDecl = null;
        // declaration for the attribute otherwise
        if (elementDecl != null) {
            CMNamedNodeMap attributes = elementDecl.getAttributes();
            CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attributes) {

                private Map caseInsensitive;

                private Map getCaseInsensitiveMap() {
                    if (caseInsensitive == null)
                        caseInsensitive = new HashMap();
                    return caseInsensitive;
                }

                public CMNode getNamedItem(String name) {
                    CMNode node = super.getNamedItem(name);
                    if (node == null) {
                        node = (CMNode) getCaseInsensitiveMap().get(name.toLowerCase(Locale.US));
                    }
                    return node;
                }

                public void put(CMNode cmNode) {
                    super.put(cmNode);
                    getCaseInsensitiveMap().put(cmNode.getNodeName().toLowerCase(Locale.US), cmNode);
                }
            };
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                List nodes = ModelQueryUtil.getModelQuery(node.getOwnerDocument()).getAvailableContent((Element) node, elementDecl, ModelQuery.INCLUDE_ATTRIBUTES);
                for (int k = 0; k < nodes.size(); k++) {
                    CMNode cmnode = (CMNode) nodes.get(k);
                    if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
                        allAttributes.put(cmnode);
                    }
                }
            }
            attributes = allAttributes;
            String noprefixName = DOMNamespaceHelper.getUnprefixedName(attributeName);
            if (attributes != null) {
                attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(noprefixName);
                if (attrDecl == null) {
                    attrDecl = (CMAttributeDeclaration) attributes.getNamedItem(attributeName);
                }
            }
            if (attrDecl == null) {
                setErrorMessage(UNKNOWN_ATTR, attributeName);
            }
        }
        String currentValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();
        String proposedInfo = null;
        Image image = CMImageUtil.getImage(attrDecl);
        if (image == null) {
            if ((attrDecl != null) && (attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED)) {
                image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATT_REQ_OBJ);
            } else {
                image = XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ATTRIBUTE);
            }
        }
        if ((attrDecl != null) && (attrDecl.getAttrType() != null)) {
            // attribute is known, prompt with values from the declaration
            proposedInfo = getAdditionalInfo(elementDecl, attrDecl);
            List possibleValues = getPossibleDataTypeValues(node, attrDecl);
            String defaultValue = attrDecl.getAttrType().getImpliedValue();
            if (possibleValues.size() > 0 || defaultValue != null) {
                // ENUMERATED VALUES
                String matchString = contentAssistRequest.getMatchString();
                if (matchString == null) {
                    // $NON-NLS-1$
                    matchString = "";
                }
                if ((matchString.length() > 0) && (matchString.startsWith("\"") || matchString.startsWith("'"))) {
                    matchString = matchString.substring(1);
                }
                boolean currentValid = false;
                // d210858, if the region's a container, don't suggest the
                // enumerated values as they probably won't help
                boolean existingComplicatedValue = (contentAssistRequest.getRegion() != null) && (contentAssistRequest.getRegion() instanceof ITextRegionContainer);
                if (!existingComplicatedValue) {
                    int rOffset = contentAssistRequest.getReplacementBeginPosition();
                    int rLength = contentAssistRequest.getReplacementLength();
                    for (Iterator j = possibleValues.iterator(); j.hasNext(); ) {
                        String possibleValue = (String) j.next();
                        if (!possibleValue.equals(defaultValue)) {
                            currentValid = currentValid || possibleValue.equals(currentValue);
                            if ((matchString.length() == 0) || possibleValue.startsWith(matchString)) {
                                // $NON-NLS-2$//$NON-NLS-1$
                                String rString = "\"" + possibleValue + "\"";
                                CustomCompletionProposal proposal = new CustomCompletionProposal(rString, rOffset, rLength, possibleValue.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ENUM), rString, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                                contentAssistRequest.addProposal(proposal);
                            }
                        }
                    }
                    if (defaultValue != null && ((matchString.length() == 0) || defaultValue.startsWith(matchString))) {
                        // $NON-NLS-2$//$NON-NLS-1$
                        String rString = "\"" + defaultValue + "\"";
                        CustomCompletionProposal proposal = new CustomCompletionProposal(rString, rOffset, rLength, defaultValue.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_DEFAULT), rString, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            } else if (((attrDecl.getUsage() == CMAttributeDeclaration.FIXED) || (attrDecl.getAttrType().getImpliedValueKind() == CMDataType.IMPLIED_VALUE_FIXED)) && (attrDecl.getAttrType().getImpliedValue() != null)) {
                // FIXED values
                String value = attrDecl.getAttrType().getImpliedValue();
                if ((value != null) && (value.length() > 0)) {
                    // $NON-NLS-2$//$NON-NLS-1$
                    String rValue = "\"" + value + "\"";
                    CustomCompletionProposal proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), rValue.length() + 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                    contentAssistRequest.addProposal(proposal);
                    if ((currentValue.length() > 0) && !value.equals(currentValue)) {
                        // $NON-NLS-2$//$NON-NLS-1$
                        rValue = "\"" + currentValue + "\"";
                        proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), rValue.length() + 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            }
        } else {
            // unknown attribute, so supply nice empty values
            proposedInfo = getAdditionalInfo(null, elementDecl);
            CustomCompletionProposal proposal = null;
            if ((currentValue != null) && (currentValue.length() > 0)) {
                // $NON-NLS-2$//$NON-NLS-1$
                String rValue = "\"" + currentValue + "\"";
                proposal = new CustomCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                contentAssistRequest.addProposal(proposal);
            }
        }
    } else {
        setErrorMessage(UNKNOWN_CONTEXT);
    }
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) HashMap(java.util.HashMap) CustomCompletionProposal(org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) Image(org.eclipse.swt.graphics.Image) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMNodeList(org.eclipse.wst.xml.core.internal.contentmodel.CMNodeList) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) Map(java.util.Map) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 77 with ITextRegionList

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

the class AbstractContentAssistProcessor method getTagName.

private String getTagName(IStructuredDocumentRegion sdRegion) {
    ITextRegionList regions = sdRegion.getRegions();
    ITextRegion region = null;
    // $NON-NLS-1$
    String name = "";
    for (int i = 0; i < regions.size(); i++) {
        region = regions.get(i);
        if (region.getType() == DOMRegionContext.XML_TAG_NAME) {
            name = sdRegion.getText(region);
            break;
        }
    }
    return name;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 78 with ITextRegionList

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

the class ProcessingInstructionImpl method getData.

/**
 * getData method
 *
 * @return java.lang.String
 */
public String getData() {
    if (this.data != null)
        return this.data;
    IStructuredDocumentRegion flatNode = getFirstStructuredDocumentRegion();
    if (flatNode == null)
        return NodeImpl.EMPTY_STRING;
    ITextRegionList regions = flatNode.getRegions();
    if (regions == null)
        return NodeImpl.EMPTY_STRING;
    ITextRegion targetRegion = null;
    ITextRegion dataRegion = null;
    ITextRegion closeRegion = null;
    Iterator e = regions.iterator();
    while (e.hasNext()) {
        ITextRegion region = (ITextRegion) e.next();
        String regionType = region.getType();
        if (regionType == DOMRegionContext.XML_PI_OPEN)
            continue;
        if (regionType == DOMRegionContext.XML_PI_CLOSE) {
            closeRegion = region;
        } else {
            if (targetRegion == null)
                targetRegion = region;
            else if (dataRegion == null)
                dataRegion = region;
        }
    }
    if (dataRegion == null)
        return NodeImpl.EMPTY_STRING;
    int offset = dataRegion.getStart();
    int end = flatNode.getLength();
    if (closeRegion != null)
        end = closeRegion.getStart();
    String source = flatNode.getText();
    return source.substring(offset, end);
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator)

Example 79 with ITextRegionList

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

the class ReadOnlyController method getDataSpanForElement.

private Span getDataSpanForElement(IDOMElement node) {
    IStructuredDocumentRegion docRegion = node.getStartStructuredDocumentRegion();
    if (docRegion == null || docRegion.getParentDocument() == null) {
        return new Span(0, -1);
    }
    ITextRegionList regions = docRegion.getRegions();
    if (regions == null) {
        return new Span(0, -1);
    }
    String startType;
    String endType;
    if (node.isCommentTag()) {
        startType = DOMRegionContext.XML_COMMENT_OPEN;
        endType = DOMRegionContext.XML_COMMENT_CLOSE;
    } else {
        startType = DOMRegionContext.XML_TAG_NAME;
        endType = DOMRegionContext.XML_TAG_CLOSE;
    }
    int startOffset = -1;
    int endOffset = -1;
    ITextRegion prevRegion = null;
    ITextRegion region;
    for (int i = 0; i < regions.size(); i++) {
        region = regions.get(i);
        String type = region.getType();
        if (type == startType) {
            startOffset = region.getEnd();
        } else if (type == endType && prevRegion != null) {
            endOffset = prevRegion.getTextEnd();
        }
        prevRegion = region;
    }
    if (0 <= startOffset && 0 <= endOffset) {
        return new Span(startOffset, endOffset - startOffset);
    } else {
        return new Span(0, -1);
    }
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)

Example 80 with ITextRegionList

use of org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList 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)

Aggregations

ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)193 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)171 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)74 Iterator (java.util.Iterator)46 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)27 ITextRegionContainer (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer)24 ArrayList (java.util.ArrayList)21 List (java.util.List)18 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)16 StyleRange (org.eclipse.swt.custom.StyleRange)13 TextAttribute (org.eclipse.jface.text.TextAttribute)12 LocalizedMessage (org.eclipse.wst.validation.internal.operations.LocalizedMessage)12 TextRegionListImpl (org.eclipse.wst.sse.core.internal.text.TextRegionListImpl)10 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)9 IExtensionPoint (org.eclipse.core.runtime.IExtensionPoint)8 CustomCompletionProposal (org.eclipse.wst.sse.ui.internal.contentassist.CustomCompletionProposal)8 DOMException (org.w3c.dom.DOMException)8 BadLocationException (org.eclipse.jface.text.BadLocationException)7 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)7 ITextRegionCollection (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection)7