Concept functions that are used in the MPS editor aspect usually have two parameters, editorContext and node. The current editor context provides access to the editor component, the repository, and a whole host of other services, but not to the current project. Nevertheless, it is possible and I’ll show you two ways of obtaining the project.

Approach 1: Using the IDEA platform APIs

// Component below is java.awt.Component
Component editorComponent = (Component) editorContext.getEditorComponent();

MPSProject project = MPSDataKeys.MPS_PROJECT.getData(DataManager.getInstance().getDataContext(component));

How does this work?

We make use of the nice extensible mechanism that IDEA provides to access context-specific global data1 such as the current project, file, or editor. MPS plugs into this mechanism to provide access to its globals such as the current MPS project or the current node or model.

The mechanism works through an IDEA service called DataManager. When asked about a certain data key for a certain AWT (UI) component, the data manager will walk the UI hierarchy from this component towards the main application window and ask each UI component on the way to provide a value for that data key. As soon as a component provides a non-null value the search stops and the value is returned.

The MPS editor component is also an AWT (Swing) component and so we can use it as the starting point for the data manager query.


This solution will not work if the editor component is not part of a UI hierarchy, such as if it’s headless.

Approach 2: Using the repository of the editor context

// Project is jetbrains.mps.project.Project
// ProjectHelper is jetbrains.mps.ide.project.ProjectHelper
Project project = ProjectHelper.getProject(editorContext.getRepository());

This approach obtains the project from the current editor’s repository. I don’t quite recommend it on theoretical grounds since its success depends on what repository the editor was initialized with. While MPS seems to initialize its editors with a ProjectRepository that provides access to the project, it could in theory change to another kind of repository in the future. In practice however, using the editor repository works and I admit that it is conceptually simpler than the first approach.

  1. A term I just made up, I don’t know the official term for this functionality ↩︎