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

Store cursor position when changing lines #749

Merged
merged 2 commits into from
Apr 12, 2024
Merged
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
26 changes: 11 additions & 15 deletions framework/editor/InputTarget.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ internal class InputTarget constructor(
}
}

internal data class Cursor constructor(val row: Int, val col: Int) : Comparable<Cursor> {
internal data class Cursor constructor(val row: Int, val col: Int, val lastCol: Int = col) : Comparable<Cursor> {

companion object {
fun min(first: Cursor, second: Cursor): Cursor {
Expand Down Expand Up @@ -371,23 +371,19 @@ internal class InputTarget constructor(
}

internal fun moveCursorUpByLine(isSelecting: Boolean = false) {
var newRow = cursor.row - 1
var newCol = cursor.col
if (newRow < 0) {
newRow = 0
newCol = 0
} else newCol = newCol.coerceAtMost(content[newRow].length)
updateCursor(Cursor(newRow, newCol), isSelecting)
if (cursor.row != 0) {
val newRow = cursor.row - 1
val newCol = cursor.lastCol.coerceAtMost(content[newRow].length)
updateCursor(Cursor(newRow, newCol, lastCol = cursor.lastCol), isSelecting)
}
}

internal fun moveCursorDownByLine(isSelecting: Boolean = false) {
var newRow = cursor.row + 1
var newCol = cursor.col
if (newRow >= content.size) {
newRow = content.size - 1
newCol = content[newRow].length
} else newCol = newCol.coerceAtMost(content[newRow].length)
updateCursor(Cursor(newRow, newCol), isSelecting)
if (cursor.row != content.size - 1) {
val newRow = cursor.row + 1
val newCol = cursor.lastCol.coerceAtMost(content[newRow].length)
updateCursor(Cursor(newRow, newCol, lastCol = cursor.lastCol), isSelecting)
}
}

internal fun moveCursorUpByPage(isSelecting: Boolean = false) {
Expand Down