Skip to content

Commit afd1f0f

Browse files
author
Jarno Goossens
committed
Get the end of a word instead of cursor position
1 parent 7a3fb17 commit afd1f0f

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

library/src/main/java/com/tokenautocomplete/TokenCompleteTextView.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ protected String currentCompletionText() {
391391
if (hintVisible) return ""; //Can't have any text if the hint is visible
392392

393393
Editable editable = getText();
394-
int end = getSelectionEnd();
394+
int end = getSelectionWordEnd();
395395
int start = tokenizer.findTokenStart(editable, end);
396396
if (start < prefix.length()) {
397397
start = prefix.length();
@@ -428,7 +428,7 @@ public void invalidate() {
428428
public boolean enoughToFilter() {
429429
Editable text = getText();
430430

431-
int end = getSelectionEnd();
431+
int end = getSelectionWordEnd();
432432
if (end < 0 || tokenizer == null) {
433433
return false;
434434
}
@@ -747,6 +747,24 @@ protected CharSequence convertSelectionToString(Object object) {
747747
}
748748
}
749749

750+
/**
751+
* Get the position of the end of the word were the cursor is positioned. A word is defined as
752+
* a sequence of characters without the presence of a character in {@link #splitChar}.
753+
*
754+
* @return The position of the end of the word were the cursor is positioned.
755+
*/
756+
public int getSelectionWordEnd()
757+
{
758+
int end = getSelectionEnd();
759+
760+
//Increase end until the end is reached or a split char is detected
761+
while (end < length() && !isSplitChar(getText().charAt(end))) {
762+
end++;
763+
}
764+
765+
return end;
766+
}
767+
750768
private SpannableStringBuilder buildSpannableForText(CharSequence text) {
751769
//Add a sentinel , at the beginning so the user can remove an inner token and keep auto-completing
752770
//This is a hack to work around the fact that the tokenizer cannot directly detect spans
@@ -774,7 +792,7 @@ protected void replaceText(CharSequence text) {
774792
TokenImageSpan tokenSpan = buildSpanForObject(selectedObject);
775793

776794
Editable editable = getText();
777-
int end = getSelectionEnd();
795+
int end = getSelectionWordEnd();
778796
int start = tokenizer.findTokenStart(editable, end);
779797
if (start < prefix.length()) {
780798
start = prefix.length();
@@ -1387,7 +1405,7 @@ public boolean canDeleteSelection(int beforeLength) {
13871405
// if beforeLength is 1, we either have no selection or the call is coming from OnKey Event.
13881406
// In these scenarios, getSelectionStart() will return the correct value.
13891407

1390-
int endSelection = getSelectionEnd();
1408+
int endSelection = getSelectionWordEnd();
13911409
int startSelection = beforeLength == 1 ? getSelectionStart() : endSelection - beforeLength;
13921410

13931411
Editable text = getText();

0 commit comments

Comments
 (0)