Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more flexibility to the way we find the ICompositeNode for an object #2927

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I don't understand the details, but wouldn't this be equivalent to

Adapter adapter = EcoreUtil.getExistingAdapter(object, ICompositeNode.class);
if (adapter instanceof INodeReference) {
	return ((INodeReference) adapter).getNode();
}
return (ICompositeNode) adapter;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to have the instanceof check for the ICompositeNode first since it's far more likely to be true. Doesn't matter in practice so I'll apply your suggestion.

return (ICompositeNode) adapter;
}

/**
Expand Down
Loading