Search in sources :

Example 6 with SingletonScope

use of org.eclipse.xtext.scoping.impl.SingletonScope in project xtext-extras by eclipse.

the class SerializerScopeProvider method doGetTypeScope.

protected IScope doGetTypeScope(XFeatureCall call, JvmType type) {
    if (call.isPackageFragment()) {
        if (type instanceof JvmDeclaredType) {
            String packageName = ((JvmDeclaredType) type).getPackageName();
            int dot = packageName.indexOf('.');
            if (dot == -1) {
                return new SingletonScope(EObjectDescription.create(packageName, type), IScope.NULLSCOPE);
            } else {
                String firstSegment = packageName.substring(0, dot);
                return new SingletonScope(EObjectDescription.create(firstSegment, type), IScope.NULLSCOPE);
            }
        }
        return IScope.NULLSCOPE;
    } else {
        if (type instanceof JvmDeclaredType && ((JvmDeclaredType) type).getDeclaringType() != null) {
            Resource resource = call.eResource();
            if (resource instanceof XtextResource) {
                XImportSection importSection = importsConfiguration.getImportSection((XtextResource) resource);
                if (importSection != null) {
                    List<XImportDeclaration> importDeclarations = importSection.getImportDeclarations();
                    List<IEObjectDescription> descriptions = Lists.newArrayList();
                    for (XImportDeclaration importDeclaration : importDeclarations) {
                        if (!importDeclaration.isStatic() && !importDeclaration.isWildcard() && !importDeclaration.isExtension()) {
                            JvmDeclaredType importedType = importDeclaration.getImportedType();
                            if (importedType == type) {
                                String syntax = importsConfiguration.getLegacyImportSyntax(importDeclaration);
                                if (syntax != null && /* no node model attached */
                                syntax.equals(type.getQualifiedName())) {
                                    String packageName = importedType.getPackageName();
                                    descriptions.add(EObjectDescription.create(syntax.substring(packageName.length() + 1), type));
                                }
                            }
                            if (EcoreUtil.isAncestor(importedType, type)) {
                                String name = type.getSimpleName();
                                JvmType worker = type;
                                while (worker != importedType) {
                                    worker = (JvmType) worker.eContainer();
                                    name = worker.getSimpleName() + "$" + name;
                                }
                                descriptions.add(EObjectDescription.create(name, type));
                            }
                        }
                    }
                    return new SimpleScope(descriptions);
                }
            }
            return new SingletonScope(EObjectDescription.create(type.getSimpleName(), type), IScope.NULLSCOPE);
        } else {
            return new SingletonScope(EObjectDescription.create(type.getSimpleName(), type), IScope.NULLSCOPE);
        }
    }
}
Also used : SingletonScope(org.eclipse.xtext.scoping.impl.SingletonScope) XImportSection(org.eclipse.xtext.xtype.XImportSection) SimpleScope(org.eclipse.xtext.scoping.impl.SimpleScope) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) XtextResource(org.eclipse.xtext.resource.XtextResource) JvmType(org.eclipse.xtext.common.types.JvmType) XImportDeclaration(org.eclipse.xtext.xtype.XImportDeclaration) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription)

Aggregations

SingletonScope (org.eclipse.xtext.scoping.impl.SingletonScope)6 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)4 JvmIdentifiableElement (org.eclipse.xtext.common.types.JvmIdentifiableElement)3 QualifiedName (org.eclipse.xtext.naming.QualifiedName)3 JvmConstructor (org.eclipse.xtext.common.types.JvmConstructor)2 JvmExecutable (org.eclipse.xtext.common.types.JvmExecutable)2 JvmType (org.eclipse.xtext.common.types.JvmType)2 XAbstractFeatureCall (org.eclipse.xtext.xbase.XAbstractFeatureCall)2 Resource (org.eclipse.emf.ecore.resource.Resource)1 JvmField (org.eclipse.xtext.common.types.JvmField)1 JvmFormalParameter (org.eclipse.xtext.common.types.JvmFormalParameter)1 JvmMember (org.eclipse.xtext.common.types.JvmMember)1 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 SimpleScope (org.eclipse.xtext.scoping.impl.SimpleScope)1 XAssignment (org.eclipse.xtext.xbase.XAssignment)1 XSwitchExpression (org.eclipse.xtext.xbase.XSwitchExpression)1 XVariableDeclaration (org.eclipse.xtext.xbase.XVariableDeclaration)1 XImportDeclaration (org.eclipse.xtext.xtype.XImportDeclaration)1 XImportSection (org.eclipse.xtext.xtype.XImportSection)1