Re: NSTextView completion
Re: NSTextView completion
- Subject: Re: NSTextView completion
- From: Douglas Davidson <email@hidden>
- Date: Mon, 18 Jul 2005 10:14:27 -0700
On Jul 16, 2005, at 1:49 PM, Mark Alldritt wrote:
Does anyone know how to have NSTextView's completion invoke its self
automatically as the user types (as is seen in Apple's Script Editor)?
NSTextView won't do it for you, but you can readily invoke it
yourself. For example, you can act as the text view's delegate, take
note of changes in the text, and decide based on those when you want
to invoke completion. You may wish to use a timer that you reset
when the text or the selection changes. You may also want to take
note of hasMarkedText so as not to interfere with input methods.
I've appended some code from a little demo I gave at WWDC last year,
which might help you get started.
Douglas Davidson
#define COMPLETION_DELAY (0.5)
@implementation Controller
- (void)doCompletion:(NSTimer *)timer {
[self stopCompletionTimer];
[myTextView complete:nil];
}
- (void)startCompletionTimer {
[self stopCompletionTimer];
completionTimer = [[NSTimer
scheduledTimerWithTimeInterval:COMPLETION_DELAY target:self
selector:@selector(doCompl
etion:) userInfo:nil repeats:NO] retain];
}
- (void)stopCompletionTimer {
[completionTimer invalidate];
[completionTimer release];
completionTimer = nil;
}
- (BOOL)textView:(NSTextView *)textView shouldChangeTextInRange:
(NSRange)affectedCharRange replacementString:(NSString
*)replacementString {
/* Start a timer to fire autocompletion on the typing of "c" in
"Mac". Stop the timer on any other changes. */
if (NSOrderedSame == [@"c" compare:replacementString
options:NSCaseInsensitiveSearch]) {
[self startCompletionTimer];
nextInsertionIndex = affectedCharRange.location +
[replacementString length];
} else {
[self stopCompletionTimer];
nextInsertionIndex = NSNotFound;
}
return YES;
}
- (NSRange)textView:(NSTextView *)textView
willChangeSelectionFromCharacterRange:(NSRange)oldSelectedCharRange
toChara
cterRange:(NSRange)newSelectedCharRange {
/* Stop the timer on selection changes other than those caused
by the typing that started the timer. */
if (newSelectedCharRange.length > 0 ||
newSelectedCharRange.location != nextInsertionIndex) [self
stopCompletionTi
mer];
return newSelectedCharRange;
}
- (NSArray *)textView:(NSTextView *)textView completions:(NSArray *)
words forPartialWordRange:(NSRange)charRange index
OfSelectedItem:(int *)index {
/* Complete "Mac" to "Macintosh", "Mac OS", or "Mac OS X". */
NSArray *result = nil;
NSString *string = [[textView string]
substringWithRange:charRange];
if (NSOrderedSame == [@"Mac" compare:string
options:NSCaseInsensitiveSearch]) {
result = [NSArray arrayWithObjects:@"Macintosh", @"Mac OS",
@"Mac OS X", nil];
}
return result;
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden