简体   繁体   English

如何扫描包含EMF资源的文件夹以获取同级资源

[英]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. 我用xText开发了DSL,最近又添加了somme增强功能。 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. 在xText生成的编辑器中,当通过Ctrl-Space调用完成时,完成处理程序必须执行文件夹扫描以在同一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. 获取模型的emf.ecore容器。

And now, how can I look for sibling resources, the other files in the same folder, in term of ecore resource? 现在,如何使用ecore资源来查找同级资源(同一文件夹中的其他文件)?

With the another resources, I'll call Resource.load() to populate the underlaying emf.ecore model of the siblings. 对于其他资源,我将调用Resource.load()来填充兄弟姐妹的底层emf.ecore模型。

I hope you understand my approximative English (I'm French)... 希望您能理解我的近似英语(我是法语)...

I am assuming that the sibling models dont refer each other. 我假设同级模型不会互相引用。 In that case you can use WorkspaceSynchronizer to get the file from the resource. 在这种情况下,您可以使用WorkspaceSynchronizer从资源中获取文件。

Example

Resource rootRc = root.eResource();
IFile file = WorkspaceSynchronizer.getFile(rootRc);

IResource parent = file.getParent();

Iresource[] childern = parent.members();

for(<iterate over children>)
     load the children resources. 

Hope this helps. 希望这可以帮助。

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())
         {
            ...
         }
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM