|
27 | 27 | import android.widget.MultiAutoCompleteTextView; |
28 | 28 |
|
29 | 29 | /** |
30 | | - * The default tokenizer that used in CodeView auto complete feature |
| 30 | + * The default tokenizer that used for CodeView auto complete feature |
31 | 31 | */ |
32 | 32 | public class KeywordTokenizer implements MultiAutoCompleteTextView.Tokenizer { |
33 | 33 |
|
34 | 34 | @Override |
35 | 35 | public int findTokenStart(CharSequence charSequence, int cursor) { |
36 | | - String sequenceStr = charSequence.toString(); |
37 | | - sequenceStr = sequenceStr.substring(0, cursor); |
38 | | - |
39 | | - int spaceIndex = sequenceStr.lastIndexOf(" "); |
40 | | - int lineIndex = sequenceStr.lastIndexOf("\n"); |
41 | | - int bracketIndex = sequenceStr.lastIndexOf("("); |
42 | | - |
43 | | - int index = Math.max(0, Math.max(spaceIndex, Math.max(lineIndex, bracketIndex))); |
44 | | - if(index == 0) return 0; |
45 | | - return (index + 1 < charSequence.length()) ? index + 1 : index; |
| 36 | + // All text until the current cursor position |
| 37 | + final String sequenceStr = charSequence.toString().substring(0, cursor); |
| 38 | + |
| 39 | + // Iterate until find space, newline or (, starting from the current cursor position |
| 40 | + for (int i = cursor - 1; i >= 0; i--) { |
| 41 | + // Return the next position after the prefix character |
| 42 | + final char c = sequenceStr.charAt(i); |
| 43 | + if (c == ' ' || c == '\n' || c == '(') return i + 1; |
| 44 | + } |
| 45 | + |
| 46 | + // If no prefix character found then token start is the start of text |
| 47 | + return 0; |
46 | 48 | } |
47 | 49 |
|
48 | 50 | @Override |
|
0 commit comments