Skip to content

Commit 8a78979

Browse files
author
Jarno Goossens
committed
Make use of findTokenEnd
1 parent afd1f0f commit 8a78979

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

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

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,9 @@ 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 = getSelectionWordEnd();
395-
int start = tokenizer.findTokenStart(editable, end);
394+
int cursorPosition = getSelectionEnd();
395+
int end = tokenizer.findTokenEnd(editable, cursorPosition);
396+
int start = tokenizer.findTokenStart(editable, cursorPosition);
396397
if (start < prefix.length()) {
397398
start = prefix.length();
398399
}
@@ -428,12 +429,19 @@ public void invalidate() {
428429
public boolean enoughToFilter() {
429430
Editable text = getText();
430431

431-
int end = getSelectionWordEnd();
432-
if (end < 0 || tokenizer == null) {
432+
if (tokenizer == null)
433+
{
433434
return false;
434435
}
435436

436-
int start = tokenizer.findTokenStart(text, end);
437+
int cursorPosition = getSelectionEnd();
438+
439+
if (cursorPosition < 0) {
440+
return false;
441+
}
442+
443+
int end = tokenizer.findTokenEnd(text, cursorPosition);
444+
int start = tokenizer.findTokenStart(text, cursorPosition);
437445
if (start < prefix.length()) {
438446
start = prefix.length();
439447
}
@@ -747,24 +755,6 @@ protected CharSequence convertSelectionToString(Object object) {
747755
}
748756
}
749757

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-
768758
private SpannableStringBuilder buildSpannableForText(CharSequence text) {
769759
//Add a sentinel , at the beginning so the user can remove an inner token and keep auto-completing
770760
//This is a hack to work around the fact that the tokenizer cannot directly detect spans
@@ -792,8 +782,9 @@ protected void replaceText(CharSequence text) {
792782
TokenImageSpan tokenSpan = buildSpanForObject(selectedObject);
793783

794784
Editable editable = getText();
795-
int end = getSelectionWordEnd();
796-
int start = tokenizer.findTokenStart(editable, end);
785+
int cursorPosition = getSelectionEnd();
786+
int end = tokenizer.findTokenEnd(editable, cursorPosition);
787+
int start = tokenizer.findTokenStart(editable, cursorPosition);
797788
if (start < prefix.length()) {
798789
start = prefix.length();
799790
}
@@ -1405,7 +1396,7 @@ public boolean canDeleteSelection(int beforeLength) {
14051396
// if beforeLength is 1, we either have no selection or the call is coming from OnKey Event.
14061397
// In these scenarios, getSelectionStart() will return the correct value.
14071398

1408-
int endSelection = getSelectionWordEnd();
1399+
int endSelection = getSelectionEnd();
14091400
int startSelection = beforeLength == 1 ? getSelectionStart() : endSelection - beforeLength;
14101401

14111402
Editable text = getText();

0 commit comments

Comments
 (0)