Search in sources :

Example 56 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project webtools.sourceediting by eclipse.

the class DefaultPartitionerZeroLengthTest method assertComputeZeroLengthPartitioning_InterleavingPartitions.

private void assertComputeZeroLengthPartitioning_InterleavingPartitions(int startOffset, int endOffset, int[] offsets, String startType) {
    ITypedRegion[] regions = fPartitioner.computePartitioning(startOffset, endOffset - startOffset, true);
    String type = startType;
    int previousOffset = startOffset;
    assertEquals(offsets.length + 1, regions.length);
    for (int i = 0; i <= offsets.length; i++) {
        int currentOffset = (i == offsets.length) ? endOffset : offsets[i];
        ITypedRegion region = regions[i];
        assertTypedRegion(region, previousOffset, currentOffset, type);
        // advance
        if (type == DEFAULT)
            type = COMMENT;
        else
            type = DEFAULT;
        previousOffset = currentOffset;
    }
}
Also used : ITypedRegion(org.eclipse.jface.text.ITypedRegion)

Example 57 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project webtools.sourceediting by eclipse.

the class BasicStructuredDocument method getPartition.

public ITypedRegion getPartition(String partitioning, int offset, boolean preferOpenPartitions) throws BadLocationException, BadPartitioningException {
    if ((0 > offset) || (offset > getLength()))
        throw new BadLocationException();
    ITypedRegion result = null;
    IDocumentPartitioner partitioner = getDocumentPartitioner(partitioning);
    if (partitioner instanceof IDocumentPartitionerExtension2) {
        result = ((IDocumentPartitionerExtension2) partitioner).getPartition(offset, preferOpenPartitions);
    } else if (partitioner != null) {
        result = partitioner.getPartition(offset);
    } else if (IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING.equals(partitioning)) {
        result = new TypedRegion(0, getLength(), DEFAULT_CONTENT_TYPE);
    } else
        throw new BadPartitioningException();
    return result;
}
Also used : BadPartitioningException(org.eclipse.jface.text.BadPartitioningException) IDocumentPartitionerExtension2(org.eclipse.jface.text.IDocumentPartitionerExtension2) IDocumentPartitioner(org.eclipse.jface.text.IDocumentPartitioner) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TypedRegion(org.eclipse.jface.text.TypedRegion) ITypedRegion(org.eclipse.jface.text.ITypedRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 58 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project webtools.sourceediting by eclipse.

the class StructuredTextPartitioner method computePartitioning.

/**
 * Returns the partitioning of the given range of the connected document.
 * There must be a document connected to this partitioner.
 *
 * Note: this shouldn't be called directly by clients, unless they control
 * the threading that includes modifications to the document. Otherwise
 * the document could be modified while partitions are being computed. We
 * advise that clients use the computePartitions API directly from the
 * document, so they won't have to worry about that.
 *
 * @param offset
 *            the offset of the range of interest
 * @param length
 *            the length of the range of interest
 * @return the partitioning of the range
 */
public ITypedRegion[] computePartitioning(int offset, int length) {
    if (fStructuredDocument == null) {
        // $NON-NLS-1$
        throw new IllegalStateException("document partitioner is not connected");
    }
    ITypedRegion[] results = null;
    synchronized (cachedPartitions) {
        if ((!cachedPartitions.isInValid) && (offset == cachedPartitions.fOffset) && (length == cachedPartitions.fLength))
            results = cachedPartitions.fPartitions;
    }
    if (results == null) {
        if (length == 0) {
            results = new ITypedRegion[] { getPartition(offset) };
        } else {
            List list = new ArrayList();
            int endPos = offset + length;
            if (endPos > fStructuredDocument.getLength()) {
                // and everyone's not yet up to date
                return new ITypedRegion[] { createPartition(offset, length, getUnknown()) };
            }
            int currentPos = offset;
            IStructuredTypedRegion previousPartition = null;
            while (currentPos < endPos) {
                IStructuredTypedRegion partition = null;
                synchronized (PARTITION_LOCK) {
                    internalGetPartition(currentPos, false);
                    currentPos += internalReusedTempInstance.getLength();
                    // instance.
                    if (previousPartition != null && internalReusedTempInstance.getType().equals(previousPartition.getType())) {
                        // same partition type
                        previousPartition.setLength(previousPartition.getLength() + internalReusedTempInstance.getLength());
                    } else {
                        // not the same, so add to list
                        partition = createNewPartitionInstance();
                    }
                }
                if (partition != null) {
                    list.add(partition);
                    // and make current, previous
                    previousPartition = partition;
                }
            }
            results = new ITypedRegion[list.size()];
            list.toArray(results);
        }
        if (results.length > 0) {
            // truncate returned results to requested range
            if (results[0].getOffset() < offset && results[0] instanceof IStructuredRegion) {
                ((IStructuredRegion) results[0]).setOffset(offset);
            }
            int lastEnd = results[results.length - 1].getOffset() + results[results.length - 1].getLength();
            if (lastEnd > offset + length && results[results.length - 1] instanceof IStructuredRegion) {
                ((IStructuredRegion) results[results.length - 1]).setLength(offset + length - results[results.length - 1].getOffset());
            }
        }
        synchronized (cachedPartitions) {
            cachedPartitions.fLength = length;
            cachedPartitions.fOffset = offset;
            cachedPartitions.fPartitions = results;
            cachedPartitions.isInValid = false;
        }
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) ITypedRegion(org.eclipse.jface.text.ITypedRegion) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 59 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project webtools.sourceediting by eclipse.

the class TaglibHyperlinkDetector method detectHyperlinks.

public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
    IHyperlink hyperlink = null;
    if (textViewer != null && region != null) {
        IDocument doc = textViewer.getDocument();
        if (doc != null) {
            try {
                // check if jsp tag/directive first
                ITypedRegion partition = TextUtilities.getPartition(doc, IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING, region.getOffset(), false);
                if (partition != null && partition.getType() == IJSPPartitions.JSP_DIRECTIVE) {
                    IStructuredModel sModel = null;
                    try {
                        sModel = StructuredModelManager.getModelManager().getExistingModelForRead(doc);
                        // check if jsp taglib directive
                        Node currentNode = getCurrentNode(sModel, region.getOffset());
                        if (currentNode != null && currentNode.getNodeType() == Node.ELEMENT_NODE) {
                            String baseLocationForTaglib = getBaseLocationForTaglib(doc);
                            if (baseLocationForTaglib != null && JSP11Namespace.ElementName.DIRECTIVE_TAGLIB.equalsIgnoreCase(currentNode.getNodeName())) {
                                /**
                                 * The taglib directive itself
                                 */
                                // get the uri attribute
                                Attr taglibURINode = ((Element) currentNode).getAttributeNode(JSP11Namespace.ATTR_NAME_URI);
                                if (taglibURINode != null) {
                                    ITaglibRecord reference = TaglibIndex.resolve(baseLocationForTaglib, taglibURINode.getValue(), false);
                                    // there's nothing to link to
                                    if (reference != null) {
                                        // handle taglibs
                                        switch(reference.getRecordType()) {
                                            case (ITaglibRecord.TLD):
                                                {
                                                    ITLDRecord record = (ITLDRecord) reference;
                                                    String uriString = record.getPath().toString();
                                                    IRegion hyperlinkRegion = getHyperlinkRegion(taglibURINode, region);
                                                    if (hyperlinkRegion != null) {
                                                        hyperlink = createHyperlink(uriString, hyperlinkRegion, doc, null);
                                                    }
                                                }
                                                break;
                                            case (ITaglibRecord.JAR):
                                            case (ITaglibRecord.URL):
                                                {
                                                    IRegion hyperlinkRegion = getHyperlinkRegion(taglibURINode, region);
                                                    if (hyperlinkRegion != null) {
                                                        hyperlink = new TaglibJarUriHyperlink(hyperlinkRegion, reference);
                                                    }
                                                }
                                        }
                                    }
                                }
                            } else if (baseLocationForTaglib != null && JSP12Namespace.ElementName.ROOT.equalsIgnoreCase(currentNode.getNodeName())) {
                                /**
                                 * The jsp:root element
                                 */
                                NamedNodeMap attrs = currentNode.getAttributes();
                                for (int i = 0; i < attrs.getLength(); i++) {
                                    Attr attr = (Attr) attrs.item(i);
                                    if (attr.getNodeName().startsWith(XMLNS)) {
                                        String uri = StringUtils.strip(attr.getNodeValue());
                                        if (uri.startsWith(URN_TLD)) {
                                            uri = uri.substring(URN_TLD.length());
                                        }
                                        ITaglibRecord reference = TaglibIndex.resolve(baseLocationForTaglib, uri, false);
                                        // there's nothing to link to
                                        if (reference != null) {
                                            // handle taglibs
                                            switch(reference.getRecordType()) {
                                                case (ITaglibRecord.TLD):
                                                    {
                                                        ITLDRecord record = (ITLDRecord) reference;
                                                        String uriString = record.getPath().toString();
                                                        IRegion hyperlinkRegion = getHyperlinkRegion(attr, region);
                                                        if (hyperlinkRegion != null) {
                                                            hyperlink = createHyperlink(uriString, hyperlinkRegion, doc, null);
                                                        }
                                                    }
                                                    break;
                                                case (ITaglibRecord.JAR):
                                                case (ITaglibRecord.URL):
                                                    {
                                                        IRegion hyperlinkRegion = getHyperlinkRegion(attr, region);
                                                        if (hyperlinkRegion != null) {
                                                            hyperlink = new TaglibJarUriHyperlink(hyperlinkRegion, reference);
                                                        }
                                                    }
                                            }
                                        }
                                    }
                                }
                            } else {
                                /**
                                 * Hyperlink custom tag to its TLD or tag file
                                 */
                                TLDCMDocumentManager documentManager = TaglibController.getTLDCMDocumentManager(doc);
                                if (documentManager != null) {
                                    List documentTrackers = documentManager.getCMDocumentTrackers(currentNode.getPrefix(), region.getOffset());
                                    for (int i = 0; i < documentTrackers.size(); i++) {
                                        TaglibTracker tracker = (TaglibTracker) documentTrackers.get(i);
                                        CMElementDeclaration decl = (CMElementDeclaration) tracker.getElements().getNamedItem(currentNode.getNodeName());
                                        if (decl != null) {
                                            decl = (CMElementDeclaration) ((CMNodeWrapper) decl).getOriginNode();
                                            if (decl instanceof CMElementDeclarationImpl) {
                                                String base = ((CMElementDeclarationImpl) decl).getLocationString();
                                                IRegion hyperlinkRegion = getHyperlinkRegion(currentNode, region);
                                                if (hyperlinkRegion != null) {
                                                    hyperlink = createHyperlink(base, hyperlinkRegion, doc, currentNode);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    } finally {
                        if (sModel != null)
                            sModel.releaseFromRead();
                    }
                }
            } catch (BadLocationException e) {
                Logger.log(Logger.WARNING_DEBUG, e.getMessage(), e);
            }
        }
    }
    if (hyperlink != null)
        return new IHyperlink[] { hyperlink };
    return null;
}
Also used : TLDCMDocumentManager(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TLDCMDocumentManager) NamedNodeMap(org.w3c.dom.NamedNodeMap) ITaglibRecord(org.eclipse.jst.jsp.core.taglib.ITaglibRecord) ITLDRecord(org.eclipse.jst.jsp.core.taglib.ITLDRecord) TaglibTracker(org.eclipse.jst.jsp.core.internal.contentmodel.tld.TaglibTracker) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr) IDOMAttr(org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr) IRegion(org.eclipse.jface.text.IRegion) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) ITypedRegion(org.eclipse.jface.text.ITypedRegion) CMElementDeclarationImpl(org.eclipse.jst.jsp.core.internal.contentmodel.tld.CMElementDeclarationImpl) List(java.util.List) ITextRegionList(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList) NodeList(org.w3c.dom.NodeList) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 60 with ITypedRegion

use of org.eclipse.jface.text.ITypedRegion in project webtools.sourceediting by eclipse.

the class StructuredPresentationReconciler method createPresentation.

/**
 * Constructs a "repair description" for the given damage and returns this
 * description as a text presentation. For this, it queries the partitioning
 * of the damage region and asks the appropriate presentation repairer for
 * each partition to construct the "repair description" for this partition.
 *
 * @param damage the damage to be repaired
 * @param document the document whose presentation must be repaired
 * @return the presentation repair description as text presentation or
 *         <code>null</code> if the partitioning could not be computed
 */
protected TextPresentation createPresentation(IRegion damage, IDocument document) {
    try {
        int validLength = Math.min(damage.getLength(), document.getLength() - damage.getOffset());
        if (fRepairers == null || fRepairers.isEmpty()) {
            TextPresentation presentation = new TextPresentation(damage, 1);
            presentation.setDefaultStyleRange(new StyleRange(damage.getOffset(), validLength, null, null));
            return presentation;
        }
        TextPresentation presentation = new TextPresentation(damage, 1000);
        ITypedRegion[] partitions = TextUtilities.computePartitioning(document, getDocumentPartitioning(), damage.getOffset(), validLength, false);
        for (int i = 0; i < partitions.length; i++) {
            ITypedRegion r = partitions[i];
            IPresentationRepairer repairer = getRepairer(r.getType());
            if (repairer != null)
                repairer.createPresentation(presentation, r);
        }
        return presentation;
    } catch (BadLocationException x) {
    /* ignored in platform PresentationReconciler, too */
    }
    return null;
}
Also used : IPresentationRepairer(org.eclipse.jface.text.presentation.IPresentationRepairer) StyleRange(org.eclipse.swt.custom.StyleRange) ITypedRegion(org.eclipse.jface.text.ITypedRegion) TextPresentation(org.eclipse.jface.text.TextPresentation) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

ITypedRegion (org.eclipse.jface.text.ITypedRegion)129 BadLocationException (org.eclipse.jface.text.BadLocationException)59 IRegion (org.eclipse.jface.text.IRegion)42 IDocument (org.eclipse.jface.text.IDocument)21 Region (org.eclipse.jface.text.Region)19 ArrayList (java.util.ArrayList)17 TypedRegion (org.eclipse.jface.text.TypedRegion)16 BadPositionCategoryException (org.eclipse.jface.text.BadPositionCategoryException)14 Position (org.eclipse.jface.text.Position)14 TypedPosition (org.eclipse.jface.text.TypedPosition)13 List (java.util.List)11 StyleRange (org.eclipse.swt.custom.StyleRange)7 Test (org.junit.Test)7 Iterator (java.util.Iterator)6 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)6 ITextSelection (org.eclipse.jface.text.ITextSelection)5 BadPartitioningException (org.eclipse.jface.text.BadPartitioningException)4 Document (org.eclipse.jface.text.Document)4 IDocumentExtension3 (org.eclipse.jface.text.IDocumentExtension3)4 IDocumentPartitioner (org.eclipse.jface.text.IDocumentPartitioner)4