Skip to content

Commit 2b1820c

Browse files
authored
Add documentation about custom empty cells (#6)
1 parent 2736216 commit 2b1820c

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ This repository contains some tips and tricks around JetBrains MPS, which I miss
1212

1313
- [Custom cells and Swing Components](./editor/customCells.md)
1414
- [Custom Error Cells](./editor/errorCells.md)
15+
- [Custom Empty Cells](./editor/customEmptyCells.md)

editor/customEmptyCells.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Custom empty cell
2+
3+
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.
4+
5+
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`.
6+
7+
However, when you just enter any constant here, the behavior of cell is not the same as the default empty cell.
8+
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).
9+
10+
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.
11+
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).
12+
13+
[setText]: https://github.com/JetBrains/MPS/blob/7ed0ccb451197c7dc33d9395dbe0dfa74ca96786/editor/editor-runtime/source/jetbrains/mps/nodeEditor/cells/EditorCell_Label.java#L265
14+
[setDefaultText]: https://github.com/JetBrains/MPS/blob/7ed0ccb451197c7dc33d9395dbe0dfa74ca96786/editor/editor-runtime/source/jetbrains/mps/nodeEditor/cells/EditorCell_Label.java#L271
15+
16+
### Error Cell
17+
18+
The error cell (both variants described in [Error Cells](./errorCells.md)) also uses this technique to achieve the same behavior.
19+
However, it decides to which method to give its text based on its `myEditable` flag in its [`synchornizeViewWithModel` method][ErrorCellsynchronizeViewWithModel].
20+
21+
[ErrorCellsynchronizeViewWithModel]: https://github.com/JetBrains/MPS/blob/50a1d67b34f3510097c49cd7f6c590460ed25771/editor/editor-runtime/source/jetbrains/mps/nodeEditor/cells/EditorCell_Error.java#L65
22+
23+
## Setting styles to actually match default empty cell
24+
25+
To actually look exactly like the default empty cell, I've found that the following styles must be set:
26+
27+
```
28+
EmptyCell {
29+
font-style : bold
30+
}
31+
```
32+
with `EmptyCell` from `BaseLanguageStyle` from `jetbrains.mps.baseLangauge.editor`

editor/errorCells.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The [javadoc] says the following:
3030
be still completely editable
3131
```
3232
So if `false`, the cell will completely be replaced by the first character the user types, if `true` the content may be edited.
33+
See also [Custom Empty Cells](./customEmptyCells.md#error-cell) for more information on this behavior.
3334

3435
Even though the cell is an error cell and also sets the internal error state to true,
3536
this does not cause an error during model check.

0 commit comments

Comments
 (0)