Skip to content

Commit

Permalink
Add more flexibility to the way we find the ICompositeNode for an object
Browse files Browse the repository at this point in the history
Signed-off-by: Sebastian Zarnekow <sebastian.zarnekow@gmail.com>
  • Loading branch information
szarnekow committed Feb 4, 2024
1 parent a144255 commit 59784bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@
*******************************************************************************/
package org.eclipse.xtext.nodemodel;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.nodemodel.impl.NodeModelBuilder;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;

/**
* An EMF object might store the node model information differently from the default adapter
* based approach. If it implements {@link INodeReference}, it will be queried by
* {@link NodeModelUtils#getNode(org.eclipse.emf.ecore.EObject)}.
* {@link NodeModelUtils#getNode(org.eclipse.emf.ecore.EObject)}.
*
* @since 2.34
* Alternatively, the {@link EObject} might contain an adapter for the {@link ICompositeNode ICompositeNode.class}
* which also implements {@link INodeReference}. This is mostly meant as a compatiblity mode
* for existing EMF packages that cannot be altered in a way that the EObject itself fulfills this
* interface.
*
* @see NodeModelBuilder#associateWithSemanticElement(ICompositeNode, org.eclipse.emf.ecore.EObject)
* @see NodeModelUtils#getNode(org.eclipse.emf.ecore.EObject)
*
* @since 2.34
*/
public interface INodeReference {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Collections;
import java.util.List;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
Expand Down Expand Up @@ -111,7 +112,11 @@ public static ICompositeNode getNode(/* @Nullable */ EObject object) {
if (object instanceof INodeReference) {
return ((INodeReference) object).getNode();
}
return (ICompositeNode) EcoreUtil.getExistingAdapter(object, ICompositeNode.class);
Adapter adapter = EcoreUtil.getExistingAdapter(object, ICompositeNode.class);
if (adapter instanceof INodeReference) {
return ((INodeReference) adapter).getNode();
}
return (ICompositeNode) adapter;
}

/**
Expand Down

0 comments on commit 59784bc

Please sign in to comment.