Search in sources :

Example 76 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class RegionComparator method compare.

@Override
public int compare(T t1, T t2) {
    ITextRegion o1 = regionAccess.apply(t1);
    ITextRegion o2 = regionAccess.apply(t2);
    int cmp1 = o1.getOffset() - o2.getOffset();
    if (cmp1 != 0)
        return cmp1;
    int cmp2 = o1.getLength() - o2.getLength();
    if (cmp2 != 0)
        return cmp2;
    return 0;
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 77 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class TokenRegionProviderTest method testBug486454.

@Test
public void testBug486454() throws Exception {
    String model = "t";
    ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(model, new TextRegion(1, 0));
    assertEquals(0, tokenRegion.getOffset());
    assertEquals(1, tokenRegion.getLength());
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) Test(org.junit.Test)

Example 78 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class AbstractFormatter2 method isInRequestedRange.

protected boolean isInRequestedRange(EObject obj) {
    Collection<ITextRegion> regions = request.getRegions();
    if (regions.isEmpty())
        return true;
    ITextRegionAccess access = request.getTextRegionAccess();
    IEObjectRegion objRegion = access.regionForEObject(obj);
    if (objRegion == null)
        return false;
    IHiddenRegion previousHidden = objRegion.getPreviousHiddenRegion();
    IHiddenRegion nextHidden = objRegion.getNextHiddenRegion();
    int objOffset = previousHidden != null ? previousHidden.getOffset() : 0;
    int objEnd = nextHidden != null ? nextHidden.getEndOffset() : access.regionForRootEObject().getEndOffset();
    for (ITextRegion region : regions) {
        int regionOffset = region.getOffset();
        int regionEnd = regionOffset + region.getLength();
        if (regionOffset <= objEnd && regionEnd >= objOffset)
            return true;
    }
    return false;
}
Also used : ITextRegionAccess(org.eclipse.xtext.formatting2.regionaccess.ITextRegionAccess) IEObjectRegion(org.eclipse.xtext.formatting2.regionaccess.IEObjectRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) IHiddenRegion(org.eclipse.xtext.formatting2.regionaccess.IHiddenRegion)

Example 79 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class MultiLineJavaDocTypeReferenceProvider method computeEObjectReferencedInComment.

/**
 * {@inheritDoc}
 */
@Override
public EObjectInComment computeEObjectReferencedInComment(XtextResource resource, int offset) {
    IParseResult parseResult = resource.getParseResult();
    if (parseResult != null) {
        INode rootNode = parseResult.getRootNode();
        INode node = NodeModelUtils.findLeafNodeAtOffset(rootNode, offset);
        EObject semanticObject = NodeModelUtils.findActualSemanticObjectFor(node);
        if (semanticObject != null) {
            EReference reference = getEReference(semanticObject, node, offset);
            if (reference != null) {
                IScope scope = getScope(semanticObject, reference, node, offset);
                List<ReplaceRegion> eObjectReferences = computeTypeRefRegions(node);
                for (ReplaceRegion eObjectReference : eObjectReferences) {
                    if (eObjectReference.getOffset() <= offset && offset <= eObjectReference.getEndOffset()) {
                        String eObjectReferenceText = eObjectReference.getText();
                        if (!Strings.isNullOrEmpty(eObjectReferenceText)) {
                            ITextRegion region = new TextRegion(eObjectReference.getOffset(), eObjectReference.getLength());
                            IEObjectDescription candidate = getElementFromScope(scope, node, region, eObjectReferenceText);
                            if (candidate != null) {
                                EObject eObject = candidate.getEObjectOrProxy();
                                if (eObject != null) {
                                    return new EObjectInComment(eObject, region);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObjectInComment(org.eclipse.xtext.documentation.EObjectInComment) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) IParseResult(org.eclipse.xtext.parser.IParseResult) EReference(org.eclipse.emf.ecore.EReference) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Example 80 with ITextRegion

use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.

the class TracingSugar method location.

/**
 * @param obj
 *            the EObject containing the feature
 * @param feature
 *            the EStructuralFeature to trace
 * @param idx
 *            the index of the value to trace, in case the feature contains a list, should be <code>-1</code>
 *            otherwise.
 *
 * @return ILocationData covering the <code>fullTextRegion</code> of the given feature in the given EObject.
 */
public ILocationData location(EObject obj, EStructuralFeature feature, int idx) {
    ITextRegion region = locationProvider.getFullTextRegion(obj, feature, idx);
    SourceRelativeURI uri = traceURIConverter.getURIForTrace(obj.eResource());
    return new LocationData((ITextRegionWithLineInformation) region, uri);
}
Also used : LocationData(org.eclipse.xtext.generator.trace.LocationData) ILocationData(org.eclipse.xtext.generator.trace.ILocationData) ITextRegion(org.eclipse.xtext.util.ITextRegion) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI)

Aggregations

ITextRegion (org.eclipse.xtext.util.ITextRegion)122 TextRegion (org.eclipse.xtext.util.TextRegion)44 EObject (org.eclipse.emf.ecore.EObject)33 Test (org.junit.Test)20 INode (org.eclipse.xtext.nodemodel.INode)19 XtextResource (org.eclipse.xtext.resource.XtextResource)17 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)15 ILeafNode (org.eclipse.xtext.nodemodel.ILeafNode)15 BadLocationException (org.eclipse.jface.text.BadLocationException)11 Region (org.eclipse.jface.text.Region)9 XExpression (org.eclipse.xtext.xbase.XExpression)9 IOException (java.io.IOException)8 CoreException (org.eclipse.core.runtime.CoreException)8 IParseResult (org.eclipse.xtext.parser.IParseResult)7 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)7 IRegion (org.eclipse.jface.text.IRegion)6 ITextSelection (org.eclipse.jface.text.ITextSelection)6 Collection (java.util.Collection)5 ArrayList (java.util.ArrayList)4 TextSelection (org.eclipse.jface.text.TextSelection)4