How to assign new values to ActionContextParameters/DataKeys?

Question

Hello,

is it possible to assign new values to ActionContextParameters/DataKeys inside the execute(event) function of an action?

 

Example

1) I've created an action, that requires context parameters with the DataKeys NODE and EDITOR_COMPONENT 

2) If i want to access the values of these parameters inside the execute function i can do it with the this keyword or via the event parameter:

3) Now i was wondering how i can change the values of these parameters programmatically?  For instance, to execute an action inside the execute function with other ActionContextParameters.

My best attempt

I've tried it with the MPS.IDEA/com.intellij.ide.DataManager, because i found out, that this class manages the same DataContext as the event parameterThe attempt failed because the DataManager class uses Key<T> instead of DataKey<T> objects and i was not able to figure out how to resolve the proper Key<T> object.

Any suggestions?

 

Thank you in advance and best regards

2 comments
Comment actions Permalink
Official comment

Hello,

Looking at your example, I guess what you are really looking for is jetbrains.mps.nodeEditor.reflectiveEditor.ReflectiveHintsManager#makeNodeReflective and jetbrains.mps.nodeEditor.reflectiveEditor.ReflectiveHintsManager#makeSubtree. But you also have to handle editor update in case of using this methods.

If managing Reflective Editor programmatically is your intention, we can provide you with more info, how to do it, if needed.

 

For calling action from action with different data context - there is solution, but it is not recommended:

You have to create new AnActionEvent with your custom DataContext, that return new value for specific DataKey.

So it will be smth like this:

DataContext dataContext = new DataContext() {
@Nullable
@Override
public Object getData(String dataId) {
if (MPSCommonDataKeys.NODE.is(dataId)) {
return this.node.getParent();
}
return event.getDataContext().getData(dataId);
}
};
final AnActionEvent anActionEvent =
new AnActionEvent(event.getInputEvent(), dataContext, event.getPlace(), event.getPresentation(), event.getActionManager(),
event.getModifiers());

But this is kind of hack, because you changing context, which should not be changed.

Comment actions Permalink

Hi Victor,

thanks for your response.

 

Yes, i tried to push reflective editor hints by recursively calling the ShowReflecitveEditor_Action with altered context parameters. During this adventure i was wondering how to change context parameters of actions in general, but if this is considered a hack and is not recommended, I won't do it as well.

 

I can't use the ReflectiveHintsManager class, because i'm currently working with MPS 2017.2.

In my current solution i partially copied the implementation of the ShowReflectiveEditor_Action, iterated over all applicable descendant nodes, pushed the editor hints and updated the editor content.

 

EditorContext editorContext = this.editorComponent.getEditorContext(); 
node<> currentNode = this.node;
nlist<> allDescendentNodes = currentNode.descendants;
this.editorComponent.getUpdater().addExplicitEditorHintsForNode(this.node.getReference(), concept editor hint/reflectiveEditor/);

allDescendentNodes.where({~node =>
string[] hints = this.editorComponent.getUpdater().getExplicitEditorHintsForNode(((SNode) node).getReference());
return hints == null ? true : !Arrays.asList(hints).contains(concept editor hint/reflectiveEditor/);
}).forEach({~node => this.editorComponent.getUpdater().addExplicitEditorHintsForNode(((SNode) node).getReference(), concept editor hint/reflectiveEditor/); });

this.editorComponent.rebuildEditorContent();
editorContext.flushEvents();
editorContext.getSelectionManager().setSelection(this.node);
0

Please sign in to leave a comment.