Skip to content

Commit

Permalink
feat(editor): add old cursor position before changed for selection ch…
Browse files Browse the repository at this point in the history
…ange event
  • Loading branch information
StarkZhidian authored and Rosemoe committed Feb 22, 2024
1 parent 36702a5 commit f5f64f6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package io.github.rosemoe.sora.event;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import io.github.rosemoe.sora.text.CharPosition;
import io.github.rosemoe.sora.widget.CodeEditor;
Expand Down Expand Up @@ -74,12 +75,18 @@ public class SelectionChangeEvent extends Event {
* From mouse
*/
public final static int CAUSE_MOUSE_INPUT = 8;
@Nullable
private final CharPosition oldLeft;
@Nullable
private final CharPosition oldRight;
private final CharPosition left;
private final CharPosition right;
private final int cause;

public SelectionChangeEvent(@NonNull CodeEditor editor, int cause) {
public SelectionChangeEvent(@NonNull CodeEditor editor, @Nullable CharPosition oldLeft, @Nullable CharPosition oldRight, int cause) {
super(editor);
this.oldLeft = oldLeft;
this.oldRight = oldRight;
var cursor = editor.getText().getCursor();
left = cursor.left();
right = cursor.right();
Expand All @@ -101,6 +108,22 @@ public int getCause() {
return cause;
}

/**
* Get the last left selection's position before changed
*/
@Nullable
public CharPosition getOldLeft() {
return oldLeft;
}

/**
* Get the last right selection's position before changed
*/
@Nullable
public CharPosition getOldRight() {
return oldRight;
}

/**
* Get the left selection's position
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ public class CodeEditor extends View implements ContentListener, Formatter.Forma
private boolean horizontalAbsorb;
private LineSeparator lineSeparator;
private TextRange lastInsertion;
private TextRange lastSelectedTextRange;
private SnippetController snippetController;

public CodeEditor(Context context) {
Expand Down Expand Up @@ -3209,6 +3210,7 @@ protected void ensureSelectionAnchorAvailable() {

/**
* Move or extend selection, according to {@code extend} param.
*
* @param extend True if you want to extend selection.
*/
public void moveOrExtendSelection(@NonNull SelectionMovement movement, boolean extend) {
Expand Down Expand Up @@ -4376,7 +4378,15 @@ protected EditorRenderer onCreateRenderer() {
* Called when the text is edited or {@link CodeEditor#setSelection} is called
*/
protected void onSelectionChanged(int cause) {
dispatchEvent(new SelectionChangeEvent(this, cause));
CharPosition oldLeft = null;
CharPosition oldRight = null;
final TextRange lastTextRange = this.lastSelectedTextRange;
if (lastTextRange != null) {
oldLeft = lastTextRange.getStart();
oldRight = lastTextRange.getEnd();
}
dispatchEvent(new SelectionChangeEvent(this, oldLeft, oldRight, cause));
this.lastSelectedTextRange = getCursorRange();
}

/**
Expand Down

0 comments on commit f5f64f6

Please # to comment.