title: Simple import instruction in XText
body: <p>I want to add some of the objects from another file in XText to the scope.  To illustrate what I need, I have this toy grammar:</p>
  
  <pre><code>grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
  generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
  
  Model:
     (imports += Import)*
     (classes += ClassDef )*;
  
  Import:
      'import' filename += STRING;    
  
  ClassDef:
      'class' name=ID ('extends' extends=[ClassDef])?;
  </code></pre>
  
  <p>Lets say I want to be able to extend only the classes that are defined before a given class, and only those that start with the same first letter as the class being defined.</p>
  
  <p>A recommended way of doing that is via <a href="https://eclipse.org/Xtext/documentation/303_runtime_concepts.html#scoping" rel="nofollow noreferrer">scoping</a>. Here is the scope provider that does it within a single file:</p>
  
  <pre><code>package org.xtext.example.mydsl.scoping
  import ....
  
  class MyDslScopeProvider extends AbstractMyDslScopeProvider {
      override getScope(EObject context, EReference reference){
          if(reference ==  MyDslPackage.Literals.CLASS_DEF__EXTENDS){
              if(! (context instanceof ClassDef) ) {return IScope.NULLSCOPE }
              val root = (context as ClassDef).getRootContainer
              val classes = root.getAllContentsOfType(ClassDef)
              val before = classes.subList(0, classes.indexOf(context))
              val fstLetter = (context as ClassDef).name.charAt(0)
              val haveSame1stLetter = before.filter[name.charAt(0) == fstLetter]          
              return Scopes::scopeFor(haveSame1stLetter)
          }
      }
  }
  </code></pre>
  
  <p>I want to be able to access the class declarations from imported files like this:</p>
  
  <pre><code>// --- c ---
  class Xlib
  class Xextend extends Xlib
  
  // --- main.mydsl ---
  import "lib.mydsl"
  class A
  class Xmain extends Xlib
  </code></pre>
  
  <p>I've found some conflicting suggestions about using <code>importURI</code> thing, but it doesn't seem to work for me and seems to be and outdated way of doing things now.</p>
  
  <p>The documentation mentions that for cross-resource references I probably need to: </p>
  
  <ul>
  <li>create an instance of <code>IGlobalScopeProvider</code>, </li>
  <li>export elements as part of <code>IResourceDescription</code></li>
  <li>convert between a <code>QualifiedName</code> and its String representation </li>
  <li>implement and bind my own <code>IDefaultResourceDescriptionStrategy</code></li>
  <li>get <code>IContainer</code> from <code>IContainer.Manager</code> </li>
  <li>get <code>IResourceDescriptions</code> (note the plural form) from <code>IContainer</code> </li>
  </ul>
  
  <p>... and many more other things. Unfortunately, they do not add up into a coherent picture of what I have to do to a achieve a desired behavior.    </p>
  
  <hr>
  
  <p><strong>Edit:</strong> Following Sven's suggestion I've come up with the solution. If anyone is interested -- I've posted the code in <a href="https://gist.github.com/KKostya/c380e86a67f94c5b7d45646149e29252" rel="nofollow noreferrer">this gist</a>. </p>