Note that there are some explanatory texts on larger screens.

plurals
  1. POCocoa: looking for a general strategy for programmatic manipulation of NSTextView storage without messing up undo
    primarykey
    data
    text
    <p>I am writing a special-purpose text editor in cocoa that does things like automatic text substitution, inline text completions (ala <strong>Xcode</strong>), etc.</p> <p>I need to be able to programmatically manipulate the <code>NSTextView</code>’s <code>NSTextStorage</code> in response to 1) user typing, 2) user pasting, 3) user dropping text.</p> <p>I have tried two different general approaches and both of them have caused the <code>NSTextView</code>’s native undo manager to get out of sync in different ways. In each case, I am only using <code>NSTextView</code> delegate methods. I have been trying to avoid subclassing <code>NSTextview</code> or <code>NSTextStorage</code> (though I will subclass if necessary).</p> <p>The first approach I tried was doing the manipulations from within the <code>textView</code> <code>delegate</code>’s <code>textDidChange</code> method. From within that method, I analyzed what had been changed in the <code>textView</code> and then called a general purpose method for modifying text that wrapped the changes in the textStorage with calls to <code>shouldChangeTextInRange:</code> and <code>didChangeText:</code>. Some of the programmatic changes allowed clean undo’s but some did not.</p> <p>The second (and maybe more intuitive because it makes changes before the text actually appears in the <code>textView</code>) approach I tried was doing the manipulations from within the <code>delegate</code>’s <code>shouldChangeTextInRange:</code> method, again using the same general purpose storage modification method that wraps changes in the storage with a call to <code>shouldChangeTextInRange:</code> and <code>didChangeText:</code>. Since these changes were being triggered originally from within <code>shouldChangeTextInRange:</code>, I set a flag that told the inner call to <code>shouldChangeTextInRange:</code> to be ignored so as not to enter recursive blackholeness. Again, Some of the programmatic changes allowed clean undo’s but some did not (though different ones this time, and in different ways).</p> <p>With all that background, my question is, can someone point me to a general strategy for programmatically manipulating the storage of an <code>NSTextview</code> that will keep the undo manager clean and in sync?</p> <p>In which <code>NSTextview</code> delegate method should I pay attention to the text changes in the textView (via typing, pasting, or dropping) and do the manipulations to the <code>NSTextStorage</code>? Or is the only clean way to do this by subclassing either <code>NSTextView</code> or <code>NSTextStorage</code>?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload