The Jetbrains MetaProgrammin-System (MPS)
I published the original version of this article in March 2012, using MPS 2.0.6. I have then updated in July 2018, using MPS 2018.1.5. It is interesting to see how MPS evolved in the meantime.
The MetaProgramming-System is a Language Workbench based on projectional editing and it is the most advanced publicly available tool of this kind.
If you have not yet took a look at it you should, really. It makes possible (and easy!) to evolve and mix programming languages and domain specific languages providing great tool support.
Have you ever dreamed to improve a little bit your favourite language (e.g., Java). Well, with MPS it is just some clicks away. To get familiar with it take a look at the basic tutorials at https://www.jetbrains.com/mps/learn/.
In this tutorial we will see how to add an action to the IDE.
How to write an action for MPS
We want to add an action that we can invoke from the “Tools” menu.
The first step is create a new Project (File -> New Project).
At this point in the good, old MPS 2.0 we would have add a plugin aspect to a language and put the action there. It looked like this:
However in MPS 2018.1 we do things differently: we create a solution and inside the solution we create a model named plugin. Yes, the name must be “plugin”
We then set the solution kind to be “Other”.
We now need to import two languages in our model:
Thanks to these languages we can create our StandalonePluginDescriptor
Now the rest of the process is the same we had to follow in MPS 2.0: basically we need to create a Group and then put the Action inside the Group.
Let’s start creating a Group:
Creating a Group in MPS 2.0
Creating a Group in MPS 2018.1
This group will contain our Action. Set its name and add an element list where the editor indicates an error. Under the modifications section specify that the editor should go under the Tools group.
A Group in MPS 2.0
A Group in MPS 2018.1
Then we should add an action.
Adding an Action in MPS 2.0
Adding an Action in MPS 2018.1
Set the name and the caption. To test that our action get invoked we will show a Message using JOptionPane. To do that we should add the stub to our language. Select our language and from the context menu select “Module properties”. In the common tab add under Accessory models the javax.swing stub. Or that was what we did in MPS 2.0
Now in MPS 2018.1 we do this instead:
We can now add the code to show a message.
Add the Action to the group.
Now build the language (in MPS 2.0) or the solution (in MPS 2018.1). In the Tools menu it is now present our action.
The menu in MPS 2.0
The menu in MPS 2018.1
If we invoke it a message appears.
The message in MPS 2.0
The message in MPS 2018.1
Ok, that was fun! It is nice to see how things progressed in the few years I have been playing with MPS. Good job Jetbrains!
Download the guide with 68 resources on Creating Programming Languages
Receive the guide to your inbox to read it on all your devices when you have time