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;
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations