Skip to content

Commit

Permalink
Merge pull request #473 from JordanMartinez/fixCaretAdvanceIssue
Browse files Browse the repository at this point in the history
Fix regression: Update caret position after replace call
  • Loading branch information
JordanMartinez authored Mar 28, 2017
2 parents db3b8d8 + e625f02 commit c7efe0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,9 @@ public void replaceText(int start, int end, String text) {
@Override
public void replace(int start, int end, StyledDocument<PS, SEG, S> replacement) {
content.replace(start, end, replacement);

int newCaretPos = start + replacement.length();
selectRange(newCaretPos, newCaretPos);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ public Position offsetToPosition(int offset, Bias bias) {
@Override
public void replace(int start, int end, StyledDocument<PS, SEG, S> replacement) {
ensureValidRange(start, end);
if (replacement.length() == 0 && start == end) {
// ignore a replacement that doesn't do anything
return;
}
doc.replace(start, end, ReadOnlyStyledDocument.from(replacement)).exec(this::update);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public void setup() {
interact(() -> {
area.setDisable(true);
area.replaceText("When Area Is Disabled Test: Some text goes here");
area.moveTo(0);
});
}

Expand Down Expand Up @@ -136,6 +137,8 @@ public void draggingTheMouseDoesNotSelectText() {

@Test
public void releasingTheMouseAfterDragDoesNothing() {
assertEquals(0, area.getCaretPosition());

moveTo(firstLineOfArea())
.press(MouseButton.PRIMARY)
.dropBy(20, 0);
Expand All @@ -155,7 +158,10 @@ public class AndTextIsNotSelected extends InlineCssTextAreaAppTest {

@Before
public void setup() {
interact(() -> area.replaceText(firstParagraph));
interact(() -> {
area.replaceText(firstParagraph);
area.moveTo(0);
});
}

@Test
Expand Down Expand Up @@ -357,4 +363,23 @@ public void pressingMouseOnSelectionAndDraggingAndReleasingMovesSelectedTextToTh

}

public class KeyboardBehaviorTests extends InlineCssTextAreaAppTest {

@Test
public void typingALetterMovesTheCaretAfterThatInsertedLetter() {
interact(() -> {
area.moveTo(0);
area.clear();
});

String userInputtedText = "some user-inputted text";
clickOn(area).write(userInputtedText);

assertEquals(userInputtedText, area.getText());
assertEquals(userInputtedText.length(), area.getCaretPosition());
assertTrue(area.getSelectedText().isEmpty());
}

}

}

0 comments on commit c7efe0e

Please sign in to comment.