Search in sources :

Example 81 with ITextRegionList

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

the class ElementNodeFormatter method getUndefinedRegions.

protected String getUndefinedRegions(IDOMNode node, int startOffset, int length) {
    String result = NodeFormatter.EMPTY_STRING;
    IStructuredDocumentRegion flatNode = node.getFirstStructuredDocumentRegion();
    ITextRegionList regions = flatNode.getRegions();
    for (int i = 0; i < regions.size(); i++) {
        ITextRegion region = regions.get(i);
        String regionType = region.getType();
        int regionStartOffset = flatNode.getStartOffset(region);
        if (regionType.compareTo(DOMRegionContext.UNDEFINED) == 0 && regionStartOffset >= startOffset && regionStartOffset < startOffset + length)
            result = result + flatNode.getFullText(region);
    }
    if (result.length() > 0)
        return SPACE_CHAR + result.trim();
    else
        return result;
}
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 82 with ITextRegionList

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

the class LineStyleProviderForXSL method prepareTextRegion.

/**
 * @param region
 * @param start
 * @param length
 * @param holdResults
 * @return
 */
protected boolean prepareTextRegion(ITextRegionCollection blockedRegion, int partitionStartOffset, int partitionLength, Collection holdResults) {
    boolean handled = false;
    final int partitionEndOffset = partitionStartOffset + partitionLength - 1;
    ITextRegion region = null;
    ITextRegionList regions = blockedRegion.getRegions();
    StyleRange styleRange = null;
    for (int i = 0; i < regions.size(); i++) {
        region = regions.get(i);
        TextAttribute attr = null;
        TextAttribute previousAttr = null;
        if (blockedRegion.getStartOffset(region) > partitionEndOffset)
            break;
        if (blockedRegion.getEndOffset(region) <= partitionStartOffset)
            continue;
        if (region instanceof ITextRegionCollection) {
            handled = prepareTextRegion((ITextRegionCollection) region, partitionStartOffset, partitionLength, holdResults);
        } else {
            attr = getAttributeFor(blockedRegion, region);
            if (attr != null) {
                handled = true;
                styleRange = applyStyleRange(blockedRegion, partitionStartOffset, partitionLength, holdResults, region, styleRange, attr, previousAttr);
            } else {
                previousAttr = null;
            }
        }
    }
    return handled;
}
Also used : ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) TextAttribute(org.eclipse.jface.text.TextAttribute) StyleRange(org.eclipse.swt.custom.StyleRange) ITextRegionCollection(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionCollection)

Example 83 with ITextRegionList

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

the class AbstractXMLModelQueryCompletionProposalComputer 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 84 with ITextRegionList

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

the class AbstractXMLModelQueryCompletionProposalComputer method addAttributeValueProposals.

protected void addAttributeValueProposals(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
    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) {
            CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(elementDecl.getAttributes()) {

                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);
                }
            };
            this.addModelQueryAttributeDeclarations(node, elementDecl, allAttributes);
            String noprefixName = DOMNamespaceHelper.getUnprefixedName(attributeName);
            if (allAttributes != null) {
                attrDecl = (CMAttributeDeclaration) allAttributes.getNamedItem(attributeName);
                if (attrDecl == null) {
                    attrDecl = (CMAttributeDeclaration) allAttributes.getNamedItem(noprefixName);
                }
            }
            if (attrDecl == null) {
                setErrorMessage(XMLUIMessages.No_known_attribute__UI_ + attributeName);
            }
        }
        String currentValue = node.getAttributes().getNamedItem(attributeName).getNodeValue();
        String proposedInfo = null;
        // get proposal image
        Image image = CMImageUtil.getImage(attrDecl);
        if (image == null) {
            if ((attrDecl != null) && (attrDecl.getUsage() == CMAttributeDeclaration.REQUIRED)) {
                image = this.getRequiredAttributeImage();
            } else {
                image = this.getNotRequiredAttributeImage();
            }
        }
        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();
            // $NON-NLS-1$
            String qualifiedDelimiter = (String) attrDecl.getProperty("qualified-delimiter");
            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("'"))) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    matchString = matchString.substring(1);
                }
                boolean currentValid = false;
                // create suggestions for enumerated values
                int rOffset = contentAssistRequest.getReplacementBeginPosition();
                int rLength = contentAssistRequest.getReplacementLength();
                for (Iterator j = possibleValues.iterator(); j.hasNext(); ) {
                    String possibleValue = (String) j.next();
                    String alternateMatch = null;
                    if (qualifiedDelimiter != null) {
                        int delimiter = possibleValue.lastIndexOf(qualifiedDelimiter);
                        if (delimiter >= 0 && delimiter < possibleValue.length() - 1) {
                            alternateMatch = possibleValue.substring(delimiter + 1);
                        }
                    }
                    if (!possibleValue.equals(defaultValue)) {
                        currentValid = currentValid || possibleValue.equals(currentValue);
                        if ((matchString.length() == 0) || possibleValue.startsWith(matchString)) {
                            // $NON-NLS-1$ //$NON-NLS-2$
                            String rString = "\"" + possibleValue + "\"";
                            // $NON-NLS-1$
                            alternateMatch = "\"" + alternateMatch;
                            CustomCompletionProposal proposal = new MarkupCompletionProposal(rString, rOffset, rLength, possibleValue.length() + 1, XMLEditorPluginImageHelper.getInstance().getImage(XMLEditorPluginImages.IMG_OBJ_ENUM), rString, alternateMatch, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE, true);
                            contentAssistRequest.addProposal(proposal);
                        }
                    }
                }
                if (defaultValue != null && ((matchString.length() == 0) || defaultValue.startsWith(matchString))) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    String rString = "\"" + defaultValue + "\"";
                    final String regionText = contentAssistRequest.getDocumentRegion().getText(contentAssistRequest.getRegion());
                    final int matchStringLength = contentAssistRequest.getMatchString().length();
                    if (matchString.length() > 0 && matchStringLength < regionText.length()) {
                        final String remaining = regionText.substring(matchStringLength).trim();
                        if (remaining.charAt(0) != '\'' && remaining.charAt(0) != '"') {
                            rLength = matchStringLength;
                        }
                    }
                    CustomCompletionProposal proposal = new MarkupCompletionProposal(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 MarkupCompletionProposal(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 MarkupCompletionProposal(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)) {
                final String regionText = open.getText(contentAssistRequest.getRegion());
                if (regionText.charAt(0) != '"' && regionText.charAt(0) != '\'') {
                    // $NON-NLS-2$//$NON-NLS-1$
                    String rValue = "\"" + currentValue + "\"";
                    proposal = new MarkupCompletionProposal(rValue, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), 1, image, rValue, null, proposedInfo, XMLRelevanceConstants.R_XML_ATTRIBUTE_VALUE);
                    contentAssistRequest.addProposal(proposal);
                }
            }
        }
    } else {
        setErrorMessage(XMLUIMessages.Content_Assist_not_availab_UI_);
    }
}
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) 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) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) Map(java.util.Map) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) HashMap(java.util.HashMap) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 85 with ITextRegionList

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

the class AttributeContextInformationPresenter method updatePresentation.

/**
 * @see org.eclipse.jface.text.contentassist.IContextInformationPresenter#updatePresentation(int,
 *      org.eclipse.jface.text.TextPresentation)
 */
public boolean updatePresentation(int documentPosition, TextPresentation presentation) {
    presentation.clear();
    if (!(fInfo instanceof AttributeContextInformation)) {
        return false;
    }
    // iterate existing attributes from current node
    IDOMNode xmlNode = fModelUtil.getXMLNode(documentPosition);
    if (xmlNode == null)
        return false;
    IStructuredDocumentRegion sdRegion = xmlNode.getFirstStructuredDocumentRegion();
    ITextRegionList regions = sdRegion.getRegions();
    ITextRegion r = null;
    // $NON-NLS-1$
    String attrName = "";
    Object temp = null;
    Position p = null;
    HashMap map = ((AttributeContextInformation) fInfo).getAttr2RangeMap();
    // so we can add ranges in order
    StyleRange[] sorted = new StyleRange[fInfo.getInformationDisplayString().length()];
    for (int i = 0; i < regions.size(); i++) {
        r = regions.get(i);
        if (r.getType() == DOMRegionContext.XML_TAG_ATTRIBUTE_NAME) {
            attrName = sdRegion.getText(r);
            temp = map.get(attrName);
            if (temp != null) {
                p = (Position) temp;
                sorted[p.offset] = new StyleRange(p.offset, p.length, null, null, SWT.BOLD);
            }
        }
    }
    // style ranges need to be added in order
    StyleRange sr = null;
    for (int i = 0; i < sorted.length; i++) {
        sr = sorted[i];
        if (sr != null) {
            presentation.addStyleRange(sr);
        }
    }
    return true;
}
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) Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) StyleRange(org.eclipse.swt.custom.StyleRange)

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