Skip to content

Commit ee4ccd1

Browse files
committed
Merge pull request #198 from dejarno/master
Fix for autocompleting with cursor in the middle
2 parents 7a3fb17 + 8a78979 commit ee4ccd1

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

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

Lines changed: 16 additions & 7 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 = getSelectionEnd();
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 = getSelectionEnd();
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
}
@@ -774,8 +782,9 @@ protected void replaceText(CharSequence text) {
774782
TokenImageSpan tokenSpan = buildSpanForObject(selectedObject);
775783

776784
Editable editable = getText();
777-
int end = getSelectionEnd();
778-
int start = tokenizer.findTokenStart(editable, end);
785+
int cursorPosition = getSelectionEnd();
786+
int end = tokenizer.findTokenEnd(editable, cursorPosition);
787+
int start = tokenizer.findTokenStart(editable, cursorPosition);
779788
if (start < prefix.length()) {
780789
start = prefix.length();
781790
}

0 commit comments

Comments
 (0)