Intentions

Intentions are a very useful mechanism providing fast access to the most used operations with syntactical constructions, such as "negate boolean," "invert if condition," etc. If you've ever used IntelliJ IDEA's intentions, you will find MPS intentions very familiar, because they are really the same thing.

Using intentions

Like in IDEA, if you have some avaliable intentions at the current position in your code, a light bulb is shown. To view the list of avaliable intentions, press Alt+Enter or click the light bulb. To apply an intention, either click it or select it and press Enter.

Intentions can be disabled/enabled (press "D" or right-click the intention in the list). "Disabled" means that you will be no longer notified if this intention is applicable. However, a disabled intention will be visible in the popup menu and thus can be used or re-enabled. If there are only disabled intentions for the current place in the code, the light bulb will not be shown, but you can still invoke the intentions list with Alt+Enter.


Example: list of applicable intentions

Where to store my intentions?

You can create intentions in any model by importing the intentions language. However, MPS collects intentions only from Intentions language aspects. If you want your intentions to be used by the MPS intentions subsystem, it must be stored in the Intentions aspect of your language.

Intention structure

name

The name of an intention. You can choose any name you like, the only obvious constraint being that names must be unique in scope of the model. 

for concept

Intention will be tested for applicability only to nodes that are instances of this concept and its subconcepts.

is error intention

This flag is responsible for an intention's presentation. It distinguishes two types of intentions - "error" intentions which correct some errors (e.g. a missing 'cast') and "regular" intentions which only perform some code transformation. Error intentions are shown with a red bulb, instead of an orange one, and are placed above regular intentions in the applicable intentions list.

available in child nodes

Suppose N is a node for which the intention can be applied. If this flag is set to false, intention will be visible only when the cursor is over the node N itself, otherwise it will be also visible in N's descendants (but still will be applied to N)

description

The value returned by this function is what you will see in the list of intentions.

isApplicable

Intentions that have passed the "for concept" test are tested for applicability to the current node. If this method returns "true," the intention is shown in the list and can be applied. Otherwise the intention is not shown in the list. The node argument of this method is guaranteed to be an instance of the concept specified in "for concept" or one of its subconcepts.

execute

This method performs a code transformation. It is guaranteed that node parameter has passed the "for concept" and "is applicable" tests.

Examples

You can find some intentions examples in jetbrains.mps.baseLanguage.intentions.

You can also see all intentions by going to the IntentionDeclaration concept (press Ctrl+N, type "IntentionDeclaration", press ENTER) and finding all instances of this concept (pres Alt+F7, check instances, check Global Scope).

Previous Next

Labels

 
(None)