From 754bea23f8d71b0487108f3277708e7945664083 Mon Sep 17 00:00:00 2001 From: Jurgen Date: Wed, 12 Jun 2024 09:12:37 +0200 Subject: [PATCH] Added getStyleRange to StyleSpans (#1232) --- .../org/fxmisc/richtext/model/StyleSpan.java | 17 ++++++++++++++++- .../org/fxmisc/richtext/model/StyleSpans.java | 11 ++++++++++- .../richtext/model/StyleSpansBuilder.java | 13 ++++++++++++- .../fxmisc/richtext/model/ParagraphTest.java | 3 +++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpan.java b/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpan.java index 38e4bdd68..8ccf7069b 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpan.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpan.java @@ -11,6 +11,7 @@ public class StyleSpan { private final S style; private final int length; + private int startPos = 0; /** * Creates a style span. Note: length cannot be negative. @@ -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; } @@ -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. @@ -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); diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpans.java b/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpans.java index ce7e4e345..2a132788b 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpans.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpans.java @@ -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; @@ -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. * @@ -36,6 +39,12 @@ static StyleSpans singleton(StyleSpan span) { int getSpanCount(); StyleSpan 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 diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpansBuilder.java b/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpansBuilder.java index 0b160f472..3d98137c1 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpansBuilder.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/model/StyleSpansBuilder.java @@ -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. * @@ -169,8 +171,9 @@ private void _add(StyleSpan span) { } else { StyleSpan 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); } } @@ -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 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) { diff --git a/richtextfx/src/test/java/org/fxmisc/richtext/model/ParagraphTest.java b/richtextfx/src/test/java/org/fxmisc/richtext/model/ParagraphTest.java index 530064963..7377435b6 100644 --- a/richtextfx/src/test/java/org/fxmisc/richtext/model/ParagraphTest.java +++ b/richtextfx/src/test/java/org/fxmisc/richtext/model/ParagraphTest.java @@ -5,6 +5,8 @@ import java.util.Collection; import java.util.Collections; +import javafx.scene.control.IndexRange; + import org.junit.Test; public class ParagraphTest { @@ -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> 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; } };