Auto-complete in an NSTextView

  • How would I go about making some kind of auto-complete menu, in an
    NSTextView?

    Xcode's IDE has auto-complete, as does Safari and even Mail's "To:"
    field.

    I'd like to add an auto-complete pretty much like those, to an
    NSTextView.

    How is it done?

    --
    http://elfdata.com/plugin/
  • On 17 apr 2006, at 19.02, Theodore H. Smith wrote:

    > How would I go about making some kind of auto-complete menu, in an
    > NSTextView?
    >
    > Xcode's IDE has auto-complete, as does Safari and even Mail's "To:"
    > field.
    >
    > I'd like to add an auto-complete pretty much like those, to an
    > NSTextView.
    >
    > How is it done?

    How about searching for "completion" in the API reference
    documentation for NSTextView?

    j o a r
  • On Apr 17, 2006, at 10:02 AM, Theodore H. Smith wrote:

    > How would I go about making some kind of auto-complete menu, in an
    > NSTextView?
    >
    > Xcode's IDE has auto-complete, as does Safari and even Mail's "To:"
    > field.
    >
    > I'd like to add an auto-complete pretty much like those, to an
    > NSTextView.
    >
    > How is it done?
    >

    The examples you mention probably roll their own, but here is a
    little demo I put together for WWDC a few years back.  It's fairly
    crude, but it at least is something to start from.

    Douglas Davidson

    @interface Controller : NSObject {
        id myTextView;    // We should be set up in the nib as this text
    view's delegate.
        unsigned nextInsertionIndex;
        NSTimer *completionTimer;
    }

    - (void)doCompletion:(NSTimer *)timer;
    - (void)startCompletionTimer;
    - (void)stopCompletionTimer;

    @end

    #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(doCompletion:) userInfo:nil repeats:NO] retain];
    }

    - (void)stopCompletionTimer {
        [completionTimer invalidate];
        [completionTimer release];
        completionTimer = nil;
    }

    /* NSTextView delegate methods */

    - (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
    toCharacterRange:(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
    stopCompletionTimer];
        return newSelectedCharRange;
    }

    - (NSArray *)textView:(NSTextView *)textView completions:(NSArray *)
    words forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(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
  • On 17 Apr 2006, at 18:25, Douglas Davidson wrote:

    > On Apr 17, 2006, at 10:02 AM, Theodore H. Smith wrote:
    >
    >> How would I go about making some kind of auto-complete menu, in an
    >> NSTextView?
    >
    > The examples you mention probably roll their own, but here is a
    > little demo I put together for WWDC a few years back.  It's fairly
    > crude, but it at least is something to start from.

    That's a very helpful example.  On a closely related note, what do
    you feel it the best method for attaching such an auto-completion to
    an NSTextField?  I assume that I'm going to have to customise an
    NSTextFieldCell with my own -setUpFieldEditorAttributes: but I'm not
    sure if I can simply add some code to switch the delegate of the
    window-wide field editor to point to the completion bits or if I need
    to make my own field editor object.  Do you have an opinion?

    Nicko
  • On Apr 18, 2006, at 6:32 AM, Nicko van Someren wrote:

    > That's a very helpful example.  On a closely related note, what do
    > you feel it the best method for attaching such an auto-completion
    > to an NSTextField?  I assume that I'm going to have to customise an
    > NSTextFieldCell with my own -setUpFieldEditorAttributes: but I'm
    > not sure if I can simply add some code to switch the delegate of
    > the window-wide field editor to point to the completion bits or if
    > I need to make my own field editor object.  Do you have an opinion?
    >

    The text field usually acts as the field editor's delegate, so you
    could potentially use a custom text field subclass.  You could also
    consider using a combo box.

    Douglas Davidson
previous month april 2006 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today