The answer from Ken Thomases here is correct in its analysis of the issue regarding the field editor and how to replace it, but the solution it then recommends – replacing the NSTextStorage
of the field editor – is not the correct solution, according to Apple. In their doc they specifically recommend that for delimiter-balancing the selectionRangeForProposedRange:granularity:
method should be used. Once you have a custom field editor going, as per Ken's answer, you should therefore use the solution for NSTextView
here, applied to a custom NSTextView
subclass that you use for your field editor.
In case it is of interest, using NSTextStorage
's doubleClickAtIndex:
method for delimiter-balancing is probably the wrong solution for several trivial reasons: (1) because Apple says so, (2) because subclassing NSTextStorage
is complicated and error-prone, and (3) because NSTextView
provides a method specifically intended for the purpose of doing things like delimiter-balancing. But it is also wrong for a non-trivial reason: (4) that doubleClickAtIndex:
is documented as "Returns the range of characters that form a word (or other linguistic unit) surrounding the given index, taking language characteristics into account". So doubleClickAtIndex:
is really about how the linguistic units of the text (i.e. words) are defined, and redefining those in some way to make delimiter-balancing work would probably break other aspects of word-level text processing. For example, I would guess that it would be pretty tricky to make double-click-drag (dragging out a selection word by word) work properly if you have overridden doubleClickAtIndex:
to do delimiter balancing. Cocoa may use doubleClickAtIndex:
for other aspects of word-finding too, and may add more uses of it in the future. Since a delimiter-balanced section of text is not a "word", who knows what weirdness might result.
- textView:doubleClickedOnCell:inRect:atIndex:
is called on your text field'sdelegate
, not on your custom NSTextView. – Aaron BragermouseDown:
in a subclass ofNSTextView
provided as a custom field editor. – JWWalker