@@ -86,7 +86,7 @@ protected void onAcceptSuggestionManually(String word, int fromKey) {
8686 )[0 ];
8787 updateShiftState (beforeCursor , true , false );
8888 resetKeyRepeat ();
89- getMagicSuggestions (beforeCursor , word , true );
89+ guessNextWord (beforeCursor , word , true );
9090 }
9191
9292 if (!Characters .getSpace (mLanguage ).equals (word )) {
@@ -147,7 +147,6 @@ protected void handleSuggestions(@Nullable Runnable onComplete) {
147147 }
148148
149149 final ArrayList <String > suggestions = mInputMode .getSuggestions ();
150- final boolean noSuggestionsBefore = suggestionOps .isEmpty ();
151150 suggestionOps .set (suggestions , mInputMode .getRecommendedSuggestionIdx (), mInputMode .containsGeneratedSuggestions ());
152151
153152 // either accept the first one automatically (when switching from punctuation to text
@@ -171,11 +170,11 @@ protected void handleSuggestions(@Nullable Runnable onComplete) {
171170 appHacks .setComposingTextWithHighlightedStem (trimmedWord , mInputMode .getWordStem (), mInputMode .isStemFilterFuzzy ());
172171 }
173172
174- onAfterSuggestionsHandled (onComplete , beforeCursor , trimmedWord , suggestions .isEmpty (), noSuggestionsBefore );
173+ onAfterSuggestionsHandled (onComplete , beforeCursor , trimmedWord , suggestions .isEmpty ());
175174 }
176175
177176
178- private void onAfterSuggestionsHandled (@ Nullable Runnable callback , @ Nullable String beforeCursor , @ Nullable String trimmedWord , boolean noSuggestions , boolean noSuggestionsBefore ) {
177+ private void onAfterSuggestionsHandled (@ Nullable Runnable callback , @ Nullable String beforeCursor , @ Nullable String trimmedWord , boolean noSuggestions ) {
179178 final String shiftStateContext = beforeCursor != null ? beforeCursor + trimmedWord : trimmedWord ;
180179 if (noSuggestions ) {
181180 updateShiftStateDebounced (shiftStateContext , true , false );
@@ -185,15 +184,11 @@ private void onAfterSuggestionsHandled(@Nullable Runnable callback, @Nullable St
185184
186185 forceShowWindow ();
187186
188- // @todo: here get completions for the current word, instead of the next words for it.
189- // if (noSuggestionsBefore && !noSuggestions && !mInputMode.containsSpecialChars()) {
190-
191- // mindReader.guess(
192- // mLanguage,
193- // beforeCursor == null ? textField.getSurroundingStringForAutoAssistance(settings, mInputMode)[0] : beforeCursor + trimmedWord,
194- // false
195- // );
196- // }
187+ // if this is the first letter of a word, and not punctuation, guess what the word might be
188+ // we also check if mind-reading is on to prevent expensive getSurroundingStringForAutoAssistance()
189+ if (!noSuggestions && settings .getAutoMindReading () && mInputMode .getSequenceLength () == 1 && !mInputMode .containsSpecialChars ()) {
190+ guessCurrentWord (beforeCursor , trimmedWord );
191+ }
197192
198193 if (callback != null ) {
199194 callback .run ();
@@ -202,24 +197,38 @@ private void onAfterSuggestionsHandled(@Nullable Runnable callback, @Nullable St
202197
203198
204199 @ Override
205- protected boolean clearMagicContext () {
200+ protected boolean clearGuessingContext () {
206201 return mindReader .clearContext ();
207202 }
208203
209204
210205 @ Override
211- protected void setMagicContext (@ NonNull String beforeCursor , @ Nullable String lastWord ) {
206+ protected void setGuessingContext (@ NonNull String beforeCursor , @ Nullable String lastWord ) {
212207 mindReader .setContext (mInputMode , mLanguage , beforeCursor , lastWord );
213208 }
214209
215210
211+ private void guessCurrentWord (@ Nullable String beforeCursor , @ Nullable String trimmedWord ) {
212+ String beforeWithoutTrimmed = beforeCursor ;
213+ if (beforeWithoutTrimmed == null ) {
214+ beforeWithoutTrimmed = textField .getSurroundingStringForAutoAssistance (settings , mInputMode )[0 ];
215+ if (trimmedWord != null && beforeWithoutTrimmed .endsWith (" " + trimmedWord )) {
216+ beforeWithoutTrimmed = beforeWithoutTrimmed .substring (0 , beforeWithoutTrimmed .length () - trimmedWord .length () - 1 );
217+ }
218+ }
219+
220+ mindReader .guessCurrent (mInputMode , mLanguage , beforeWithoutTrimmed , trimmedWord , this ::handleGuesses );
221+ }
222+
223+
216224 @ Override
217- protected void getMagicSuggestions (@ NonNull String beforeCursor , @ Nullable String lastWord , boolean saveContext ) {
218- mindReader .guess (mInputMode , mLanguage , beforeCursor , lastWord , saveContext , this ::handleMagicSuggestions );
225+ protected void guessNextWord (@ NonNull String beforeCursor , @ Nullable String lastWord , boolean saveContext ) {
226+ mindReader .guessNext (mInputMode , mLanguage , beforeCursor , lastWord , saveContext , this ::handleGuesses );
219227 }
220228
221229
222- private void handleMagicSuggestions (ArrayList <String > suggestions ) {
230+ private void handleGuesses (ArrayList <String > suggestions ) {
231+ // @todo: suggestionOps.addGuesses(suggestions);
223232 Logger .d ("LOG" , "=========> " + suggestions );
224233 }
225234}
0 commit comments