Skip to content

Commit

Permalink
Fix completing on hint when adding a token with no text
Browse files Browse the repository at this point in the history
  • Loading branch information
mgod committed Mar 2, 2016
1 parent 2df134a commit 17c50a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,12 @@
public class CharacterTokenizer implements MultiAutoCompleteTextView.Tokenizer {
ArrayList<Character> splitChar;

@SuppressWarnings("unused")
CharacterTokenizer(){
super();
this.splitChar = new ArrayList<>(1);
this.splitChar.add(',');
}

CharacterTokenizer(char[] splitChar){
super();
this.splitChar = new ArrayList<>(splitChar.length);
for(char c : splitChar) this.splitChar.add(c);
}

@SuppressWarnings("unused")
CharacterTokenizer(char splitChar){
super();
this.splitChar = new ArrayList<>(1);
this.splitChar.add(splitChar);
}

public int findTokenStart(CharSequence text, int cursor) {
int i = cursor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,13 @@ public List<T> getObjects() {
* @param splitChar char[] with a characters that trigger the token creation
*/
public void setSplitChar(char[] splitChar) {
if (splitChar[0] == ' ' && splitChar.length == 1) {
splitChar = new char[]{'§', splitChar[0]};
char[] fixed = splitChar;
if (splitChar[0] == ' ') {
fixed = new char[splitChar.length + 1];
fixed[0] = '§';
System.arraycopy(splitChar, 0, fixed, 1, splitChar.length);
}
this.splitChar = splitChar;
this.splitChar = fixed;
// Keep the tokenizer and splitchars in sync
this.setTokenizer(new CharacterTokenizer(splitChar));
}
Expand All @@ -315,8 +318,7 @@ public void setSplitChar(char[] splitChar) {
*/
@SuppressWarnings("unused")
public void setSplitChar(char splitChar) {
if (splitChar == ' ') this.setSplitChar(new char[]{'§', splitChar});
else this.setSplitChar(new char[]{splitChar});
setSplitChar(new char[]{splitChar});
}

/**
Expand Down Expand Up @@ -393,16 +395,28 @@ public void setTokenLimit(int tokenLimit) {
*/
abstract protected T defaultObject(String completionText);

protected String currentCompletionText() {
if (hintVisible) return ""; //Can't have any text if the hint is visible

private int getCorrectedTokenEnd() {
Editable editable = getText();
int cursorPosition = getSelectionEnd();
int end = tokenizer.findTokenEnd(editable, cursorPosition);
int start = tokenizer.findTokenStart(editable, cursorPosition);
return tokenizer.findTokenEnd(editable, cursorPosition);
}

private int getCorrectedTokenBeginning(int end) {
int start = tokenizer.findTokenStart(getText(), end);
if (start < prefix.length()) {
start = prefix.length();
}
return start;
}

protected String currentCompletionText() {
if (hintVisible) return ""; //Can't have any text if the hint is visible

Editable editable = getText();
int end = getCorrectedTokenEnd();
int start = getCorrectedTokenBeginning(end);

//Some keyboards add extra spaces when doing corrections, so
return TextUtils.substring(editable, start, end);
}

Expand Down Expand Up @@ -433,10 +447,7 @@ public void invalidate() {

@Override
public boolean enoughToFilter() {
Editable text = getText();

if (tokenizer == null)
{
if (tokenizer == null) {
return false;
}

Expand All @@ -446,11 +457,8 @@ public boolean enoughToFilter() {
return false;
}

int end = tokenizer.findTokenEnd(text, cursorPosition);
int start = tokenizer.findTokenStart(text, cursorPosition);
if (start < prefix.length()) {
start = prefix.length();
}
int end = getCorrectedTokenEnd();
int start = getCorrectedTokenBeginning(end);

//Don't allow 0 length entries to filter
return end - start >= Math.max(getThreshold(), 1);
Expand Down Expand Up @@ -789,11 +797,15 @@ protected void replaceText(CharSequence text) {

Editable editable = getText();
int cursorPosition = getSelectionEnd();
int end = tokenizer.findTokenEnd(editable, cursorPosition);
int start = tokenizer.findTokenStart(editable, cursorPosition);
if (start < prefix.length()) {
start = prefix.length();
int end = cursorPosition;
int start = cursorPosition;
if (!hintVisible) {
//If you force the drop down to show when the hint is visible, you can run a completion
//on the hint. If the hint includes commas, this truncates and inserts the hint in the field
end = getCorrectedTokenEnd();
start = getCorrectedTokenBeginning(end);
}

String original = TextUtils.substring(editable, start, end);

if (editable != null) {
Expand Down

0 comments on commit 17c50a6

Please sign in to comment.