Skip to content

Commit

Permalink
Merge pull request #2558 from daboe01/smartcopypastefixes
Browse files Browse the repository at this point in the history
Fixed: smart copy/paste did not work properly [+1]
  • Loading branch information
mrcarlberg committed Sep 8, 2017
2 parents 403b12f + 72d96e9 commit 25dc38b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 deletions.
1 change: 1 addition & 0 deletions AppKit/CPPasteboard.j
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ CPURLPboardType = @"CPURLPboardType";
CPImagesPboardType = @"CPImagesPboardType";
CPVideosPboardType = @"CPVideosPboardType";
CPRTFPboardType = @"CPRTFPboardType";
_CPSmartPboardType = @"_CPSmartPboardType";

UTF8PboardType = @"public.utf8-plain-text";

Expand Down
7 changes: 7 additions & 0 deletions AppKit/CPText.j
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
@import "CPView.j"

@global CPStringPboardType
@global _CPSmartPboardType

@class CPAttributedString
@class _CPRTFParser

Expand Down Expand Up @@ -175,6 +177,11 @@ CPKernAttributeName = @"CPKernAttributeName";
return stringForPasting;
}

- (BOOL)_shouldUseSmartPasting
{
return parseInt([[CPPasteboard generalPasteboard] stringForType:_CPSmartPboardType], 10) > 0 || NO;
}

- (void)paste:(id)sender
{
var stringForPasting = [self _stringForPasting];
Expand Down
38 changes: 9 additions & 29 deletions AppKit/CPTextView/CPTextView.j
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ var kDelegateRespondsTo_textShouldBeginEditing
CPSelectionGranularity _selectionGranularity @accessors(property=selectionGranularity);

CPSelectionGranularity _previousSelectionGranularity; // private
CPSelectionGranularity _copySelectionGranularity; // private

CPTextContainer _textContainer @accessors(property=textContainer);
CPTextStorage _textStorage @accessors(getter=textStorage);
Expand Down Expand Up @@ -373,7 +372,6 @@ var kDelegateRespondsTo_textShouldBeginEditing

- (void)copy:(id)sender
{
_copySelectionGranularity = _previousSelectionGranularity;
[super copy:sender];

if (![self isRichText])
Expand All @@ -387,14 +385,17 @@ var kDelegateRespondsTo_textShouldBeginEditing
[pasteboard declareTypes:[CPStringPboardType, CPRTFPboardType] owner:nil];
[pasteboard setString:stringForPasting._string forType:CPStringPboardType];
[pasteboard setString:richData forType:CPRTFPboardType];
[pasteboard setString:_previousSelectionGranularity + '' forType:_CPSmartPboardType];
}

- (void)_pasteString:(id)stringForPasting
{
if (!stringForPasting)
return;

if (_copySelectionGranularity > 0 && _selectionRange.location > 0)
var shouldUseSmartPasting = [self _shouldUseSmartPasting];

if (shouldUseSmartPasting && _selectionRange.location > 0)
{
if (!_isWhitespaceCharacter([[_textStorage string] characterAtIndex:_selectionRange.location - 1]) &&
_selectionRange.location != [_layoutManager numberOfCharacters])
Expand All @@ -403,29 +404,9 @@ var kDelegateRespondsTo_textShouldBeginEditing
}
}

if (_copySelectionGranularity == CPSelectByParagraph)
{
var peekStr = stringForPasting,
i = 0;

if (![stringForPasting isKindOfClass:[CPString class]])
peekStr = stringForPasting._string;

while (_isWhitespaceCharacter([peekStr characterAtIndex:i]))
i++;

if (i)
{
if ([stringForPasting isKindOfClass:[CPString class]])
stringForPasting = [stringForPasting stringByReplacingCharactersInRange:CPMakeRange(0, i) withString:''];
else
[stringForPasting replaceCharactersInRange:CPMakeRange(0, i) withString:''];
}
}

[self insertText:stringForPasting];

if (_copySelectionGranularity > 0)
if (shouldUseSmartPasting)
{
if (!_isWhitespaceCharacter([[_textStorage string] characterAtIndex:CPMaxRange(_selectionRange)]) &&
!_isNewlineCharacter([[_textStorage string] characterAtIndex:MAX(0, _selectionRange.location - 1)]) &&
Expand Down Expand Up @@ -1514,7 +1495,7 @@ var kDelegateRespondsTo_textShouldBeginEditing
[_CPNativeInputManager cancelCurrentInputSessionIfNeeded]; // handle ESC during native input
}

- (void)deleteBackward:(id)sender ignoreSmart:(BOOL)ignoreFlag
- (void)deleteBackward:(id)sender handleSmart:(BOOL)handleSmart
{
var changedRange;

Expand All @@ -1524,7 +1505,7 @@ var kDelegateRespondsTo_textShouldBeginEditing
changedRange = _selectionRange;

// smart delete
if (!ignoreFlag && _copySelectionGranularity > 0 &&
if (handleSmart &&
changedRange.location > 0 && _isWhitespaceCharacter([[_textStorage string] characterAtIndex:_selectionRange.location - 1]) &&
changedRange.location < [[self string] length] && _isWhitespaceCharacter([[_textStorage string] characterAtIndex:CPMaxRange(changedRange)]))
changedRange.length++;
Expand All @@ -1535,8 +1516,7 @@ var kDelegateRespondsTo_textShouldBeginEditing

- (void)deleteBackward:(id)sender
{
_copySelectionGranularity = _previousSelectionGranularity; // smart delete
[self deleteBackward:self ignoreSmart:_selectionRange.length > 0? NO:YES];
[self deleteBackward:self handleSmart:[self _shouldUseSmartPasting] && _selectionRange.length > 0];
}

- (void)deleteForward:(id)sender
Expand All @@ -1559,7 +1539,7 @@ var kDelegateRespondsTo_textShouldBeginEditing
return;

[self copy:sender];
[self deleteBackward:sender ignoreSmart:NO];
[self deleteBackward:sender handleSmart:_previousSelectionGranularity];
}

- (void)insertLineBreak:(id)sender
Expand Down

0 comments on commit 25dc38b

Please sign in to comment.