From 8cf1c235e6cc45d6f32db734f5386da26d7dd186 Mon Sep 17 00:00:00 2001 From: Gabriel Reimers Date: Sun, 23 Feb 2020 10:33:10 +0100 Subject: [PATCH] Fix crash when untabbing at end of file #28 --- Source/Tincta/TextEditor/TCTextView.m | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Source/Tincta/TextEditor/TCTextView.m b/Source/Tincta/TextEditor/TCTextView.m index 9b39cfa..853151f 100755 --- a/Source/Tincta/TextEditor/TCTextView.m +++ b/Source/Tincta/TextEditor/TCTextView.m @@ -268,6 +268,8 @@ - (void)insertBacktab:(id)sender { NSInteger selLength = selRange.length; NSInteger selMax = NSMaxRange(selRange); NSString* text = [self string]; + + NSRange lineRange = [text lineRangeForRange:NSMakeRange(selLoc, 0)]; NSInteger lineStart = lineRange.location; while (lineStart <= selMax) { //iterate over all selected lines @@ -317,13 +319,13 @@ - (void)insertBacktab:(id)sender { if (selLoc > self.string.length || selLoc < 0) { selLoc = 0; } - } else if ((lineStart >= selLoc)&& [text characterAtIndex:lineStart] == '\t') { + } else if ((lineStart >= selLoc) && (text.length > lineStart) && [text characterAtIndex:lineStart] == '\t') { //remove tab at line start stringToReplace = @"\t"; blankLength = 1; tabReplaceRange = NSMakeRange(lineStart, blankLength); selLength -= blankLength; - } else if ((lineStart >= selLoc)&& [text characterAtIndex:lineStart] == ' ') { + } else if ((lineStart >= selLoc) && (text.length > lineStart) &&[text characterAtIndex:lineStart] == ' ') { //remove blanks at line start blankLength = 0; NSInteger tabLength = tabBlankLength;