Deep dive into Xtext scoping
local and global scopes explained
Sebastian Holger
Who are these guys?
Scoping with Xtext
What is scoping?
What is scoping?
What is scoping?
Convention
What is scoping?
QualifiedName
What is scoping?
QualifiedName
grandfather
father
son1
son2
- grandfather.father.son1
- grandfather.father.son2
What is scoping?
Simple name
What is scoping?
What is scoping?
Same EMF Container
What is scoping?
What is scoping?
PrefixMatcher does it’s job
What is scoping?
What is scoping?
Not same container
Imports?
Imports?
org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider
Imports?
org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider
Imports?
org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider
Feels wrong
Implicit imports
Implicit imports
Implicit imports
Implicit imports
Not called for root object!
Implicit imports
Implicit imports
Implicit imports
So far easy…
So far easy…
But what is a scope?
So far easy…
But what is a scope?
It describes the reachable Elements…
So far easy…
But what is a scope?
…depending on the context…
It describes the reachable Elements…
So far easy…
But what is a scope?
…depending on the context…
…and maps name to EObject…
It describes the reachable Elements…
So far easy…
But what is a scope?
…depending on the context…
…and maps name to EObject…
…in a chain of scopes.
It describes the reachable Elements…
Simply spoken
Scope
EObjectDescription
EObjectDescription
…
EObjectDescription
QualifiedName URI UserData
A chain of responsibility
Scope
EObjectDescription
EObjectDescription
…
Scope
EObjectDescription
EObjectDescription
…
outer
Index
global
ResourceDescription
…
Simply spoken
A chain of responsibility
Scope
EObjectDescription
EObjectDescription
…
Scope
EObjectDescription
EObjectDescription
…
outer
Index
global
ResourceDescription
…
Simply spoken
Index
ResourceDescription
…
ResourceDescription URI
ExportedObjects
ReferencedObjects
ImportedNames
- List of EObjectDescriptions
- List of ReferenceDesciptions
- All the names
Simply spoken
Local vs. global scoping
Local
Local vs. global scoping
Local Global
Other files in project and more…
Who creates Scopes?
Local Scopes Global Scope
aka. the Index
Who creates Scopes?
Local Scopes Global Scope
aka. the Index
YOU! The builder!
Override getScope!
Don’t use the declarative stuff any
more!
Simple Example
Simple Example
Simple Example
Collect all fields
Collect all fields
Collect all fields
More logic
More logic
Walkthrough
Walkthrough
Field
More logic
Walkthrough
Walkthrough
Field
More logic
Walkthrough
Walkthrough
Field
Field
More logic
More logic
More logic
More logic
context.getField cannot be called!
CyclicLinkingException
Objects can have different names in
different contexts
Function to transform the name
The implementation creates new EObjectDescriptions
Objects can have different names in
different contexts
org.eclipse.xtext.resource.impl.AliasedEObjectDescription
org.eclipse.xtext.scoping.impl.ImportScope
Don’t create too tight scopes
Don’t create too tight scopes
Use validations instead!
They can produce more meaningful
messages then!
The NodeModel
EObject CompositeNode
Adapter
LeafNode LeafNode CompositeNode
LeafNode LeafNode
EObject CompositeNode
Adapter
LeafNode LeafNode CompositeNode
LeafNode LeafNode
The NodeModel
Don’trelyontheNodeModelduring
scoping
Never load resources explicitly
There is a global scope
Global Scoping
Global Scoping
ResourceDescriptions
Constructed when the builder runs…
Global Scoping
ResourceDescriptions
Constructed when the builder runs…
org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider
…accessible through…
IResourceDescriptions - getResourceDescriptions(ResourceSet)
Global Scoping
ResourceDescriptions
Constructed when the builder runs…
org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider
…accessible through…
IResourceDescriptions - getResourceDescriptions(ResourceSet)
…depending on the LoadOptions…
LIVE_SCOPE NAMED_BUILDER_SCOPE
Information hiding
Not everything should be referable
Information hiding
Not everything should be referable
Save memory and time!
EObjectDescription(QualifiedName qualifiedName,
EObject element, Map<String, String> userData)
UserData
Serializable stuff
Informations without resolving the
proxy
Index in Standalone
ResourceSetGlobalScopeProvider
Long time the
was the default.
Index in Standalone
ResourceSetGlobalScopeProvider
Long time the
was the default.
Today we can do better but you need to use it
and know that there is something new…
For Maven- and Gradle it’s done.
Index in Standalone
Fill the index and install it on the ResourceSet!
Load each resource and ask the
ResourceDescriptionManager to create a
ResourceDescription…
Index in Standalone
Fill the index and install it on the ResourceSet!
Load each resource and ask the
ResourceDescriptionManager to create a
ResourceDescription…
Install it on the ResourceSet
Lessons learned
Never use #getResource
Avoid the Node Model
CyclicResolutionExceptions!
UserData helps
Never use #getResource!
Don’t Filter - But Validate!

Deep dive into Xtext scoping local and global scopes explained