From cdd1be0f8a264264495fce7291a7dfca89fbf019 Mon Sep 17 00:00:00 2001 From: Tzu-ping Chung Date: Fri, 3 Oct 2014 10:44:33 +0800 Subject: [PATCH] Fix whitespaces calculation logic for deletion Fix #176 --- MacDown/Code/Extension/NSTextView+Autocomplete.m | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/MacDown/Code/Extension/NSTextView+Autocomplete.m b/MacDown/Code/Extension/NSTextView+Autocomplete.m index a2042b4..4e404aa 100644 --- a/MacDown/Code/Extension/NSTextView+Autocomplete.m +++ b/MacDown/Code/Extension/NSTextView+Autocomplete.m @@ -249,11 +249,15 @@ static NSString * const kMPBlockquoteLinePattern = @"^((?:\\> ?)+).*$"; if (whitespaceCount < 2) return NO; - NSUInteger offset = - ([self.string locationOfFirstNewlineBefore:location] + 1) % 4; + NSUInteger lineStart = [string locationOfFirstNewlineBefore:location] + 1; + if (location <= lineStart) + return NO; + + NSUInteger offset = (location - lineStart) % 4; if (offset == 0) offset = 4; - offset = offset > whitespaceCount ? whitespaceCount : 4; + if (whitespaceCount < offset) + offset = whitespaceCount; NSRange range = NSMakeRange(location - offset, offset); [self replaceCharactersInRange:range withString:@""];