Skip to content

Commit

Permalink
#1062 fix: Select word at cursor action selects the entire line if th…
Browse files Browse the repository at this point in the history
…e cursor is at the end of the line
  • Loading branch information
sds100 committed Jul 20, 2022
1 parent 0ac409e commit 35f6177
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,13 @@ class PerformActionsUseCaseImpl(
}

is ActionData.SelectWordAtCursor -> {
result = accessibilityService.performActionOnNode({ it.isFocused }) {
result = accessibilityService.performActionOnNode({ it.isFocused }) { node ->
//it is at the cursor position if they both return the same value
if (it.textSelectionStart == it.textSelectionEnd) {
val cursorPosition = it.textSelectionStart
if (node.textSelectionStart == node.textSelectionEnd) {
val cursorPosition = node.textSelectionStart

val wordBoundary =
it.text.toString().getWordBoundaries(cursorPosition)
node.text.toString().getWordBoundaries(cursorPosition)
?: return@performActionOnNode null

val extras = mapOf(
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/io/github/sds100/keymapper/util/StringUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ fun String.getWordBoundaries(@IntRange(from = 0L) cursorPosition: Int): Pair<Int
if (this.isBlank()) return null

//return null if there is just whitespace around the position

if (getOrNull(cursorPosition - 1)?.isWhitespace() == true && getOrNull(cursorPosition)?.isWhitespace() == true) {
return null
}
Expand All @@ -28,10 +29,16 @@ fun String.getWordBoundaries(@IntRange(from = 0L) cursorPosition: Int): Pair<Int
}
}

if (index == cursorPosition) {
/*
If the cursor is at the end of the line then it is outside the character index range so check for this case
check if we are at the end of the line.
*/
if (cursorPosition == this.length && index == this.lastIndex
|| index == cursorPosition
) {
firstBoundary = lastSpaceIndex?.plus(1)
}
}

return Pair(firstBoundary ?: 0, secondBoundary ?: lastIndex)
}

0 comments on commit 35f6177

Please sign in to comment.