Undocumented NSTextMovment for space in NSTextView's -insertCompletion:...
Undocumented NSTextMovment for space in NSTextView's -insertCompletion:...
- Subject: Undocumented NSTextMovment for space in NSTextView's -insertCompletion:...
- From: Keith Blount <email@hidden>
- Date: Fri, 5 Dec 2008 11:25:34 -0800 (PST)
Hi,
This is an observation more than a question, but it may be that someone can point me in the direction of a more orthodox solution than my current one.
My application has a "suggest completions as you you type" feature. If this is on, then NSTextView's -complete: action gets called in -didChangeText after a slight delay, so that if the user pauses from typing, a list of completions automatically pops up without the need for hitting opt-escape or cmd-period.
In order for this feature not to be hideously annoying, I have overridden NSTextView's -insertCompletion:forPartialWordRange:movement:isFinal: method to ensure that completions don't get inserted if the user carries on typing. I do this by just having this method do nothing if auto-suggestion is turned on and the text movement was NSOtherTextMovement, like this:
- (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag
{
// If we suggest completions whilst typing, don't insert the completion unless the user has specifically chosen it!
if ([self autoCompletionEnabled])
{
if (flag == NO || (flag == YES && movement == NSOtherTextMovement))
return;
}
}
All of which works very nicely. However, I would also like completions *not* to get inserted if the user hits the space key, as this can be a little annoying, too. I thought the space key would be covered by NSOtherTextMovement, given that the only values declared for NSTextMovement in NSText are:
enum {
NSIllegalTextMovement = 0,
NSReturnTextMovement = 0x10,
NSTabTextMovement = 0x11,
NSBacktabTextMovement = 0x12,
NSLeftTextMovement = 0x13,
NSRightTextMovement = 0x14,
NSUpTextMovement = 0x15,
NSDownTextMovement = 0x16,
NSCancelTextMovement = 0x17,
NSOtherTextMovement = 0
};
No NSSpaceTextMovement is defined, so it seems reasonable to expect this to be covered by "other text movement". However, it turns out that this is not the case. Testing shows that if you hit the space key, the "movement" parameter of -insertCompletion:... gets passed in with a value of 20. So it seems that there is at least one other, undeclared, value for NSTextMovement, even though the docs for NSText state that the above are the only "possible values".
Thus, it seems that
NSSpaceTextMovement = 0x20
So, I can fix up my overridden method thus:
- (void)insertCompletion:(NSString *)word forPartialWordRange:(NSRange)charRange movement:(NSInteger)movement isFinal:(BOOL)flag
{
// If we suggest completions whilst typing, don't insert the completion unless the user has specifically chosen it!
if ([self autoCompletionEnabled])
{
// Note: testing shows that hitting the space bar provides us with an NSTextMovement value of 20,
// even though this is undocumented. Thus we check for this too - so if the user is typing or hit the space, we don't enter
// a completion.
if (flag == NO || (flag == YES && (movement == NSOtherTextMovement || movement == 20)))
return;
}
}
This works fine. However, I'm a little uncomfortable using a value that isn't defined in the documentation. Does anyone know if there is any reason that I am seeing this behaviour, or why this value isn't defined in the docs? Is it safe to rely on this? Is this value specific only to this text view method, perhaps?
Thanks and all the best,
Keith
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden