Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use old approach to update selection's 2D positions #581

Merged
merged 2 commits into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.fxmisc.richtext.api;

import com.nitorcreations.junit.runners.NestedRunner;
import javafx.stage.Stage;
import org.fxmisc.richtext.InlineCssTextAreaAppTest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import static javafx.scene.input.KeyCode.DELETE;

@RunWith(NestedRunner.class)
public class MiscellaneousTests {

public class WhenAreaEndsWithEmptyLine extends InlineCssTextAreaAppTest {

@Override
public void start(Stage stage) throws Exception {
super.start(stage);
area.replaceText(0, 0, "abc\n");
}

public class AndAllTextIsSelected {

@Before
public void selectAllText() {
interact(() -> area.selectAll());
}


@Test
public void pressingDeleteShouldNotThrowException() {
push(DELETE);
}

}
}
}
62 changes: 28 additions & 34 deletions richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class SelectionImpl<PS, SEG, S> implements Selection<PS, SEG, S> {
@Override public final int getLength() { return length.getValue(); }
@Override public final ObservableValue<Integer> lengthProperty() { return length; }

private final SuspendableVal<Integer> paragraphSpan;
private final Val<Integer> paragraphSpan;
@Override public final int getParagraphSpan() { return paragraphSpan.getValue(); }
@Override public final ObservableValue<Integer> paragraphSpanProperty() { return paragraphSpan; }

Expand All @@ -63,11 +63,11 @@ final class SelectionImpl<PS, SEG, S> implements Selection<PS, SEG, S> {
@Override public final int getStartPosition() { return startPosition.getValue(); }
@Override public final ObservableValue<Integer> startPositionProperty() { return startPosition; }

private final SuspendableVal<Integer> startParagraphIndex;
private final Val<Integer> startParagraphIndex;
@Override public final int getStartParagraphIndex() { return startParagraphIndex.getValue(); }
@Override public final ObservableValue<Integer> startParagraphIndexProperty() { return startParagraphIndex; }

private final SuspendableVal<Integer> startColumnPosition;
private final Val<Integer> startColumnPosition;
@Override public final int getStartColumnPosition() { return startColumnPosition.getValue(); }
@Override public final ObservableValue<Integer> startColumnPositionProperty() { return startColumnPosition; }

Expand All @@ -76,11 +76,11 @@ final class SelectionImpl<PS, SEG, S> implements Selection<PS, SEG, S> {
@Override public final int getEndPosition() { return endPosition.getValue(); }
@Override public final ObservableValue<Integer> endPositionProperty() { return endPosition; }

private final SuspendableVal<Integer> endPararagraphIndex;
@Override public final int getEndParagraphIndex() { return endPararagraphIndex.getValue(); }
@Override public final ObservableValue<Integer> endParagraphIndexProperty() { return endPararagraphIndex; }
private final Val<Integer> endParagraphIndex;
@Override public final int getEndParagraphIndex() { return endParagraphIndex.getValue(); }
@Override public final ObservableValue<Integer> endParagraphIndexProperty() { return endParagraphIndex; }

private final SuspendableVal<Integer> endColumnPosition;
private final Val<Integer> endColumnPosition;
@Override public final int getEndColumnPosition() { return endColumnPosition.getValue(); }
@Override public final ObservableValue<Integer> endColumnPositionProperty() { return endColumnPosition; }

Expand All @@ -98,6 +98,9 @@ final class SelectionImpl<PS, SEG, S> implements Selection<PS, SEG, S> {
private final Var<IndexRange> internalRange;
private final EventStream<?> dirty;

private final Var<Position> start2DPosition;
private final Val<Position> end2DPosition;

private Subscription subscription = () -> {};

public SelectionImpl(GenericStyledArea<PS, SEG, S> area) {
Expand Down Expand Up @@ -126,32 +129,30 @@ public SelectionImpl(GenericStyledArea<PS, SEG, S> area, SuspendableNo dependent
selectedDocument = documentVal.suspendable();
selectedText = documentVal.map(StyledDocument::getText).suspendable();

Val<Tuple2<Position, Position>> positions = internalRange.map(sel -> {
Position start2D = area.offsetToPosition(sel.getStart(), Forward);
Position end2D = sel.getLength() == 0
? start2D
: start2D.offsetBy(sel.getLength(), Backward);
return Tuples.t(start2D, end2D);
start2DPosition = Var.newSimpleVar(position(0, 0));
end2DPosition = start2DPosition.map(startPos2D ->
getLength() == 0
? startPos2D
: startPos2D.offsetBy(getLength(), Backward)
);

internalRange.addListener(obs -> {
IndexRange sel = internalRange.getValue();
start2DPosition.setValue(area.offsetToPosition(sel.getStart(), Forward));
});

startPosition = internalRange.map(IndexRange::getStart).suspendable();

Val<Position> start2D = positions.map(Tuple2::get1);
Val<Integer> startPar = start2D.map(Position::getMajor);
startParagraphIndex = startPar.suspendable();
startColumnPosition = start2D.map(Position::getMinor).suspendable();
startParagraphIndex = start2DPosition.map(Position::getMajor);
startColumnPosition = start2DPosition.map(Position::getMinor);

endPosition = internalRange.map(IndexRange::getEnd).suspendable();
endParagraphIndex = end2DPosition.map(Position::getMajor);
endColumnPosition = end2DPosition.map(Position::getMinor);

Val<Position> end2D = positions.map(Tuple2::get2);
Val<Integer> endPar = end2D.map(Position::getMajor);
endPararagraphIndex = endPar.suspendable();
endColumnPosition = end2D.map(Position::getMinor).suspendable();

paragraphSpan = Val.create(
() -> getEndParagraphIndex() - getStartParagraphIndex() + 1,
startPar, endPar
).suspendable();
paragraphSpan = Val.combine(
startParagraphIndex, endParagraphIndex,
(startP, endP) -> endP - startP + 1
);

dirty = merge(
invalidationsOf(rangeProperty()),
Expand Down Expand Up @@ -205,14 +206,7 @@ public SelectionImpl(GenericStyledArea<PS, SEG, S> area, SuspendableNo dependent
// first, so it's released last
beingUpdated,

paragraphSpan,

endColumnPosition,
endPararagraphIndex,
endPosition,

startColumnPosition,
startParagraphIndex,
startPosition,

selectedText,
Expand Down