Skip to content

Commit

Permalink
Add documentation about custom empty cells (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
neumantm authored Sep 13, 2023
1 parent 2736216 commit 2b1820c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ This repository contains some tips and tricks around JetBrains MPS, which I miss

- [Custom cells and Swing Components](./editor/customCells.md)
- [Custom Error Cells](./editor/errorCells.md)
- [Custom Empty Cells](./editor/customEmptyCells.md)
32 changes: 32 additions & 0 deletions editor/customEmptyCells.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Custom empty cell

In some cases it is desired to customize the empty cell of a reference or child cell but still retain the behavior of the cell to completely be replaced by the first character the user types.

To customize the empty cell of such a cell, usually the editor DSL provides a field labeled `/empty cell`. For some cell types this must first be enabled by setting `customize empty cell` in the Inspector to `true`.

However, when you just enter any constant here, the behavior of cell is not the same as the default empty cell.
To achieve the desired behavior of completely being replaced by the first character, use the `text*` aka `nullText` property of the constant cell instead of the `text` property (see bottom of Inspector).

When looking deeper into why and how this works, the actual behavior depends on whether the text is set via [`EditorCell_Label.setText(String)`][setText] or via [`EditorCell_Label.setDefaultText(String)`][setDefaultText] with any non-empty string being set via `setText` hiding the "default" or "null" text.
The `CellModel_Constant` basically just calls `setText` with the value of `text` and `setDefaultText` with the value of `text*` aka `nullText` (this is done in the generator).

[setText]: https://github.com/JetBrains/MPS/blob/7ed0ccb451197c7dc33d9395dbe0dfa74ca96786/editor/editor-runtime/source/jetbrains/mps/nodeEditor/cells/EditorCell_Label.java#L265
[setDefaultText]: https://github.com/JetBrains/MPS/blob/7ed0ccb451197c7dc33d9395dbe0dfa74ca96786/editor/editor-runtime/source/jetbrains/mps/nodeEditor/cells/EditorCell_Label.java#L271

### Error Cell

The error cell (both variants described in [Error Cells](./errorCells.md)) also uses this technique to achieve the same behavior.
However, it decides to which method to give its text based on its `myEditable` flag in its [`synchornizeViewWithModel` method][ErrorCellsynchronizeViewWithModel].

[ErrorCellsynchronizeViewWithModel]: https://github.com/JetBrains/MPS/blob/50a1d67b34f3510097c49cd7f6c590460ed25771/editor/editor-runtime/source/jetbrains/mps/nodeEditor/cells/EditorCell_Error.java#L65

## Setting styles to actually match default empty cell

To actually look exactly like the default empty cell, I've found that the following styles must be set:

```
EmptyCell {
font-style : bold
}
```
with `EmptyCell` from `BaseLanguageStyle` from `jetbrains.mps.baseLangauge.editor`
1 change: 1 addition & 0 deletions editor/errorCells.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ The [javadoc] says the following:
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](./customEmptyCells.md#error-cell) 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.
Expand Down

0 comments on commit 2b1820c

Please sign in to comment.