0
votes

I want to cross-reference in XText not by using some attribute in the other file, instead by file metadata (the file name).

import: "string_msg/msg/StringMessage"

inputs:
    name: testString
    type: types.string_msg.StringMessage   # this is the cross-reference
    description: "Here goes some type of description or the other"      

The language "string_msg/msg/StringMessage" belongs to only contains attribute information, not the type name. I have to derive the type name from the filename, but ideally make the model objects of the referenced file available.

How do I do this? All examples I have seen so far have referenced other EObjects by their name attribute.

1

1 Answers

0
votes

you can adapt the IQualifiedNameProvider and use the EObjects eResource and the resources URI to calculate the name.

package org.xtext.example.mydsl10;

import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.naming.DefaultDeclarativeQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.xtext.example.mydsl10.myDsl.Greeting;

public class MyDslNameProvider extends DefaultDeclarativeQualifiedNameProvider {

    public QualifiedName qualifiedName(Greeting g) {
        URI uri = g.eResource().getURI();
        // this is your job 
        String simpleName = uri.trimFileExtension().lastSegment();
        return QualifiedName.create(simpleName, g.getName());
    }

}

dont forget to bind

class MyDslRuntimeModule extends AbstractMyDslRuntimeModule {

    override bindIQualifiedNameProvider() {
        MyDslNameProvider
    }

}