-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deleting text that was just inserted merges those changes into a change that does nothing, causing an Exception #322
Comments
Thanks for reporting. It certainly has to do with undo/redo. The undo manager was probably undoing a change
so it set to expect its inverse (i.e. UndoManager should at least report these errors earlier. That's about all I can do without a reproducible test case. At least it should then be easier to produce one. |
You can try to switch temporarily to the fresh |
You can now also switch to 0.7-M2. Hopefully the reported error will now be more useful. |
Thanks. I haven't had much time to work on this but i'll try and see if i can reproduce it with that version. |
It seems i'm now able reproduce this easily.
here's a small extract of my class to show you how I handle the comments public class DeckEditor extends CodeArea {
private static final Pattern COMMENT_PATTERN = Pattern.compile("^ *#(?!#) ?([^\n]*)");
// ... more code
public DeckEditor() {
super();
init();
}
private final void init() {
setParagraphGraphicFactory(LineNumberFactory.get(this));
// ... more code
// Add shortcuts when the scene is set.
sceneProperty().addListener((obs, old, scene) -> {
if (scene != null) {
// CTRL + Q > (un)comment line(s)
scene.getAccelerators().put(new KeyCodeCombination(KeyCode.Q, KeyCodeCombination.CONTROL_DOWN), () -> {
IndexRange range = getSelection();
IndexRange linesRange = getSelectionLines(range);
String selectedText = getText(linesRange.getStart(), linesRange.getEnd());
int originalSize = selectedText.length();
if (originalSize != 0) {
try (BufferedReader reader = new BufferedReader(new StringReader(selectedText))) {
AtomicReference<Integer> firstLineDiffSize = new AtomicReference<>();
String replacement = reader.lines().map(l -> {
String newline;
if (l.trim().isEmpty()) {
newline = "";
}
else {
Matcher m = COMMENT_PATTERN.matcher(l);
if (m.matches()) {
newline = m.group(1);
}
else if (l.trim().startsWith("#")) {
newline = l;
}
else {
newline = "# " + l;
}
}
int diff = newline.length() - l.length();
firstLineDiffSize.compareAndSet(null, diff);
return newline;
}).collect(LINE_JOINER);
int diffSize = replacement.length() - originalSize;
replaceText(linesRange.getStart(), linesRange.getEnd(), replacement);
selectRange(
Math.max(range.getStart() + firstLineDiffSize.get(), linesRange.getStart()),
range.getEnd() + diffSize);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
// CTRL + D > duplicate line(s)
scene.getAccelerators().put(new KeyCodeCombination(KeyCode.D, KeyCodeCombination.CONTROL_DOWN), () -> {
IndexRange range = getSelection();
IndexRange linesRange = getSelectionLines(range);
String selectedText = getText(linesRange.getStart(), linesRange.getEnd());
insertText(linesRange.getEnd(), "\n" + selectedText);
selectRange(range.getStart(), range.getEnd());
});
}
});
}
private IndexRange getSelectionLines(IndexRange range) {
int firstLineStart = getText().lastIndexOf("\n", range.getStart() - 1) + 1;
int lastLineEnd = getText().indexOf("\n", range.getEnd());
if (lastLineEnd == -1) {
lastLineEnd = getLength();
}
return IndexRange.normalize(firstLineStart, lastLineEnd);
}
} |
Thanks, I found the problem: the two successive changes, comment and uncomment, get merged by the UndoManager into one, which in effect is a no-op. (Note that by default, successive changes are merged if they overlap or are adjacent.) When you then undo the change that is effectively a no-op, no change event is produced, but the UndoManager is still expecting one. This needs to be fixed in RichTextFX, but the fastest way of fixing it in your code is to call area.getUndoManager().preventMerge(); before and after each comment/uncomment operation, to ensure these changes don't get merged with any previous/subsequent changes. This is the behavior you probably want anyway. |
Thanks @TomasMikula That seems to do the trick! |
I'm having an other IllegalStateException. Seems to happens sometimes when i use Undo but couldn't yet find a pattern:
|
I can easily reproduce this in the latest richtextfx demo by adding a character then removing it and then hit Ctrl+Z. Or by adding several characters then removing those characters and then hit Ctrl+Z. |
Closed by #458. |
Hi,
Since 0.7-M1 I'm getting alot of those:
I haven't been able to reproduce this as I haven't found a pattern. Once this error occurs, nothing really works anymore in the editor and i have to exit the app and reopen it.
It seems to be tied to
replaceText
(I use shortcuts to comment text) and theundo
/redos
but i can't find a fixed pattern.I think this may be connected to #216 #247 and #251
Thanks
The text was updated successfully, but these errors were encountered: