From 15796e40fba251a00197c66f2ecf04cbf086a034 Mon Sep 17 00:00:00 2001 From: Bibiko Date: Tue, 19 May 2009 09:29:00 +0000 Subject: =?UTF-8?q?=E2=80=A2=20skip=20auto-pairing=20if=20the=20caret=20is?= =?UTF-8?q?=20adjoined=20to=20an=20alphanumeric=20character=20except=20if?= =?UTF-8?q?=20the=20inserted=20character=20will=20be=20a=20=E2=80=9C(?= =?UTF-8?q?=E2=80=9D=20and=20the=20caret=20is=20located=20at=20the=20end?= =?UTF-8?q?=20of=20a=20string=20then=20process=20the=20auto-pairing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CMTextView.h | 1 + Source/CMTextView.m | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Source/CMTextView.h b/Source/CMTextView.h index 3ffa5c19..e86dc67f 100644 --- a/Source/CMTextView.h +++ b/Source/CMTextView.h @@ -52,6 +52,7 @@ - (BOOL) isNextCharMarkedBy:(id)attribute withValue:(id)aValue; - (BOOL) areAdjacentCharsLinked; +- (BOOL) isCaretAdjacentToAlphanumCharWithInsertionOf:(unichar)aChar; - (BOOL) wrapSelectionWithPrefix:(NSString *)prefix suffix:(NSString *)suffix; - (BOOL) shiftSelectionRight; - (BOOL) shiftSelectionLeft; diff --git a/Source/CMTextView.m b/Source/CMTextView.m index 6f379f90..8803af2e 100644 --- a/Source/CMTextView.m +++ b/Source/CMTextView.m @@ -123,6 +123,32 @@ YY_BUFFER_STATE yy_scan_string (const char *); return NO; } +/* + * Checks if the caret adjoins to an alphanumeric char |word or word| or wo|rd + * Exception for word| and char is a “(” to allow e.g. auto-pairing () for functions + */ +- (BOOL) isCaretAdjacentToAlphanumCharWithInsertionOf:(unichar)aChar +{ + unsigned int caretPosition = [self selectedRange].location; + NSCharacterSet *alphanum = [NSCharacterSet alphanumericCharacterSet]; + BOOL leftIsAlphanum = NO; + BOOL rightIsAlphanum = NO; + BOOL charIsOpenBracket = (aChar == '('); + + // Check previous/next character for being alphanum + // @try block for bounds checking + @try + { + leftIsAlphanum = [alphanum characterIsMember:[[self string] characterAtIndex:caretPosition-1]] && !charIsOpenBracket; + } @catch(id ae) { } + @try { + rightIsAlphanum= [alphanum characterIsMember:[[self string] characterAtIndex:caretPosition]]; + + } @catch(id ae) { } + + return (leftIsAlphanum ^ rightIsAlphanum || leftIsAlphanum && rightIsAlphanum); +} + /* * Checks if the caret is wrapped by auto-paired characters. * e.g. [| := caret]: "|" @@ -328,11 +354,13 @@ YY_BUFFER_STATE yy_scan_string (const char *); return; } - // If the caret is inside a text string, without any selection, skip autopairing. + // If the caret is inside a text string, without any selection, and not adjoined to an alphanumeric char + // (exception for '(' ) skip autopairing. // There is one exception to this - if the caret is before a linked pair character, // processing continues in order to check whether the next character should be jumped // over; e.g. [| := caret]: "foo|" and press " => only caret will be moved "foo"| - if(![self isNextCharMarkedBy:kAPlinked withValue:kAPval] && [self isNextCharMarkedBy:kLEXToken withValue:kLEXTokenValue] && ![self selectedRange].length) { + if( ([self isCaretAdjacentToAlphanumCharWithInsertionOf:insertedCharacter] && ![self isNextCharMarkedBy:kAPlinked withValue:kAPval] && ![self selectedRange].length) + || (![self isNextCharMarkedBy:kAPlinked withValue:kAPval] && [self isNextCharMarkedBy:kLEXToken withValue:kLEXTokenValue] && ![self selectedRange].length)) { [super keyDown:theEvent]; return; } -- cgit v1.2.3