Skip to content

Commit

Permalink
Merge pull request #716 from JordanMartinez/fixStyleRange
Browse files Browse the repository at this point in the history
Fix `getStyleRangeAtPosition()` bug
  • Loading branch information
JordanMartinez authored Mar 29, 2018
2 parents e2e8db9 + d1285fa commit c23d566
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public S getStyleAtPosition(int position) {
* then the range preceding {@code position} is returned.
*/
public IndexRange getStyleRangeAtPosition(int position) {
Position pos = navigator.offsetToPosition(position, Backward);
Position pos = styles.offsetToPosition(position, Backward);
int start = position - pos.getMinor();
int end = start + styles.getStyleSpan(pos.getMajor()).getLength();
return new IndexRange(start, end);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.fxmisc.richtext.model;

import static org.junit.Assert.*;

import javafx.scene.control.IndexRange;
import org.junit.Test;

public class SimpleEditableStyledDocumentTest {
Expand Down Expand Up @@ -111,4 +113,24 @@ public void testSetNonEmptyParagraphStyle() {
document.setParagraphStyle(0, newParStyle);
assertEquals(newParStyle, document.getParagraphStyle(0));
}

@Test
public void testGetStyleRangeAtPosition() {
SimpleEditableStyledDocument<String, String> document = new SimpleEditableStyledDocument<>("", "");
String first = "some";
String second = " text";
replaceText(document, 0, 0, first + second);
document.setStyle(0, first.length(), "abc");

IndexRange range = document.getStyleRangeAtPosition(0);
IndexRange expected = new IndexRange(0, first.length());
assertEquals(expected, range);

range = document.getStyleRangeAtPosition(first.length());
assertEquals(expected, range);

range = document.getStyleRangeAtPosition(first.length() + 1);
expected = new IndexRange(first.length(), (first + second).length());
assertEquals(expected, range);
}
}

0 comments on commit c23d566

Please sign in to comment.