When to use createLiveScopeResourceDescriptions? [message #804653] |
Wed, 22 February 2012 21:28 |
|
Hello,
I recently stumbled upon an itemis blog post, where I learned that I should not be injecting IResourceDescriptions into my classes, but instead to inject ResourceDescriptionsProvider:
Quote:...So if you want to access the index yourself, be it for modifying scoping, validation or something else, it is important NOT!! simply to inject IResourceDescriptions (this is an extremely common mistake causing linking problems). In Xtext 2 you should use (read: inject) the ResourceDescriptionsProvider...
(from http://blogs.itemis.de/stundzig/archives/809)
After reading this, I changed my classes and found I could obtain the same functionality if I injected ResourceDescriptionsProvider and then called ResourceDescriptionsProvider.createResourceDescriptions().
But I couldn't help but notice the other, similarly-named methods:
- ResourceDescriptionsProvider.createLiveScopeResourceDescriptions()
- ResourceDescriptionsProvider.createBuilderScopeResourceDescriptions()
When would it be appropriate to use each of these? I thought I had a good idea what they meant, but when I called createLiveScopeResourceDescriptions(), I found an empty list returned...
|
|
|
|
Re: When to use createLiveScopeResourceDescriptions? [message #804684 is a reply to message #804673] |
Wed, 22 February 2012 22:15 |
|
Alex,
Thanks for the reply. I had seen that post before - the relevant portion of it seems to be this:
Quote:the LIVE_SCOPE is necessary for refactorings in the UI. It is save to
use the ResourceSetBasedResourceDescriptions there.
I'm still left wondering, however, when I might call ResourceDescriptionsProvider.createLiveScopeResourceDescriptions(). I currently call createResourceDescriptions(), which appears to give me an IResourceDescriptions instance (Xtext index) containing all IResourceDescription instances in my workspace. Could createLiveScopeResourceDescriptions() potentially give me access to exported objects in an unsaved file or something?
|
|
|
|
Re: When to use createLiveScopeResourceDescriptions? [message #805235 is a reply to message #805053] |
Thu, 23 February 2012 14:29 |
|
Jan,
Thanks for the clarification. It seems I've been using ResourceDescriptionsProvider incorrectly, as I used it the following way to obtain all IResourceDescription instances for a given Eclipse project:
public class TplFile {
@Inject
private ResourceDescriptionsProvider resourceDescriptionsProvider;
public List<IResourceDescription> getResourceDescriptionsFor(IProject project)
{
List<IResourceDescription> descriptions = new ArrayList<IResourceDescription>();
IResourceDescriptions xtextIndex = resourceDescriptionsProvider.createResourceDescriptions();
for (IResourceDescription desc : xtextIndex.getAllResourceDescriptions())
{
org.eclipse.emf.common.util.URI descUri = desc.getURI();
List<String> uriSegments = descUri.segmentsList();
String projectSegment = uriSegments.get(1);
if (project.getName().equals(projectSegment))
{
// We found an IResourceDescription for a file in the current project:
descriptions.add(desc);
}
}
return descriptions;
}
}
If I change the code above to use ResourceDescriptionsProvider.getResourceDescriptions(Resource) instead, how should I choose the Resource parameter to either:
- Obtain all IResourceDescriptions in the workspace and filter them as I do now or
- Obtain only the IResourceDescriptions for the given project so I do not have to filter the results?
|
|
|
Re: When to use createLiveScopeResourceDescriptions? [message #805320 is a reply to message #805235] |
Thu, 23 February 2012 16:26 |
Jan Koehnlein Messages: 760 Registered: July 2009 Location: Hamburg |
Senior Member |
|
|
You're mixing up two things here:
1) Obtaining the IResourceDescriptions for a given scope (builder,
dirty, live)
2) Getting the all IResourceDescription entries for a given project
Let's start with 1):
What is the purpose of the code? I ask because you usually are in the
context of a model, which allows you to access its resource set and us
it in the ResourceDescriptionsProvider to obtain the ResourceDescriptions.
If you're interested in the dirty state, it's okay to have the
IResourceDescriptions directly injected. If you want the persisted
information only, get an ResourceSet configured for your project using
the IResourceSetProvider, ste the load option NAMED_BUILDER_SCOPE and
use this resource set in the ResourceDescriptionsProvider to get the
IResourceDescriptions.
Not to step 2)
An IResourceDescriptions object contains IResourceDescription (without
's') entries for all indexed resources. You can filter the ones in a
given project by their URI. If you want to have descriptions from
dependent projects, too, you'll likely want to dig into the
documentation on IContainers in the section on scoping in the Xtext docs.
Am 23.02.12 15:29, schrieb Joey Mink:
> Jan,
>
> Thanks for the clarification. It seems I've been using
> ResourceDescriptionsProvider incorrectly, as I used it the following way
> to obtain all IResourceDescription instances for a given Eclipse project:
>
>
> public class TplFile {
> @Inject
> private ResourceDescriptionsProvider resourceDescriptionsProvider;
>
> public List<IResourceDescription> getResourceDescriptionsFor(IProject
> project)
> {
> List<IResourceDescription> descriptions = new
> ArrayList<IResourceDescription>();
>
> IResourceDescriptions xtextIndex =
> resourceDescriptionsProvider.createResourceDescriptions();
> for (IResourceDescription desc : xtextIndex.getAllResourceDescriptions())
> {
> org.eclipse.emf.common.util.URI descUri = desc.getURI();
> List<String> uriSegments = descUri.segmentsList();
> String projectSegment = uriSegments.get(1);
> if (project.getName().equals(projectSegment))
> {
> // We found an IResourceDescription for a file in the current project:
> descriptions.add(desc);
> }
> }
> return descriptions;
> }
> }
>
>
> If I change the code above to use
> ResourceDescriptionsProvider.getResourceDescriptions(Resource) instead,
> how should I choose the Resource parameter to either:
>
>
> Obtain all IResourceDescriptions in the workspace and filter them as I
> do now or
> Obtain only the IResourceDescriptions for the given project so I do not
> have to filter the results?
>
>
>
>
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com
---
Get professional support from the Xtext committers at www.typefox.io
|
|
|
Re: When to use createLiveScopeResourceDescriptions? [message #805416 is a reply to message #805320] |
Thu, 23 February 2012 18:44 |
|
Quote:What is the purpose of the code? I ask because you usually are in the
context of a model, which allows you to access its resource set and us
it in the ResourceDescriptionsProvider to obtain the ResourceDescriptions.
The purpose of the code in question is to give us programmatic access to an instance of our DSL. We used to have a custom-built parser and DOM in our Eclipse IDE. We used that DOM to create views (Common Navigators, etc.) based on the the user's DSL code. But now we've migrated to Xtext (gaining lots of functionality in the process), and are basically trying to restore the old views by backing them with the Xtext index instead of our custom parser and DOM. Our context is usually the active editor, from which we derive the current project.
Thanks for the rest of your reply - I just need to spend some time digesting it and trying things out
|
|
|
Powered by
FUDForum. Page generated in 0.05335 seconds