From ab8c3c2b75d0b5752cddf30b03af53cb6c423dc0 Mon Sep 17 00:00:00 2001 From: daboe01 Date: Wed, 30 Dec 2015 17:35:14 +0100 Subject: [PATCH] granular selection refac --- AppKit/CPTextView/CPTextView.j | 60 ++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/AppKit/CPTextView/CPTextView.j b/AppKit/CPTextView/CPTextView.j index 17141b30a..5da7c3482 100644 --- a/AppKit/CPTextView/CPTextView.j +++ b/AppKit/CPTextView/CPTextView.j @@ -770,46 +770,50 @@ var kDelegateRespondsTo_textShouldBeginEditing return CPMakeRange(0, 0); if (proposedRange.location >= textStorageLength) - return CPMakeRange(textStorageLength, 0); + proposedRange = CPMakeRange(textStorageLength, 0); if (CPMaxRange(proposedRange) > textStorageLength) proposedRange.length = textStorageLength - proposedRange.location; - var string = [_textStorage string]; + var string = [_textStorage string], + regex, + loc = proposedRange.location; + switch (granularity) { case CPSelectByWord: - if (_isNewlineCharacter([string characterAtIndex:proposedRange.location])) - return CPMakeRange(proposedRange.location, 1); - - var wordRange = [self _characterRangeForIndex:proposedRange.location inRange:proposedRange asDefinedByRegex:[[self class] _wordBoundaryRegex] skip:YES]; - - if (proposedRange.length) - wordRange = CPUnionRange(wordRange, [self _characterRangeForIndex:CPMaxRange(proposedRange) inRange:proposedRange asDefinedByRegex:[[self class] _wordBoundaryRegex] skip:NO]); - - return wordRange; - + regex = [[self class] _wordBoundaryRegex]; + break; case CPSelectByParagraph: - if (_isNewlineCharacter([string characterAtIndex:proposedRange.location])) - return CPMakeRange(proposedRange.location, 1); - - var parRange = [self _characterRangeForIndex:proposedRange.location inRange:proposedRange asDefinedByRegex:[[self class] _paragraphBoundaryRegex] skip:YES]; - - if (proposedRange.length) - parRange = CPUnionRange(parRange, [self _characterRangeForIndex:CPMaxRange(proposedRange) - inRange:proposedRange - asDefinedByRegex:[[self class] _paragraphBoundaryRegex] - skip:NO]); - - if (parRange.length > 0 && [self _isCharacterAtIndex:CPMaxRange(parRange) granularity:CPSelectByParagraph]) - parRange.length++; - - return parRange; - + regex = [[self class] _paragraphBoundaryRegex]; + break; default: return proposedRange; } + + if (loc > 0 && _isNewlineCharacter([string characterAtIndex:loc])) + loc--; + + var granularRange = [self _characterRangeForIndex:loc inRange:proposedRange asDefinedByRegex:regex skip:YES]; + + if (proposedRange.length == 0 && _isNewlineCharacter([string characterAtIndex:proposedRange.location])) + return _MakeRangeFromAbs(_isNewlineCharacter([string characterAtIndex:loc])? proposedRange.location : granularRange.location, proposedRange.location + 1); + + if (proposedRange.length) + granularRange = CPUnionRange(granularRange, [self _characterRangeForIndex:CPMaxRange(proposedRange) + inRange:proposedRange + asDefinedByRegex:regex + skip:YES]); + + switch (granularity) + { + case CPSelectByParagraph: + if (_isNewlineCharacter([string characterAtIndex:CPMaxRange(granularRange)])) + granularRange.length++; + } + + return granularRange; } #pragma mark -