Skip to content

Commit 2b09e4e

Browse files
Fix insertText: deprecation: switch to insertText:replacementRange:
All 6 remaining call sites (5 in NSTextView+Autocomplete.m, 1 in MPDocument.m) were plain 'insert at wherever the cursor/selection currently is' cases, immediately adjacent in every file to other calls that already use -insertText:replacementRange:. Converted each to the same 2-arg NSTextInputClient method, passing NSMakeRange(NSNotFound, 0) -- Apple's documented sentinel meaning 'use the current selection, or the marked (IME composition) range if there is one', which is exactly what the deprecated 1-arg -insertText: did. This preserves IME/marked-text behavior correctly, which plain self.selectedRange would not have during active composition (this file already special-cases marked text elsewhere, e.g. hasMarkedText in completeMatchingCharacterForText:atLocation:).
1 parent e0df9c8 commit 2b09e4e

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

MacDown/Code/Document/MPDocument.m

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1516,7 +1516,12 @@ - (IBAction)insertNewParagraph:(id)sender
15161516
if (location == newlineBefore + 1 && location == newlineAfter)
15171517
[self.editor insertNewline:self];
15181518
else
1519-
[self.editor insertText:@"\n\n"];
1519+
// NSMakeRange(NSNotFound, 0) is Apple's documented sentinel for
1520+
// -insertText:replacementRange: meaning "use the current selection,
1521+
// or the marked (IME composition) range if there is one" -- the
1522+
// same behavior the deprecated 1-arg -insertText: had.
1523+
[self.editor insertText:@"\n\n"
1524+
replacementRange:NSMakeRange(NSNotFound, 0)];
15201525
}
15211526

15221527
- (IBAction)setEditorOneQuarter:(id)sender

MacDown/Code/Extension/NSTextView+Autocomplete.m

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ - (void)insertSpacesForTab
9393
NSUInteger offset = (currentLocation - p - 1) % 4;
9494
if (offset)
9595
spaces = [spaces substringFromIndex:offset];
96-
[self insertText:spaces];
96+
// NSMakeRange(NSNotFound, 0) is Apple's documented sentinel for
97+
// -insertText:replacementRange: meaning "use the current selection, or
98+
// the marked (IME composition) range if there is one" -- the same
99+
// behavior the deprecated 1-arg -insertText: had. Every other call in
100+
// this file already uses the 2-arg form; these were the stragglers.
101+
[self insertText:spaces replacementRange:NSMakeRange(NSNotFound, 0)];
97102
}
98103

99104
- (BOOL)completeMatchingCharactersForTextInRange:(NSRange)range
@@ -583,7 +588,7 @@ - (BOOL)completeNextListItem:(BOOL)autoIncrement
583588
if (contentLength > location + t.length
584589
&& [[content substringWithRange:r] isEqualToString:t])
585590
{
586-
[self insertText:indent];
591+
[self insertText:indent replacementRange:NSMakeRange(NSNotFound, 0)];
587592
return YES;
588593
}
589594

@@ -598,7 +603,7 @@ - (BOOL)completeNextListItem:(BOOL)autoIncrement
598603
// Insert completion for normal cases.
599604
if (t.length)
600605
it = [NSString stringWithFormat:@"%@ ", it];
601-
[self insertText:it];
606+
[self insertText:it replacementRange:NSMakeRange(NSNotFound, 0)];
602607
return YES;
603608
}
604609

@@ -639,7 +644,7 @@ - (BOOL)completeNextBlockquoteLine
639644
}
640645

641646
// Insert completion.
642-
[self insertText:markers];
647+
[self insertText:markers replacementRange:NSMakeRange(NSNotFound, 0)];
643648
return YES;
644649
}
645650

@@ -658,7 +663,8 @@ - (BOOL)completeNextIndentedLine
658663

659664
[self insertNewline:self];
660665
NSRange indentRange = NSMakeRange(start, end - start);
661-
[self insertText:[content substringWithRange:indentRange]];
666+
[self insertText:[content substringWithRange:indentRange]
667+
replacementRange:NSMakeRange(NSNotFound, 0)];
662668
return YES;
663669
}
664670

0 commit comments

Comments
 (0)