diff options
author | Bibiko <bibiko@eva.mpg.de> | 2009-06-05 22:12:36 +0000 |
---|---|---|
committer | Bibiko <bibiko@eva.mpg.de> | 2009-06-05 22:12:36 +0000 |
commit | 549cd7c007abef5d96c1d0fc3194df1db1f09358 (patch) | |
tree | 558cb9ef24fbd6e1e9172523d173eeb630573c12 /Source | |
parent | 1f938f1b54551a8418315301f7c5a2bca080e04d (diff) | |
download | sequelpro-549cd7c007abef5d96c1d0fc3194df1db1f09358.tar.gz sequelpro-549cd7c007abef5d96c1d0fc3194df1db1f09358.tar.bz2 sequelpro-549cd7c007abef5d96c1d0fc3194df1db1f09358.zip |
• added "Select Enclosing Brackets" to each NSTextView (⇧⌘B)
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPTextViewAdditions.h | 1 | ||||
-rw-r--r-- | Source/SPTextViewAdditions.m | 86 |
2 files changed, 87 insertions, 0 deletions
diff --git a/Source/SPTextViewAdditions.h b/Source/SPTextViewAdditions.h index 2e0753cc..d1fa91d8 100644 --- a/Source/SPTextViewAdditions.h +++ b/Source/SPTextViewAdditions.h @@ -30,6 +30,7 @@ - (IBAction)selectCurrentWord:(id)sender; - (IBAction)selectCurrentLine:(id)sender; +- (IBAction)selectEnclosingBrackets:(id)sender; - (IBAction)doSelectionUpperCase:(id)sender; - (IBAction)doSelectionLowerCase:(id)sender; - (IBAction)doSelectionTitleCase:(id)sender; diff --git a/Source/SPTextViewAdditions.m b/Source/SPTextViewAdditions.m index 134b93ca..34cbbab8 100644 --- a/Source/SPTextViewAdditions.m +++ b/Source/SPTextViewAdditions.m @@ -95,6 +95,92 @@ } /* + * + */ +- (IBAction)selectEnclosingBrackets:(id)sender +{ + long caretPosition = [self selectedRange].location; + long stringLength = [[self string] length]; + unichar co, cc; + + if(caretPosition == 0 || caretPosition >= stringLength) return; + + long pcnt = 0; + long bcnt = 0; + long scnt = 0; + + long i; + + // look for the first non-balanced closing bracket + for(i=caretPosition; i<stringLength; i++) { + switch([[self string] characterAtIndex:i]) { + case ')': + if(!pcnt) { + co='(';cc=')'; + i=stringLength; + } + pcnt++; break; + case '(': pcnt--; break; + case ']': + if(!bcnt) { + co='[';cc=']'; + i=stringLength; + } + bcnt++; break; + case '[': bcnt--; break; + case '}': + if(!scnt) { + co='{';cc='}'; + i=stringLength; + } + scnt++; break; + case '{': scnt--; break; + } + } + + long start = -1; + long end = -1; + long bracketCounter = 0; + + if([[self string] characterAtIndex:caretPosition] == cc) + bracketCounter--; + if([[self string] characterAtIndex:caretPosition] == co) + bracketCounter++; + + for(i=caretPosition; i>=0; i--) { + if([[self string] characterAtIndex:i] == co) { + if(!bracketCounter) { + start = i; + break; + } + bracketCounter--; + } + if([[self string] characterAtIndex:i] == cc) { + bracketCounter++; + } + } + if(start < 0 ) return; + + bracketCounter = 0; + for(i=caretPosition; i<stringLength; i++) { + if([[self string] characterAtIndex:i] == co) { + bracketCounter++; + } + if([[self string] characterAtIndex:i] == cc) { + if(!bracketCounter) { + end = i+1; + break; + } + bracketCounter--; + } + } + if(end < 0 || bracketCounter || end-start < 1) return; + + [self setSelectedRange:NSMakeRange(start, end-start)]; + +} + +/* * Change selection or current word to upper case and preserves the selection. */ - (IBAction)doSelectionUpperCase:(id)sender |