Search in sources :

Example 36 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-core by eclipse.

the class XtextLinkingService method getLinkedMetaModel.

private List<EObject> getLinkedMetaModel(TypeRef context, EReference ref, ILeafNode text) throws IllegalNodeException {
    final ICompositeNode parentNode = text.getParent();
    BidiIterator<INode> iterator = parentNode.getChildren().iterator();
    while (iterator.hasPrevious()) {
        INode child = iterator.previous();
        if (child instanceof ILeafNode) {
            ILeafNode leaf = (ILeafNode) child;
            if (text == leaf)
                return super.getLinkedObjects(context, ref, text);
            if (!(leaf.getGrammarElement() instanceof Keyword) && !leaf.isHidden()) {
                IScope scope = getScope(context, ref);
                return XtextMetamodelReferenceHelper.findBestMetamodelForType(context, text.getText(), leaf.getText(), scope);
            }
        }
    }
    return Collections.emptyList();
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) ILeafNode(org.eclipse.xtext.nodemodel.ILeafNode) Keyword(org.eclipse.xtext.Keyword) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IScope(org.eclipse.xtext.scoping.IScope)

Example 37 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-core by eclipse.

the class XtextScopeProvider method createEPackageScope.

protected IScope createEPackageScope(final Grammar grammar) {
    final List<Grammar> allGrammars = getAllGrammars(grammar);
    IScope current = new SimpleScope(IScope.NULLSCOPE, Iterables.transform(EPackage.Registry.INSTANCE.keySet(), new Function<String, IEObjectDescription>() {

        @Override
        public IEObjectDescription apply(String from) {
            InternalEObject proxyPackage = (InternalEObject) EcoreFactory.eINSTANCE.createEPackage();
            proxyPackage.eSetProxyURI(URI.createURI(from));
            return EObjectDescription.create(QualifiedName.create(from), proxyPackage, Collections.singletonMap("nsURI", "true"));
        }
    }));
    for (int i = allGrammars.size() - 1; i >= 0; i--) {
        current = createEPackageScope(allGrammars.get(i), current);
    }
    return current;
}
Also used : Function(com.google.common.base.Function) SimpleScope(org.eclipse.xtext.scoping.impl.SimpleScope) IScope(org.eclipse.xtext.scoping.IScope) Grammar(org.eclipse.xtext.Grammar) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Example 38 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-core by eclipse.

the class ImportedNamespaceAwareLocalScopeProviderTest method testUnambiguousImportAreShadowed_01.

@Test
public void testUnambiguousImportAreShadowed_01() throws Exception {
    final XtextResource resource = getResource(new StringInputStream("foo { " + "  entity Foo {}" + "  entity Bar {}" + "}" + "bar {" + "  entity Foo {}" + "}" + "baz {" + "  import foo.*" + "  import bar.Foo" + "  entity Baz{}" + "}"), URI.createURI("withoutwildcard.indextestlanguage"));
    Iterable<EObject> allContents = new Iterable<EObject>() {

        @Override
        public Iterator<EObject> iterator() {
            return resource.getAllContents();
        }
    };
    Entity foo = find(Iterables.filter(allContents, Entity.class), new Predicate<Entity>() {

        @Override
        public boolean apply(Entity input) {
            return input.getName().equals("Baz");
        }
    });
    IScope scope = scopeProvider.getScope(foo, IndexTestLanguagePackage.eINSTANCE.getProperty_Type());
    assertNotNull(scope.getSingleElement(nameConverter.toQualifiedName("Bar")));
    assertNull(scope.getSingleElement(nameConverter.toQualifiedName("Foo")));
    ArrayList<IEObjectDescription> list = newArrayList(scope.getAllElements());
    assertEquals(6, list.size());
    assertFalse(any(list, new Predicate<IEObjectDescription>() {

        @Override
        public boolean apply(IEObjectDescription input) {
            return input.getName().equals(QualifiedName.create("Foo"));
        }
    }));
}
Also used : Entity(org.eclipse.xtext.index.indexTestLanguage.Entity) StringInputStream(org.eclipse.xtext.util.StringInputStream) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) XtextResource(org.eclipse.xtext.resource.XtextResource) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Example 39 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-core by eclipse.

the class ImportedNamespaceAwareLocalScopeProviderTest method testImports_03.

