My application is running some commands on ctrl+space. When the user presses Ctrl+space, it begins to search if the last word typed at the caret position is a command and replaces it with a template. My problem is that the undomanager is saving the commands. eg. "pk" is replaced by "primary key" and "pk" shows up again on ctrl+z / ctrl+y. How do i force the undomanager to ignore this replaceText(...).
You'll need to create your own custom EventStream and then rebuild the default UndoManager using methods in the UndoUtils class. Something like this should work:
```java
EventStream<PlainTextChange> regularStream = area.plainTextChanges();
SuspendableYes allowChange = new SuspendableYes();
EventStream<PlainTextChange> stream = regularStream.conditionOn(allowChange);
// Recreate UndoUtils#defaultUndoManager
// but pass instream` as the EventStream
UndoManager> um = // creation code
// when want to do CTRL+Space in some other part of your code
if (userPressedCtrlSpace) {
allowChange.suspendWhile(() -> {
area.replaceText();
// area will emit a change but that change
// will be ignored because allowChange is now 'false' during its suspension
}
}
````
I'm assuming this answers your question. Reopen if it's still unanswered.
For those who are wondering about the magic "Recreate UndoUtils#defaultUndoManager" line in the answer above, use the code below to create/set the new manager:
area.setUndoManager(UndoManagerFactory.unlimitedHistoryFactory().createMultiChangeUM(area.multiRichChanges().conditionOn(allowChange), TextChange::invert, UndoUtils.applyMultiRichTextChange(area), TextChange::mergeWith, TextChange::isIdentity));
Most helpful comment
For those who are wondering about the magic "Recreate UndoUtils#defaultUndoManager" line in the answer above, use the code below to create/set the new manager: