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

Add smart delete functionality #28

Open
wants to merge 5 commits into
base: feat/0.3.0
Choose a base branch
from
Open
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
Expand Up @@ -78,6 +78,10 @@ object Token {
power, factorial, modulo, percent, sqrt,
)
}

val allWithoutBrackets by lazy {
all.filter { it !in listOf(leftBracket, rightBracket) }
}
}

object Func {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package app.myzel394.numberhub.core.ui.common.textfield

import androidx.compose.ui.text.TextRange
import app.myzel394.numberhub.core.base.Token

/** Smartly delete tokens
* @param value the current value of the text field
* @param selection the current selection of the text field - Assumed to be a valid selection
*/
data class SmartDeleteHandler(
private val value: String,
private val selection: TextRange,
) {
/**
* Calculate the range to delete based on the current selection.
*
* @return the range to delete - [Inclusive, Exclusive]
*/
fun calculateDeleteRange(): TextRange {
if (value == "") {
return TextRange(0, 0)
}

if (isSelectionARange()) {
return selection
}

val position = selection.start

when (position) {
0 -> return TextRange(0, 0)
1 -> return TextRange(0, 1)
}

val bracketPos = findPreviousBracket(position.coerceAtMost(value.length - 1) - 1)

if (bracketPos == null) {
return TextRange(0, position)
}

val isAtLeftEdge =
position - 1 == bracketPos && value[bracketPos] == Token.Operator.leftBracket[0]
val isAtRightEdge =
position - 1 == bracketPos && value[bracketPos] == Token.Operator.rightBracket[0]

if (!isAtLeftEdge && !isAtRightEdge) {
return TextRange(bracketPos + 1, position)
}

if (isAtRightEdge) {
val leftBracketPos = findClosingParenBackwards(bracketPos)

return if (leftBracketPos != null) {
TextRange(leftBracketPos + 1, position)
} else {
// Weird case, should not happen
TextRange(0, position + 1)
}
}

val rightBracketPos = findClosingParen(bracketPos)

if (rightBracketPos != null) {
return TextRange(bracketPos + 1, rightBracketPos)
}

// Find previous bracket and return range from there to cursor position
val previousBracketPos = findPreviousBracket(bracketPos - 1)?.let { it + 1 } ?: 0

return TextRange(previousBracketPos, position)
}

private fun takeNextIfIsOperator(position: Int): Int {
if (position + 1 < value.length && Token.Operator.allWithoutBrackets.contains(value[position].toString())) {
return position + 1
}

return position
}

private fun isSelectionARange(): Boolean = selection.start != selection.end

private fun findPreviousBracket(startPosition: Int): Int? {
for (index in startPosition.coerceAtMost(value.length - 1) downTo 0) {
if (value[index] == Token.Operator.rightBracket[0] || value[index] == Token.Operator.leftBracket[0]) {
return index
}
}

return null
}

private fun findLeftBracket(startPosition: Int): Int? {
for (index in startPosition.coerceAtMost(value.length - 1) downTo 0) {
if (value[index] == Token.Operator.leftBracket[0]) {
return index
}
}

return null
}

private fun findRightBracket(startPosition: Int): Int? {
for (index in startPosition.coerceAtMost(value.length - 1) downTo 0) {
if (value[index] == Token.Operator.rightBracket[0]) {
return index
}
}

return null
}

private fun isAtEdge(position: Int): Boolean {
if (position == 0) {
return false
}

val previousCharacter = value[position.coerceAtMost(value.length - 1) - 1]

return previousCharacter == Token.Operator.leftBracket[0] || previousCharacter == Token.Operator.rightBracket[0]
}

// Based of https://stackoverflow.com/a/12752226/9878135
fun findClosingParen(openPos: Int): Int? {
var closePos = openPos
var counter = 1

while (counter > 0) {
val nextPos = ++closePos

if (nextPos >= value.length) {
return null
}

val c = value[nextPos]
if (c == Token.Operator.leftBracket[0]) {
counter++
} else if (c == Token.Operator.rightBracket[0]) {
counter--
}
}

if (closePos == openPos) {
return null
}

return closePos
}

fun findClosingParenBackwards(openPos: Int): Int? {
var closePos = openPos
var counter = 1

while (counter > 0) {
val c = value[--closePos]
if (c == Token.Operator.leftBracket[0]) {
counter--
} else if (c == Token.Operator.rightBracket[0]) {
counter++
}
}

if (closePos == openPos) {
return null
}

return closePos
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ fun TextFieldValue.deleteTokens(): TextFieldValue {
)
}

fun TextFieldValue.smartDeleteTokens(): TextFieldValue {
val deleteRange = SmartDeleteHandler(text, selection).calculateDeleteRange()

return this.copy(
text = text.removeRange(deleteRange.start, deleteRange.end),
selection = TextRange(deleteRange.start),
)
}

fun TextFieldValue.placeCursorAtTheEnd(): TextFieldValue = copy(selection = TextRange(text.length))

/**
Expand Down
Loading
Loading