@Test
public void testImports_03() throws Exception {
    XtextResource resource = getResource(new StringInputStream("import foo.bar"), URI.createURI("import.indextestlanguage"));
    resource.getResourceSet().createResource(URI.createURI("foo.indextestlanguage")).load(new StringInputStream("foo.bar { " + "  entity Person {  " + "    String name " + "  } " + "  datatype String " + "}"), null);
    IScope scope = scopeProvider.getScope(resource.getContents().get(0), IndexTestLanguagePackage.eINSTANCE.getFile_Elements());
    List<QualifiedName> names = toListOfNames(scope.getAllElements());
    assertEquals(names.toString(), 4, names.size());
    assertTrue(names.contains(nameConverter.toQualifiedName("bar")));
    assertTrue(names.contains(nameConverter.toQualifiedName("foo.bar")));
    assertTrue(names.contains(nameConverter.toQualifiedName("foo.bar.Person")));
    assertTrue(names.contains(nameConverter.toQualifiedName("foo.bar.String")));
}
Also used : StringInputStream(org.eclipse.xtext.util.StringInputStream) QualifiedName(org.eclipse.xtext.naming.QualifiedName) IScope(org.eclipse.xtext.scoping.IScope) XtextResource(org.eclipse.xtext.resource.XtextResource) Test(org.junit.Test)

Example 40 with IScope

use of org.eclipse.xtext.scoping.IScope in project xtext-core by eclipse.

the class ImportedNamespaceAwareLocalScopeProviderTest method testUnambiguousImportAreShadowed_00.

@Test
public void testUnambiguousImportAreShadowed_00() throws Exception {
    final XtextResource resource = getResource(new StringInputStream("foo { " + "  entity Foo {}" + "  entity Bar {}" + "}" + "bar {" + "  entity Foo {}" + "}" + "baz {" + "  import foo.*" + "  import bar.*" + "  entity Baz{}" + "}"), URI.createURI("withoutwildcard.indextestlanguage"));
    Iterable<EObject> allContents = new Iterable<EObject>() {

        @Override
        public Iterator<EObject> iterator() {
            return resource.getAllContents();
        }
    };
    Entity foo = find(Iterables.filter(allContents, Entity.class), new Predicate<Entity>() {

        @Override
        public boolean apply(Entity input) {
            return input.getName().equals("Baz");
        }
    });
    IScope scope = scopeProvider.getScope(foo, IndexTestLanguagePackage.eINSTANCE.getProperty_Type());
    assertNotNull(scope.getSingleElement(nameConverter.toQualifiedName("Bar")));
    assertNull(scope.getSingleElement(nameConverter.toQualifiedName("Foo")));
    ArrayList<IEObjectDescription> list = newArrayList(scope.getAllElements());
    assertEquals(6, list.size());
    assertFalse(any(list, new Predicate<IEObjectDescription>() {

        @Override
        public boolean apply(IEObjectDescription input) {
            return input.getName().equals(QualifiedName.create("Foo"));
        }
    }));
}
Also used : Entity(org.eclipse.xtext.index.indexTestLanguage.Entity) StringInputStream(org.eclipse.xtext.util.StringInputStream) EObject(org.eclipse.emf.ecore.EObject) IScope(org.eclipse.xtext.scoping.IScope) XtextResource(org.eclipse.xtext.resource.XtextResource) IEObjectDescription(org.eclipse.xtext.resource.IEObjectDescription) Predicate(com.google.common.base.Predicate) Test(org.junit.Test)

Aggregations

IScope (org.eclipse.xtext.scoping.IScope)148 IEObjectDescription (org.eclipse.xtext.resource.IEObjectDescription)57 Test (org.junit.Test)53 EObject (org.eclipse.emf.ecore.EObject)43 QualifiedName (org.eclipse.xtext.naming.QualifiedName)26 XtextResource (org.eclipse.xtext.resource.XtextResource)19 EReference (org.eclipse.emf.ecore.EReference)17 EClass (org.eclipse.emf.ecore.EClass)15 StringInputStream (org.eclipse.xtext.util.StringInputStream)15 URI (org.eclipse.emf.common.util.URI)12 SimpleScope (org.eclipse.xtext.scoping.impl.SimpleScope)12 ArrayList (java.util.ArrayList)10 Entity (org.eclipse.xtext.index.indexTestLanguage.Entity)10 Resource (org.eclipse.emf.ecore.resource.Resource)9 AbstractScopingTest (com.avaloq.tools.ddk.xtext.test.scoping.AbstractScopingTest)8 JvmDeclaredType (org.eclipse.xtext.common.types.JvmDeclaredType)8 FilteringScope (org.eclipse.xtext.scoping.impl.FilteringScope)7 Predicate (com.google.common.base.Predicate)6 EClassifier (org.eclipse.emf.ecore.EClassifier)6 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)6