mps-tips

Custom Error Cells

Sometimes it is desired to make a cell of your editor appear like a usual error as automatically generated by MPS, e.g. when a property is empty which is not allowed to be empty.

Usually it is enough to use the provided CellModel_Error aka [!<text>!] aka error. However, this currently only supports static text and is therefore not sufficient in all cases. For example if you want to customize the empty cell of a child or reference cell with text about what this cell is about (e.g. parameter name, etc).

To do this we can use a custom cell provider as described in Custom Cells with a EditorCell_Error:

new AbstractCellProvider(node) {
  private final node<MyConcept> theNode = node;

  @Override
  public EditorCell createEditorCell(EditorContext p1)  {
    new  EditorCell_Error(p1, this.getSNode(), this.theNode.getMyErrorString(), false);
  }
};

The final boolean parameter of the constructor is called editable. The javadoc says the following:

@param editable - there are two different kinds of CEll_Error in MPS:
 - one (!editable) intended to show error text and then substitute it completely then user type
   something e.g. list<|<no type>>
 - another (editable) allows editing error text directly without replacing it with first types character
   e.g. myVariable.|field - in case "field" is not resolved, it should be highlighted as error, but should
   be still completely editable

So if false, the cell will completely be replaced by the first character the user types, if true the content may be edited. See also Custom Empty Cells for more information on this behavior.

Even though the cell is an error cell and also sets the internal error state to true, this does not cause an error during model check. Therefore, it is necessary to also check the appropriate condition in a checking rule.

The result can look like this (the <no type> is a usual error cell, but the parameter placeholders are custom):

../media/screenshot_error_cell.png