Search in sources :

Example 1 with GenericDeclaration

use of org.eclipse.n4js.n4JS.GenericDeclaration in project n4js by eclipse.

the class FindReferencesXpectMethod method findReferences.

/**
 * This Xpect methods compares all computed references at a given EObject to the expected references. The expected
 * references include the line number.
 */
@Xpect
@ParameterParser(syntax = "('at' arg1=OFFSET)?")
public void findReferences(@N4JSCommaSeparatedValuesExpectation IN4JSCommaSeparatedValuesExpectation expectation, IEObjectCoveringRegion offset) {
    // When you write Xpect test methods, ALWAYS retrieve eObject via IEObjectCoveringRegion to get the right
    // eObject!
    // Do NOT use EObject arg1!
    EObject context = offset.getEObject();
    EObject argEObj = offsetHelper.resolveElementAt((XtextResource) context.eResource(), offset.getOffset());
    // If not a cross-reference element, use context instead
    if (argEObj == null)
        argEObj = context;
    EObject eObj = argEObj;
    if (argEObj instanceof ParameterizedTypeRef)
        eObj = ((ParameterizedTypeRef) argEObj).getDeclaredType();
    List<EObject> refs = findReferenceHelper.findReferences(eObj);
    ArrayList<String> result = Lists.newArrayList();
    for (EObject ref : refs) {
        if (ref instanceof PropertyNameOwner)
            ref = ((PropertyNameOwner) ref).getDeclaredName();
        ICompositeNode srcNode = NodeModelUtils.getNode(ref);
        int line = srcNode.getStartLine();
        String moduleName;
        if (ref.eResource() instanceof N4JSResource) {
            N4JSResource n4jsResource = (N4JSResource) ref.eResource();
            moduleName = n4jsResource.getModule().getQualifiedName();
        } else {
            moduleName = "(unknown resource)";
        }
        String text = NodeModelUtils.getTokenText(srcNode);
        if (ref instanceof GenericDeclaration)
            text = ((GenericDeclaration) ref).getDefinedType().getName();
        String resultText = moduleName + " - " + text + " - " + line;
        result.add(resultText);
    }
    expectation.assertEquals(result);
}
Also used : ParameterizedTypeRef(org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef) PropertyNameOwner(org.eclipse.n4js.n4JS.PropertyNameOwner) EObject(org.eclipse.emf.ecore.EObject) N4JSResource(org.eclipse.n4js.resource.N4JSResource) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) GenericDeclaration(org.eclipse.n4js.n4JS.GenericDeclaration) Xpect(org.eclipse.xpect.runner.Xpect) ParameterParser(org.eclipse.xpect.parameter.ParameterParser)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)1 GenericDeclaration (org.eclipse.n4js.n4JS.GenericDeclaration)1 PropertyNameOwner (org.eclipse.n4js.n4JS.PropertyNameOwner)1 N4JSResource (org.eclipse.n4js.resource.N4JSResource)1 ParameterizedTypeRef (org.eclipse.n4js.ts.typeRefs.ParameterizedTypeRef)1 ParameterParser (org.eclipse.xpect.parameter.ParameterParser)1 Xpect (org.eclipse.xpect.runner.Xpect)1 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)1