Skip to content

Commit b108e26

Browse files
Merge pull request #759 from JordanMartinez/fixAllParIndexBug
Fix all par index bug
2 parents 5d64bd7 + 793def6 commit b108e26

4 files changed

Lines changed: 68 additions & 13 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.fxmisc.richtext;
2+
3+
import java.util.function.Function;
4+
5+
public class TextBuildingUtils {
6+
7+
private TextBuildingUtils() {
8+
throw new IllegalStateException("Cannot construct an instance of TextBuildingUtils");
9+
}
10+
11+
/** Builds {@code totalNumber} of lines that each have the index of the line as their text */
12+
public static String buildLines(int totalNumber) { return buildLines(totalNumber, String::valueOf); }
13+
14+
public static String buildLines(int totalNumber, Function<Integer, String> textOnEachLine) {
15+
StringBuilder sb = new StringBuilder();
16+
for (int i = 0; i < totalNumber - 1; i++) {
17+
sb.append(textOnEachLine.apply(i)).append("\n");
18+
}
19+
sb.append(textOnEachLine.apply(totalNumber));
20+
return sb.toString();
21+
}
22+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.fxmisc.richtext.api;
2+
3+
import javafx.stage.Stage;
4+
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
5+
import org.fxmisc.richtext.TextBuildingUtils;
6+
import org.junit.Test;
7+
8+
import java.util.Optional;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class ParagraphIndexMappingTests extends InlineCssTextAreaAppTest {
13+
14+
private static final int TOTAL_NUMBER_OF_LINES = 80;
15+
private static final int LAST_PAR_INDEX = TOTAL_NUMBER_OF_LINES - 1;
16+
private static final String CONTENT = TextBuildingUtils.buildLines(TOTAL_NUMBER_OF_LINES);
17+
18+
@Override
19+
public void start(Stage stage) throws Exception {
20+
super.start(stage);
21+
area.replaceText(CONTENT);
22+
}
23+
24+
@Test
25+
public void all_par_to_visible_par_index_is_correct() {
26+
interact(() -> area.showParagraphAtTop(0));
27+
assertEquals(Optional.of(0), area.allParToVisibleParIndex(0));
28+
29+
interact(() -> area.showParagraphAtBottom(LAST_PAR_INDEX));
30+
assertEquals(Optional.of(area.getVisibleParagraphs().size() - 1), area.allParToVisibleParIndex(LAST_PAR_INDEX));
31+
}
32+
33+
@Test
34+
public void visible_par_to_all_par_index_is_correct() {
35+
interact(() -> area.showParagraphAtTop(0));
36+
assertEquals(0, area.visibleParToAllParIndex(0));
37+
38+
interact(() -> area.showParagraphAtBottom(LAST_PAR_INDEX));
39+
assertEquals(LAST_PAR_INDEX, area.visibleParToAllParIndex(area.getVisibleParagraphs().size() - 1));
40+
41+
}
42+
}

richtextfx/src/integrationTest/java/org/fxmisc/richtext/keyboard/PageUpDownTests.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javafx.geometry.Bounds;
44
import javafx.stage.Stage;
55
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
6+
import org.fxmisc.richtext.TextBuildingUtils;
67
import org.junit.Test;
78

89
import static javafx.scene.input.KeyCode.PAGE_DOWN;
@@ -13,17 +14,7 @@
1314

1415
public class PageUpDownTests extends InlineCssTextAreaAppTest {
1516

16-
private static final String EIGHT_LINES;
17-
18-
static {
19-
StringBuilder sb = new StringBuilder();
20-
int totalLines = 8;
21-
for (int i = 0; i < totalLines - 1; i++) {
22-
sb.append(i).append("\n");
23-
}
24-
sb.append(totalLines);
25-
EIGHT_LINES = sb.toString();
26-
}
17+
private static final String EIGHT_LINES = TextBuildingUtils.buildLines(8);
2718

2819
@Override
2920
public void start(Stage stage) throws Exception {

richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,9 @@ public final double getViewportHeight() {
792792
@Override
793793
public final Optional<Integer> allParToVisibleParIndex(int allParIndex) {
794794
if (allParIndex < 0) {
795-
throw new IllegalArgumentException("Visible paragraph index cannot be negative but was " + allParIndex);
795+
throw new IllegalArgumentException("The given paragraph index (allParIndex) cannot be negative but was " + allParIndex);
796796
}
797-
if (allParIndex >= getVisibleParagraphs().size()) {
797+
if (allParIndex >= getParagraphs().size()) {
798798
throw new IllegalArgumentException(String.format(
799799
"Paragraphs' last index is [%s] but allParIndex was [%s]",
800800
getParagraphs().size() - 1, allParIndex)

0 commit comments

Comments
 (0)