Open API

This page not only considers the language repository, project modules, languages, and models, which can be accessed through Open API but also other parts of MPS that can be accessed programmatically.

In general, there are interfaces such as the ones from org.jetbrains.mps.openapi that you can use to programmatically access classes. The implementing classes can be in the same or a different package. For example, jetbrains.mps.openapi.editor.EditorComponent ⧉ is the interface, jetbrains.mps.nodeEditor.EditorComponent ⧉ one of the implementing classes. Especially implementing IntelliJ classes often have the suffix Impl, for example, DataManagerImpl is an implementation of DataManager ⧉.

How do you get an icon for a concept?

GlobalIconManager.getInstance().getIconFor(concept)

How can you copy a language without the new one having duplicate model IDs?

{ =>
SModule sm = module/module1;
SModule tm = module/module2;

map<SNode, SNode> node = new hashmap<SNode, SNode>;

foreach aspect in LanguageAspect.values {
    SModel s = aspect.get((Language) sm);
    SModel t = aspect.get((Language) tm);
    if(s != null && t!= null) {
        foreach r in new arraylist<SNode>(copy: t.getRootNodes()) {
            ((node<>) r).detach;
        }
        foreach r in CopyUtil.copyAndPreserveId((((sequence<SNode>) s,getRootNodes())).toList, node) {
            t.addRootNode(r)
        }
    }
}
}.invoke()

How can you change the default project directory?

Put the following code in an application plugin:

1
2
3
// Set default project location 
string defaultProjectDir = Paths.get(System.getProperty("user.home"), "NewProjectDir").toString();
GeneralSettings.getInstance().setDefaultProjectDirectory(defaultProjectDir);

How can you execute an MPS action programmatically?

Use ActionUtils ⧉ to create an event and to run the action:

1
2
3
4
# cast to EditorContext class
DataContext dataContext = DataManager.getInstance().getDataContext(((EditorContext) editorContext).getNodeEditorComponent()); 
AnActionEvent event = ActionUtils.createEvent(ActionPlaces.EDITOR_TAB, dataContext); 
ActionUtils.updateAndPerformAction(action<openHtmlReport>, event);

Since this code is based on IDEA components, we need to get the getNodeEditorComponent, which is not part of the Open API. That's why we need to downcast it to the EditorComponent class.

Note: action<…> comes from the jetbrains.mps.lang.plugin language.

How to shut down MPS programmatically?

In normal conditions, one can use ApplicationManager.getApplication().exit(). If this doesn't work, throw an exception on purpose.

How can you react to the opening and closing of projects?

Register a ProjectManagerListener ⧉ in a plugin via the class ProjectManager ⧉.

How do you change a model without creating an undo entry?

Example: Setting a property (=flag) via a button.

repository.getModelAccess().executeUndoTransparentCommand()

How do you work with temporary models?

try { 
undo-transparent command with this.mpsProject.getRepository() {
tmpModel = TemporaryModels.getInstance().createReadOnly(TempModuleOptions.forDefaultModule());
tmpModel.addRootNode(type);
TemporaryModels.getInstance().addMissingImports(tmpModel);
}
// do something with the node
} finally {
    undo-transparent command with this.mpsProject.getRepository() {
        tmpModel.removeRootNode(type);
        TemporaryModels.getInstance().dispose(tmpModel);
    }
}

How can you react to selection changes in the editor?

editorContext.getSelectionManager().addSelectionListener(new SingularSelectionListenerAdapter() { ... })

How can I delete a model in a module programmatically?

Get the model-to-be-deleted as SModel (interface) and use new ModelDeleteHelper(model).delete(); For more context see: DeleteModeHelper ⧉

How can you programmatically add a language to the Used Languages settings of a model?

  • Module level: ((AbstractModule) moduleA).addDependency(moduleB.getModuleReference(), false)
  • Model level: new ModelImports(model).addUsedLanguage(language)

To get the language from a reference, you can call: MetaAdapterFactory.getLanguage(moduleRef).

Can a node pointer point to non-root nodes?

Yes, for example:

node-ptr/Integer->parseInt->radix/

How can you create an MPS Language programmatically and add it to the current project?

NewModuleUtil.createLanguage(namespace, rootPath, project, saveProject)

How do I save my project(s)?

Use SRepository.saveAll().

For a single project, for example, ProjectHelper.getProjectRepository(project).saveAll();

For all opened projects, e.g.

1
2
3
foreach project in com.intellij.openapi.project.ProjectManager.getInstance().getOpenProjects() { 
  jetbrains.mps.ide.project.ProjectHelper.getProjectRepository(project).saveAll(); 
}

contributed by: @AlexeiQ

How can you convert a quotation to an expression?

new QuotationConverter(quotation).convert()

Last update: July 13, 2023

Comments