@@ -214,7 +214,10 @@ public static <PS, SEG, S> UndoManager<List<PlainTextChange>> plainTextUndoManag
214214 * by {@code area.replaceText(change.getPosition(), change.getRemovalEnd(), change.getInserted()}.
215215 */
216216 public static <PS , SEG , S > Consumer <PlainTextChange > applyPlainTextChange (GenericStyledArea <PS , SEG , S > area ) {
217- return change -> area .replaceText (change .getPosition (), change .getRemovalEnd (), change .getInserted ());
217+ return change -> {
218+ area .replaceText (change .getPosition (), change .getRemovalEnd (), change .getInserted ());
219+ moveToChange ( area , change );
220+ };
218221 }
219222
220223 /**
@@ -223,7 +226,10 @@ public static <PS, SEG, S> Consumer<PlainTextChange> applyPlainTextChange(Generi
223226 */
224227 public static <PS , SEG , S > Consumer <RichTextChange <PS , SEG , S >> applyRichTextChange (
225228 GenericStyledArea <PS , SEG , S > area ) {
226- return change -> area .replace (change .getPosition (), change .getRemovalEnd (), change .getInserted ());
229+ return change -> {
230+ area .replace (change .getPosition (), change .getRemovalEnd (), change .getInserted ());
231+ moveToChange ( area , change );
232+ };
227233 }
228234
229235 /**
@@ -238,6 +244,7 @@ public static <PS, SEG, S> Consumer<List<PlainTextChange>> applyMultiPlainTextCh
238244 builder .replaceTextAbsolutely (c .getPosition (), c .getRemovalEnd (), c .getInserted ());
239245 }
240246 builder .commit ();
247+ moveToChange ( area , changeList .get ( changeList .size ()-1 ) );
241248 };
242249 }
243250
@@ -253,7 +260,19 @@ public static <PS, SEG, S> Consumer<List<RichTextChange<PS, SEG, S>>> applyMulti
253260 builder .replaceAbsolutely (c .getPosition (), c .getRemovalEnd (), c .getInserted ());
254261 }
255262 builder .commit ();
263+ moveToChange ( area , changeList .get ( changeList .size ()-1 ) );
256264 };
257265 }
258266
267+ /*
268+ * Address #912 "After undo/redo, new text is inserted at the end".
269+ * Without breaking PositionTests. (org.fxmisc.richtext.api.caret)
270+ */
271+ private static void moveToChange ( GenericStyledArea area , TextChange chg ) {
272+ int pos = chg .getPosition (), len = chg .getNetLength ();
273+ if ( len > 0 ) {
274+ pos = Math .min ( pos + Math .abs (len ), area .getLength () );
275+ }
276+ area .moveTo ( pos );
277+ }
259278}
0 commit comments