Skip to content

Commit 4443749

Browse files
Merge pull request #419 from JordanMartinez/lineNotParagraphStartAndEnd
Update method & behavior: Line start/end/select, not Paragraph start/end/select
2 parents fd21496 + 1855384 commit 4443749

6 files changed

Lines changed: 79 additions & 12 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,36 @@ private void followCaret() {
10401040
virtualFlow.show(parIdx, region);
10411041
}
10421042

1043+
/**
1044+
* Move the caret to the start of either the line in a multi-line wrapped paragraph or the paragraph
1045+
* in a single-line / non-wrapped paragraph
1046+
*
1047+
* @param policy
1048+
*/
1049+
public void lineStart(SelectionPolicy policy) {
1050+
int columnPos = virtualFlow.getCell(getCurrentParagraph()).getNode().getCurrentLineStartPosition();
1051+
moveTo(getCurrentParagraph(), columnPos, policy);
1052+
}
1053+
1054+
/**
1055+
* Move the caret to the end of either the line in a multi-line wrapped paragraph or the paragraph
1056+
* in a single-line / non-wrapped paragraph
1057+
*
1058+
* @param policy
1059+
*/
1060+
public void lineEnd(SelectionPolicy policy) {
1061+
int columnPos = virtualFlow.getCell(getCurrentParagraph()).getNode().getCurrentLineEndPosition();
1062+
moveTo(getCurrentParagraph(), columnPos, policy);
1063+
}
1064+
1065+
/**
1066+
* Selects the current line.
1067+
*/
1068+
public void selectLine() {
1069+
lineStart(SelectionPolicy.CLEAR);
1070+
lineEnd(SelectionPolicy.ADJUST);
1071+
}
1072+
10431073
/**
10441074
* Moves caret to the previous page (i.e. page up)
10451075
* @param selectionPolicy use {@link SelectionPolicy#CLEAR} when no selection is desired and

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ public CaretOffsetX getCaretOffsetX() {
145145
return new CaretOffsetX(text.getCaretOffsetX());
146146
}
147147

148+
public int getCurrentLineStartPosition() {
149+
layout(); // ensure layout, is a no-op if not dirty
150+
return text.getCurrentLineStartPosition();
151+
}
152+
153+
public int getCurrentLineEndPosition() {
154+
layout(); // ensure layout, is a no-op if not dirty
155+
return text.getCurrentLineEndPosition();
156+
}
157+
148158
public int getLineCount() {
149159
layout(); // ensure layout, is a no-op if not dirty
150160
return text.getLineCount();

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ public Optional<Bounds> getSelectionBoundsOnScreen() {
161161
}
162162
}
163163

164+
public int getCurrentLineStartPosition() {
165+
return getLineStartPosition(clampedCaretPosition.getValue());
166+
}
167+
168+
public int getCurrentLineEndPosition() {
169+
return getLineEndPosition(clampedCaretPosition.getValue());
170+
}
171+
164172
public int currentLineIndex() {
165173
return getLineOfCharacter(clampedCaretPosition.getValue());
166174
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ class StyledTextAreaBehavior {
9797
// caret movement
9898
consume(anyOf(keyPressed(RIGHT), keyPressed(KP_RIGHT)), StyledTextAreaBehavior::right),
9999
consume(anyOf(keyPressed(LEFT), keyPressed(KP_LEFT)), StyledTextAreaBehavior::left),
100-
consume(keyPressed(HOME), (b, e) -> b.model.lineStart(SelectionPolicy.CLEAR)),
101-
consume(keyPressed(END), (b, e) -> b.model.lineEnd(SelectionPolicy.CLEAR)),
100+
consume(keyPressed(HOME), (b, e) -> b.view.lineStart(SelectionPolicy.CLEAR)),
101+
consume(keyPressed(END), (b, e) -> b.view.lineEnd(SelectionPolicy.CLEAR)),
102102
consume(
103103
anyOf(
104104
keyPressed(RIGHT, SHORTCUT_DOWN),
@@ -122,8 +122,8 @@ class StyledTextAreaBehavior {
122122
keyPressed(LEFT, SHIFT_DOWN),
123123
keyPressed(KP_LEFT, SHIFT_DOWN)
124124
), StyledTextAreaBehavior::selectLeft),
125-
consume(keyPressed(HOME, SHIFT_DOWN), (b, e) -> b.model.lineStart(selPolicy)),
126-
consume(keyPressed(END, SHIFT_DOWN), (b, e) -> b.model.lineEnd(selPolicy)),
125+
consume(keyPressed(HOME, SHIFT_DOWN), (b, e) -> b.view.lineStart(selPolicy)),
126+
consume(keyPressed(END, SHIFT_DOWN), (b, e) -> b.view.lineEnd(selPolicy)),
127127
consume(keyPressed(HOME, SHIFT_DOWN, SHORTCUT_DOWN), (b, e) -> b.model.start(selPolicy)),
128128
consume(keyPressed(END, SHIFT_DOWN, SHORTCUT_DOWN), (b, e) -> b.model.end(selPolicy)),
129129
consume(
@@ -407,7 +407,7 @@ private void mousePressed(MouseEvent e) {
407407
switch (e.getClickCount()) {
408408
case 1: firstLeftPress(hit); break;
409409
case 2: selectWord(); break;
410-
case 3: model.selectLine(); break;
410+
case 3: model.selectParagraph(); break;
411411
default: // do nothing
412412
}
413413
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ int getLineCount() {
5858
return getLines().length;
5959
}
6060

61+
int getLineStartPosition(int charIdx) {
62+
TextLine[] lines = getLines();
63+
TwoLevelNavigator navigator = new TwoLevelNavigator(
64+
() -> lines.length,
65+
i -> lines[i].getLength());
66+
int currentLineIndex = navigator.offsetToPosition(charIdx, Forward).getMajor();
67+
return navigator.position(currentLineIndex, 0).toOffset();
68+
}
69+
70+
int getLineEndPosition(int charIdx) {
71+
TextLine[] lines = getLines();
72+
TwoLevelNavigator navigator = new TwoLevelNavigator(
73+
() -> lines.length,
74+
i -> lines[i].getLength());
75+
int currentLineIndex = navigator.offsetToPosition(charIdx, Forward).getMajor();
76+
int minor = currentLineIndex == lines.length - 1 ? 0 : -1;
77+
return navigator.position(currentLineIndex + 1, minor).toOffset();
78+
}
79+
6180
int getLineOfCharacter(int charIdx) {
6281
TextLine[] lines = getLines();
6382
TwoLevelNavigator navigator = new TwoLevelNavigator(

richtextfx/src/main/java/org/fxmisc/richtext/model/NavigationActions.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,16 @@ default void wordBreaksForwards(int n, SelectionPolicy selectionPolicy) {
166166
}
167167

168168
/**
169-
* Moves the caret to the beginning of the current line.
169+
* Moves the caret to the beginning of the current paragraph.
170170
*/
171-
default void lineStart(SelectionPolicy selectionPolicy) {
171+
default void paragraphStart(SelectionPolicy selectionPolicy) {
172172
moveTo(getCaretPosition() - getCaretColumn(), selectionPolicy);
173173
}
174174

175175
/**
176-
* Moves the caret to the end of the current line.
176+
* Moves the caret to the end of the current paragraph.
177177
*/
178-
default void lineEnd(SelectionPolicy selectionPolicy) {
178+
default void paragraphEnd(SelectionPolicy selectionPolicy) {
179179
int lineLen = getText(getCurrentParagraph()).length();
180180
int newPos = getCaretPosition() - getCaretColumn() + lineLen;
181181
moveTo(newPos, selectionPolicy);
@@ -198,9 +198,9 @@ default void end(SelectionPolicy selectionPolicy) {
198198
/**
199199
* Selects the current line.
200200
*/
201-
default void selectLine() {
202-
lineStart(SelectionPolicy.CLEAR);
203-
lineEnd(SelectionPolicy.ADJUST);
201+
default void selectParagraph() {
202+
paragraphStart(SelectionPolicy.CLEAR);
203+
paragraphEnd(SelectionPolicy.ADJUST);
204204
}
205205

206206
/**

0 commit comments

Comments
 (0)