aboutsummaryrefslogtreecommitdiffstats
path: root/Source/CMTextView.m
diff options
context:
space:
mode:
authorrowanbeentje <rowan@beent.je>2009-03-30 21:01:21 +0000
committerrowanbeentje <rowan@beent.je>2009-03-30 21:01:21 +0000
commit6289788fd68bec01c4da1d838b15efbf91e35b39 (patch)
treede3e88f1b461bae3e38305613c3594d065f402a1 /Source/CMTextView.m
parent502a20d95b674f2477c85f2056af26a22838781f (diff)
downloadsequelpro-6289788fd68bec01c4da1d838b15efbf91e35b39.tar.gz
sequelpro-6289788fd68bec01c4da1d838b15efbf91e35b39.tar.bz2
sequelpro-6289788fd68bec01c4da1d838b15efbf91e35b39.zip
- Update the custom query view to preserve indentation level on newlines, to improve editor behaviour.
Diffstat (limited to 'Source/CMTextView.m')
-rw-r--r--Source/CMTextView.m43
1 files changed, 43 insertions, 0 deletions
diff --git a/Source/CMTextView.m b/Source/CMTextView.m
index d62783da..a09b3125 100644
--- a/Source/CMTextView.m
+++ b/Source/CMTextView.m
@@ -37,6 +37,49 @@ YY_BUFFER_STATE yy_scan_string (const char *);
@implementation CMTextView
+
+/*
+ * Handle special commands - see NSResponder.h for a sample list.
+ * This subclass currently handles insertNewline: in order to preserve indentation
+ * when adding newlines.
+ */
+- (void) doCommandBySelector:(SEL)aSelector
+{
+
+ // Handle newlines, adding any indentation found on the current line to the new line.
+ if (aSelector == @selector(insertNewline:)) {
+ NSString *textViewString = [[self textStorage] string];
+ NSString *currentLine, *indentString = nil;
+ NSScanner *whitespaceScanner;
+ NSUInteger lineStart, lineEnd;
+
+ // Extract the current line based on the text caret or selection start position
+ [textViewString getLineStart:&lineStart end:NULL contentsEnd:&lineEnd forRange:NSMakeRange([self selectedRange].location, 0)];
+ currentLine = [[NSString alloc] initWithString:[textViewString substringWithRange:NSMakeRange(lineStart, lineEnd - lineStart)]];
+
+ // Scan all indentation characters on the line into a string
+ whitespaceScanner = [[NSScanner alloc] initWithString:currentLine];
+ [whitespaceScanner setCharactersToBeSkipped:nil];
+ [whitespaceScanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:&indentString];
+ [whitespaceScanner release];
+ [currentLine release];
+
+ // Always add the newline, whether or not we want to indent the next line
+ [self insertNewline:self];
+
+ // Replicate the indentation on the previous line if one was found.
+ if (indentString) [self insertText:indentString];
+
+ // Return to avoid the original implementation, preventing double linebreaks
+ return;
+ }
+ [super doCommandBySelector:aSelector];
+}
+
+
+/*
+ * Handle autocompletion, returning a list of suggested completions for the supplied character range.
+ */
- (NSArray *)completionsForPartialWordRange:(NSRange)charRange indexOfSelectedItem:(int *)index
{