Invalid NFextField Autocomplete Delegate Method

I added the following delegate method for NSTextField to add autocomplete support:

 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger *)index 

The problem is that this method is never called. I can verify that the delegate for NSTextField set correctly, because the other delegate methods work as they should.

+7
source share
1 answer

You will need to complete: call the text field field editor at some point. This is what launches the completion menu, but it does not automatically get called. If you do not have F5 associated with anything, try entering it in the field and clicking on it. Termination should start then; Option-Esc may also work.

If you want automatic completion, this requires some work. You can start with something like this:

 - (void)controlTextDidChange:(NSNotification *)note { if( amDoingAutoComplete ){ return; } else { amDoingAutoComplete = YES; [[[note userInfo] objectForKey:@"NSFieldEditor"] complete:nil]; } } 

Some flag is needed, because when starting the completion, NSControlTextDidChangeNotification will be posted again, which causes it, causing the completion, which changes the control text, which ...

Obviously, you will need to disable the flag at some point. It will depend on how you want to handle user interaction with autocomplete - maybe there will be only one completion for this initial line, or the user will have to continue typing to limit the possibilities (in this case, you will need to start autocomplete again)?

A simple flag may not quite do it; it seems that although the notification will be re-published, the string field editor will not be changed - it will change only in response to direct keyboard input. In my autocomplete implementation, I found that I needed to save a copy of the "last entered line" and compare each time with the contents of the field editor.

+6
source

All Articles