Search code examples
javaeclipse-pluginxtexteclipse-emf-ecore

How to scan a folder containing an EMF resource for sibling resources


I have developed a DSL with xText and recently add somme enhanced completion. In a xText-generated editor when calling completion by Ctrl-Space, the completion handler has to perform a folder scanning to look for symbols in another text file of the same DSL. The entry point is :

public class TypesProposalProvider extends AbstractTypesProposalProvider
{
   public void completeQualifiedName_Path(
      EObject                     model,
      Assignment                  assignment,
      ContentAssistContext        context,
      ICompletionProposalAcceptor acceptor )
   {
      super.completeQualifiedName_Path( model, assignment, context, acceptor );

I use:

      Model root = (Model) context.getRootModel();
      Resource rootRc = root.eResource();

to obtain the emf.ecore container of the model.

And now, how can I look for sibling resources, the other files in the same folder, in term of ecore resource?

With the another resources, I'll call Resource.load() to populate the underlaying emf.ecore model of the siblings.

I hope you understand my approximative English (I'm French)...


Solution

  • Here is the final version, compact as usual ;-) :

    Resource   rootRc = root.eResource();
    String     rcPath = rootRc.getURI().toPlatformString( true );
    IFile      file   = (IFile)ResourcesPlugin.getWorkspace().getRoot().findMember( rcPath );
    IContainer parent = file.getParent();
    for( IResource member : parent.members())
    {
       String ext = member.getFileExtension();
       if( ext != null && ext.equals( "types" ))
       {
          String prefix     = member.getName();
          String path       = member.getLocation().toString();
          URI    uriSibling = URI.createFileURI( path );
          prefix = prefix.substring( 0, prefix.length() - ".types".length());
          if( ! rcPath.endsWith( '/' + prefix + ".types" )
             && ( context.getPrefix().isEmpty() || prefix.startsWith( cntxtPrefix )))
          {
             Resource types  = rs.createResource( uriSibling );
             types.load( null );
             for( EObject rc : types.getContents())
             {
                ...
             }
          }
       }
    }