Skip to content

Commit

Permalink
Added getStyleRange to StyleSpans (#1232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jugen authored Jun 12, 2024
1 parent 9695091 commit 754bea2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class StyleSpan<S> {

private final S style;
private final int length;
private int startPos = 0;

/**
* Creates a style span. Note: length cannot be negative.
Expand All @@ -24,6 +25,12 @@ public StyleSpan(S style, int length) {
this.length = length;
}

StyleSpan(S style, int start, int length) {
this.style = style;
this.startPos = start;
this.length = length;
}

public S getStyle() {
return style;
}
Expand All @@ -32,6 +39,14 @@ public int getLength() {
return length;
}

void setStart( int start ) {
startPos = start;
}

int getStart() {
return startPos;
}

/**
* Two {@code StyleSpan}s are considered equal if they have equal length and
* equal style.
Expand All @@ -51,7 +66,7 @@ public boolean equals(Object other) {
public int hashCode() {
return Objects.hash(style, length);
}

@Override
public String toString() {
return String.format("StyleSpan[length=%s, style=%s]", length, style);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.fxmisc.richtext.model;

import static org.fxmisc.richtext.model.TwoDimensional.Bias.*;
import static org.fxmisc.richtext.model.TwoDimensional.Bias.Backward;
import static org.fxmisc.richtext.model.TwoDimensional.Bias.Forward;

import java.util.Iterator;
import java.util.Objects;
Expand All @@ -11,6 +12,8 @@
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import javafx.scene.control.IndexRange;

/**
* Essentially, a list of {@link StyleSpan} objects.
*
Expand All @@ -36,6 +39,12 @@ static <S> StyleSpans<S> singleton(StyleSpan<S> span) {
int getSpanCount();
StyleSpan<S> getStyleSpan(int index);

/**
* @param position is relative to start of style spans
* @return IndexRange relative to start of style spans
*/
IndexRange getStyleRange( int position );

/**
* Two {@code StyleSpans} objects are considered equal if they contain equal
* number of {@code StyleSpan}s and the {@code StyleSpan}s are pairwise
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.List;
import java.util.function.BiFunction;

import javafx.scene.control.IndexRange;

/**
* A one-time-use builder that Builds a memory efficient {@link StyleSpans} object.
*
Expand Down Expand Up @@ -169,8 +171,9 @@ private void _add(StyleSpan<S> span) {
} else {
StyleSpan<S> prev = spans.get(spans.size() - 1);
if(prev.getStyle().equals(span.getStyle())) {
spans.set(spans.size() - 1, new StyleSpan<>(span.getStyle(), prev.getLength() + span.getLength()));
spans.set(spans.size() - 1, new StyleSpan<>(span.getStyle(), prev.getStart(), prev.getLength() + span.getLength()));
} else {
span.setStart(prev.getStart() + prev.getLength());
spans.add(span);
}
}
Expand Down Expand Up @@ -202,6 +205,14 @@ public Position offsetToPosition(int offset, Bias bias) {
return navigator.offsetToPosition(offset, bias);
}

@Override
public IndexRange getStyleRange(int position) {
Position offset = offsetToPosition(position, Bias.Backward);
StyleSpan<S> span = getStyleSpan(offset.getMajor());
int spanStart = span.getStart();
return new IndexRange(spanStart, spanStart + span.getLength());
}

@Override
public boolean equals(Object other) {
if(other instanceof StyleSpans) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.Collection;
import java.util.Collections;

import javafx.scene.control.IndexRange;

import org.junit.Test;

public class ParagraphTest {
Expand Down Expand Up @@ -55,6 +57,7 @@ public void restylingEmptyParagraphViaEmptyStyleSpansWorks() {
@Override public Position position( int major, int minor ) { return null; }
@Override public Position offsetToPosition( int offset, Bias bias ) { return null; }
@Override public StyleSpan<Collection<String>> getStyleSpan( int index ) { return null; }
@Override public IndexRange getStyleRange(int position) { return null; }
@Override public int getSpanCount() { return 0; }
@Override public int length() { return 0; }
};
Expand Down

0 comments on commit 754bea2

Please sign in to comment.