NSUndo & TextStorage limitation

  • There seems to be a limitation or a bug in the NSUndo TextStorage.

    Let's imagine I have 2 text fields A and B.

    +----+ +----+
    | A  | | B  |
    +----+ +----+

    When I type text in A, - (void)controlTextDidChange:(NSNotification
    *) aNotification is called for by A's delegate. The implementation
    may decide to make B the first responder if A contains 3 letters.

    Here is the problem with the NSUndo (Stack, Manager, whatever):

    1. I type 3 letters in A
    2. the first responder is set to be B
    3. I select all the letters in A
    4. I delete them with the delete key
    5. I use the Undo Typing menu item

    Result:

    A is filled back with the 3 original letters
    B is set as the first responder

    But an exception is raised claiming that [NSBigMutableString
    substring:fromIndex:] has some values out of range.

    This problem is caused by the fact that the first responder is no
    more the "original" first responder after controlTextDidChange: was
    called.

    "Of course", this can be solved by calling makeFirstResponder: with a
    delay (performSelector:withObject:afterDelay:).

    But this looks like to be a limitation (or a bug) in the NSUndo API
    or the TextStorage implementation.

    Any second thought?
  • on 10/11/06 5:00 AM, <ssudre...> purportedly said:

    > A is filled back with the 3 original letters
    > B is set as the first responder
    >
    > But an exception is raised claiming that [NSBigMutableString
    > substring:fromIndex:] has some values out of range.
    >
    > This problem is caused by the fact that the first responder is no
    > more the "original" first responder after controlTextDidChange: was
    > called.
    >
    > "Of course", this can be solved by calling makeFirstResponder: with a
    > delay (performSelector:withObject:afterDelay:).
    >
    > But this looks like to be a limitation (or a bug) in the NSUndo API
    > or the TextStorage implementation.

    I have come across similar issues, and I would say that it's not the fault
    of NSUndoManager or the storage class (which I think is part of the NSString
    class cluster), but the field editor, who is leaving the undo stack in a bad
    state.

    In my understanding, once the field editor has committed (or discarded)
    editing the undo stack is cleared. It appears that -makeFirstResponder is
    not "committing" before changing. This may have to do with there being only
    one field editor instance that prefers changes to occur through the GUI,
    rather than programmatically. Have you tried calling -commitEditing on the
    field editor before -makeFirstResponder?

    IMHO, this is more of an issue of Cocoa's notorious
    if-you-use-this-method-there-is-other-stuff-you-need-to-do-but-it's-not-docu
    mented-clearly issue.

    Best,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
previous month october 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 31          
Go to today