I figured out how to do it.
You need to override NSFieldEditor for NSTextViews.
To provide an overridden version in an NSWindow delegate:
- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)client { if ([client isKindOfClass:[NSSearchField class]]) { if (!_mlFieldEditor) { _mlFieldEditor = [[MLFieldEditor alloc] init]; [_mlFieldEditor setFieldEditor:YES]; } return _mlFieldEditor; } return nil; }
_mlFieldEditor is an instance variable. Here is the definition:
@interface MLFieldEditor : NSTextView @end @implementation MLFieldEditor - (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag { // suppress completion if user types a space if (movement == NSRightTextMovement) return; // show full replacements if (charRange.location != 0) { charRange.length += charRange.location; charRange.location = 0; } [super insertCompletion:word forPartialWordRange:charRange movement:movement isFinal:flag]; if (movement == NSReturnTextMovement) { [[NSNotificationCenter defaultCenter] postNotificationName:MLSearchFieldAutocompleted object:self userInfo:nil]; } } @end
The key part is NSReturnTextMovement after [super insertCompletion ...].
The first part will change it so that entering a space will not perform autocompletion, which was done by me in a comment: How to prevent NSSearchField from overwriting the entered lines using the first entry in the autocomplete list?
Jeremy
source share