Fine-tuning NSTextField autocompletion
I am using NSControlTextEditingDelegate to auto-complete what the NSSearchField has typed with the custom suggestions I am generating.
complete:
a message is sent to the field editor when the text changes.
Now I would like to fine-tune the autocomplete behavior and make it work exactly the same way the Google search textbox works in Safari:
-
When two or more words (separated by spaces) are entered into an NSTextField, the location for the NSRange passed in
control:textView:textView:completions:forPartialWordRange: indexOfSelectedItem:
starts after the last space has been entered.This means that if the custom types "San Fran" and Google APIs return "San Francisco", selecting it from the autocomplete list the NSTextField is replaced with "San Francisco".
A possible workaround is to restore the NSArray containing the completion and return only the suffix of the actual clauses, depending on charRange.location. It looks crappy (because the autocomplete list now only shows part of the completion string), so I want to be able to
NSTextField
ignore whitespace when asking for autocomplete. -
Selecting auto complete from the list, the default NSSearchField action is not sent. At the moment, the user needs to select autocomplete, and then press enter to start the search.
source to share
You can do this with a custom field editor rather than relying entirely on delegate callbacks.
1) Override rangeForUserCompletion
and adjust the return value so that it is rangeForUserCompletion
text after the space.
2) Override insertCompletion:forPartialWordRange:movement:isFinal
and if isFinal checked, do confirmation processing.
Step (1) above will set the range. If you also set the index to completionsForPartialWordRange
to fill the edit (or leave it at the default 0), a space along with '/' and '.' will accept the currently selected line, even if the user just wanted to keep typing. To work around this, insertCompletion:forPartialWordRange:movement:isFinal
if isFinal is true, check [NSApp currentEvent]
for those characters, and if found, skip them without processing or calling super.
source to share