diff options
Diffstat (limited to 'Source')
-rw-r--r-- | Source/SPBundleCommandTextView.h | 9 | ||||
-rw-r--r-- | Source/SPBundleCommandTextView.m | 150 | ||||
-rw-r--r-- | Source/SPBundleEditorController.m | 13 | ||||
-rw-r--r-- | Source/SPCopyTable.m | 9 | ||||
-rw-r--r-- | Source/SPStringAdditions.m | 9 | ||||
-rw-r--r-- | Source/SPTextView.m | 9 | ||||
-rw-r--r-- | Source/SPTextViewAdditions.m | 9 |
7 files changed, 172 insertions, 36 deletions
diff --git a/Source/SPBundleCommandTextView.h b/Source/SPBundleCommandTextView.h index e39ec1dd..fb9cef62 100644 --- a/Source/SPBundleCommandTextView.h +++ b/Source/SPBundleCommandTextView.h @@ -22,13 +22,22 @@ // // More info at <http://code.google.com/p/sequel-pro/> +#import "NoodleLineNumberView.h" + @interface SPBundleCommandTextView : NSTextView { + + IBOutlet NSScrollView *commandScrollView; + + NSUserDefaults *prefs; + BOOL textWasChanged; + NoodleLineNumberView *lineNumberView; } - (NSUInteger)characterIndexOfPoint:(NSPoint)aPoint; - (void)insertFileContentOfFile:(NSString *)aPath; - (void)saveChangedFontInUserDefaults; +- (void) setTabStops; @end diff --git a/Source/SPBundleCommandTextView.m b/Source/SPBundleCommandTextView.m index a840101a..72618dbc 100644 --- a/Source/SPBundleCommandTextView.m +++ b/Source/SPBundleCommandTextView.m @@ -37,6 +37,66 @@ return self; } +- (void)dealloc +{ + + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [prefs removeObserver:self forKeyPath:SPCustomQueryEditorTabStopWidth]; + [prefs release]; + [lineNumberView release]; +} + +- (void) awakeFromNib +{ + + prefs = [[NSUserDefaults standardUserDefaults] retain]; + + [prefs addObserver:self forKeyPath:SPCustomQueryEditorTabStopWidth options:NSKeyValueObservingOptionNew context:NULL]; + + if([[NSUserDefaults standardUserDefaults] dataForKey:@"BundleEditorFont"]) { + NSFont *nf = [NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"BundleEditorFont"]]; + [self setFont:nf]; + } + + lineNumberView = [[NoodleLineNumberView alloc] initWithScrollView:commandScrollView]; + [commandScrollView setVerticalRulerView:lineNumberView]; + [commandScrollView setHasHorizontalRuler:NO]; + [commandScrollView setHasVerticalRuler:YES]; + [commandScrollView setRulersVisible:YES]; + + // Re-define tab stops for a better editing + [self setTabStops]; + +} + +- (void)drawRect:(NSRect)rect +{ + // Draw background only for screen display but not while printing + if([NSGraphicsContext currentContextDrawingToScreen]) { + + // Draw textview's background since due to the snippet highlighting we're responsible for it. + [[NSColor whiteColor] setFill]; + NSRectFill(rect); + + if (![self selectedRange].length && [[self string] length]) { + NSRange r = [[self string] lineRangeForRange:NSMakeRange([self selectedRange].location, 0)]; + NSUInteger rectCount; + [[self textStorage] ensureAttributesAreFixedInRange:r]; + NSRectArray queryRects = [[self layoutManager] rectArrayForCharacterRange: r + withinSelectedCharacterRange: r + inTextContainer: [self textContainer] + rectCount: &rectCount ]; + [[NSColor colorWithCalibratedRed:0.95f green:0.95f blue:0.95f alpha:1.0f] setFill]; + NSRectFillListUsingOperation(queryRects, rectCount, NSCompositeSourceOver); + } + } + + [super drawRect:rect]; + +} + +#pragma mark - + - (IBAction)undo:(id)sender { textWasChanged = NO; @@ -75,24 +135,53 @@ [super cut:sender]; } -/** - * Validate undo and redo menu items - */ -- (BOOL)validateMenuItem:(NSMenuItem *)menuItem +- (void) setTabStops { - - if ([menuItem action] == @selector(undo:)) { - return ([[self undoManager] canUndo]); + NSFont *tvFont = [self font]; + NSInteger i; + NSTextTab *aTab; + NSMutableArray *myArrayOfTabs; + NSMutableParagraphStyle *paragraphStyle; + + BOOL oldEditableStatus = [self isEditable]; + [self setEditable:YES]; + + NSInteger tabStopWidth = [prefs integerForKey:SPCustomQueryEditorTabStopWidth]; + if(tabStopWidth < 1) tabStopWidth = 1; + + float tabWidth = NSSizeToCGSize([[NSString stringWithString:@" "] sizeWithAttributes:[NSDictionary dictionaryWithObject:tvFont forKey:NSFontAttributeName]]).width; + tabWidth = (float)tabStopWidth * tabWidth; + + NSInteger numberOfTabs = 256/tabStopWidth; + myArrayOfTabs = [NSMutableArray arrayWithCapacity:numberOfTabs]; + aTab = [[NSTextTab alloc] initWithType:NSLeftTabStopType location:tabWidth]; + [myArrayOfTabs addObject:aTab]; + [aTab release]; + for(i=1; i<numberOfTabs; i++) { + aTab = [[NSTextTab alloc] initWithType:NSLeftTabStopType location:tabWidth + ((float)i * tabWidth)]; + [myArrayOfTabs addObject:aTab]; + [aTab release]; } - if ([menuItem action] == @selector(redo:)) { - return ([[self undoManager] canRedo]); + paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; + [paragraphStyle setTabStops:myArrayOfTabs]; + // Soft wrapped lines are indented slightly + [paragraphStyle setHeadIndent:4.0]; + + NSMutableDictionary *textAttributes = [[[NSMutableDictionary alloc] initWithCapacity:1] autorelease]; + [textAttributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName]; + + NSRange range = NSMakeRange(0, [[self textStorage] length]); + if ([self shouldChangeTextInRange:range replacementString:nil]) { + [[self textStorage] setAttributes:textAttributes range: range]; + [self didChangeText]; } - return YES; -} + [self setTypingAttributes:textAttributes]; + [self setDefaultParagraphStyle:paragraphStyle]; + [self setFont:tvFont]; -- (void)textDidChange:(NSNotification *)aNotification -{ - textWasChanged = YES; + [self setEditable:oldEditableStatus]; + + [paragraphStyle release]; } - (void)keyDown:(NSEvent *)theEvent @@ -162,6 +251,8 @@ } +#pragma mark - + /* * Insert the content of a dragged file path or if ⌘ is pressed * while dragging insert the file path @@ -244,6 +335,9 @@ [self insertFileContentOfFile:[sheet helpAnchor]]; } + +#pragma mark - + /* * Convert a NSPoint, usually the mouse location, to * a character index of the text view. @@ -342,11 +436,37 @@ NSLog(@"%@ ‘%@’.", NSLocalizedString(@"Couldn't read the file content of", @"Couldn't read the file content of"), aPath); } +#pragma mark - + +/** + * Validate undo and redo menu items + */ +- (BOOL)validateMenuItem:(NSMenuItem *)menuItem +{ + + if ([menuItem action] == @selector(undo:)) { + return ([[self undoManager] canUndo]); + } + if ([menuItem action] == @selector(redo:)) { + return ([[self undoManager] canRedo]); + } + return YES; +} + +#pragma mark - + +- (void)textDidChange:(NSNotification *)aNotification +{ + textWasChanged = YES; +} + +#pragma mark - + // Store the font in the prefs for selected delegates only - (void)saveChangedFontInUserDefaults { if([[[[self delegate] class] description] isEqualToString:@"SPBundleEditorController"]) - [[NSUserDefaults standardUserDefaults] setObject:[NSArchiver archivedDataWithRootObject:[self font]] forKey:@"FieldEditorSheetFont"]; + [prefs setObject:[NSArchiver archivedDataWithRootObject:[self font]] forKey:@"BundleEditorFont"]; } // Action receiver for a font change in the font panel diff --git a/Source/SPBundleEditorController.m b/Source/SPBundleEditorController.m index 829cff3e..8587082b 100644 --- a/Source/SPBundleEditorController.m +++ b/Source/SPBundleEditorController.m @@ -205,11 +205,6 @@ [keyEquivalentField setCanCaptureGlobalHotKeys:YES]; - if([[NSUserDefaults standardUserDefaults] dataForKey:@"BundleEditorFont"]) { - NSFont *nf = [NSUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"BundleEditorFont"]]; - [commandTextView setFont:nf]; - } - } #pragma mark - @@ -982,6 +977,14 @@ #pragma mark NSTextView delegates /** + * Update command text view for highlighting the current edited line + */ +- (void)textViewDidChangeSelection:(NSNotification *)aNotification +{ + [commandTextView setNeedsDisplay:YES]; +} + +/** * Traps any editing in editTextView to allow undo grouping only if the text buffer was really changed. * Inform the run loop delayed for larger undo groups. */ diff --git a/Source/SPCopyTable.m b/Source/SPCopyTable.m index 48aa62f7..1bfc2277 100644 --- a/Source/SPCopyTable.m +++ b/Source/SPCopyTable.m @@ -840,8 +840,9 @@ NSInteger MENU_EDIT_COPY_AS_SQL = 2003; NSString *inputAction = @""; NSString *inputFallBackAction = @""; NSError *err = nil; + NSString *bundleInputFilePath = [NSString stringWithFormat:@"%@_%@", SPBundleTaskInputFilePath, [NSString stringWithNewUUID]]; - [[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskInputFilePath error:nil]; + [[NSFileManager defaultManager] removeItemAtPath:bundleInputFilePath error:nil]; if([cmdData objectForKey:SPBundleFileInputSourceKey]) inputAction = [[cmdData objectForKey:SPBundleFileInputSourceKey] lowercaseString]; @@ -850,7 +851,7 @@ NSInteger MENU_EDIT_COPY_AS_SQL = 2003; NSMutableDictionary *env = [NSMutableDictionary dictionary]; [env setObject:[infoPath stringByDeletingLastPathComponent] forKey:@"SP_BUNDLE_PATH"]; - [env setObject:SPBundleTaskInputFilePath forKey:@"SP_BUNDLE_INPUT_FILE"]; + [env setObject:bundleInputFilePath forKey:@"SP_BUNDLE_INPUT_FILE"]; if([[self delegate] respondsToSelector:@selector(usedQuery)] && [[self delegate] usedQuery]) [env setObject:[[self delegate] usedQuery] forKey:@"SP_USED_QUERY_FOR_TABLE"]; @@ -877,7 +878,7 @@ NSInteger MENU_EDIT_COPY_AS_SQL = 2003; } if(input == nil) input = @""; - [input writeToFile:SPBundleTaskInputFilePath + [input writeToFile:bundleInputFilePath atomically:YES encoding:NSUTF8StringEncoding error:&inputFileError]; @@ -892,7 +893,7 @@ NSInteger MENU_EDIT_COPY_AS_SQL = 2003; NSString *output = [cmd runBashCommandWithEnvironment:env atCurrentDirectoryPath:nil error:&err]; - [[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskInputFilePath error:nil]; + [[NSFileManager defaultManager] removeItemAtPath:bundleInputFilePath error:nil]; if(err == nil && output && [cmdData objectForKey:SPBundleFileOutputActionKey]) { if([[cmdData objectForKey:SPBundleFileOutputActionKey] length] diff --git a/Source/SPStringAdditions.m b/Source/SPStringAdditions.m index 816dc362..09f37866 100644 --- a/Source/SPStringAdditions.m +++ b/Source/SPStringAdditions.m @@ -421,7 +421,8 @@ NSMutableArray *scriptHeaderArguments = [NSMutableArray array]; NSString *scriptPath = @""; - NSString *stdoutFilePath = @"/tmp/SP_BUNDLE_OUTPUT_FILE"; + NSString *stdoutFilePath = [NSString stringWithFormat:@"/tmp/SP_BUNDLE_OUTPUT_FILE_%@", [NSString stringWithNewUUID]]; + NSString *scriptFilePath = [NSString stringWithFormat:@"%@_%@", SPBundleTaskScriptCommandFilePath, [NSString stringWithNewUUID]]; [[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskScriptCommandFilePath error:nil]; [[NSFileManager defaultManager] removeItemAtPath:stdoutFilePath error:nil]; @@ -443,10 +444,10 @@ if([scriptPath hasPrefix:@"/"] && [[NSFileManager defaultManager] fileExistsAtPath:scriptPath isDirectory:&isDir] && !isDir) { NSString *script = [self substringWithRange:NSMakeRange(NSMaxRange(firstLineRange), [self length] - NSMaxRange(firstLineRange))]; NSError *writeError = nil; - [script writeToFile:SPBundleTaskScriptCommandFilePath atomically:YES encoding:NSUTF8StringEncoding error:writeError]; + [script writeToFile:scriptFilePath atomically:YES encoding:NSUTF8StringEncoding error:writeError]; if(writeError == nil) { redirectForScript = YES; - [scriptHeaderArguments addObject:SPBundleTaskScriptCommandFilePath]; + [scriptHeaderArguments addObject:scriptFilePath]; } else { NSBeep(); NSLog(@"Couldn't write script file."); @@ -532,7 +533,7 @@ } if(redirectForScript) - [[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskScriptCommandFilePath error:nil]; + [[NSFileManager defaultManager] removeItemAtPath:scriptFilePath error:nil]; // If return from bash re-activate Sequel Pro [NSApp activateIgnoringOtherApps:YES]; diff --git a/Source/SPTextView.m b/Source/SPTextView.m index 0af0b2ff..b7e39a39 100644 --- a/Source/SPTextView.m +++ b/Source/SPTextView.m @@ -3453,10 +3453,11 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) NSString *inputAction = @""; NSString *inputFallBackAction = @""; NSError *err = nil; + NSString *bundleInputFilePath = [NSString stringWithFormat:@"%@_%@", SPBundleTaskInputFilePath, [NSString stringWithNewUUID]]; NSRange currentWordRange, currentQueryRange, currentSelectionRange, currentLineRange; - [[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskInputFilePath error:nil]; + [[NSFileManager defaultManager] removeItemAtPath:bundleInputFilePath error:nil]; if([cmdData objectForKey:SPBundleFileInputSourceKey]) inputAction = [[cmdData objectForKey:SPBundleFileInputSourceKey] lowercaseString]; @@ -3494,7 +3495,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) NSMutableDictionary *env = [NSMutableDictionary dictionary]; [env setObject:[infoPath stringByDeletingLastPathComponent] forKey:@"SP_BUNDLE_PATH"]; - [env setObject:SPBundleTaskInputFilePath forKey:@"SP_BUNDLE_INPUT_FILE"]; + [env setObject:bundleInputFilePath forKey:@"SP_BUNDLE_INPUT_FILE"]; if(currentSelectionRange.length) [env setObject:[[self string] substringWithRange:currentSelectionRange] forKey:@"SP_SELECTED_TEXT"]; @@ -3510,7 +3511,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) NSError *inputFileError = nil; NSString *input = [NSString stringWithString:[[self string] substringWithRange:replaceRange]]; - [input writeToFile:SPBundleTaskInputFilePath + [input writeToFile:bundleInputFilePath atomically:YES encoding:NSUTF8StringEncoding error:&inputFileError]; @@ -3525,7 +3526,7 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) NSString *output = [cmd runBashCommandWithEnvironment:env atCurrentDirectoryPath:nil error:&err]; - [[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskInputFilePath error:nil]; + [[NSFileManager defaultManager] removeItemAtPath:bundleInputFilePath error:nil]; if(err == nil && output && [cmdData objectForKey:SPBundleFileOutputActionKey]) { if([[cmdData objectForKey:SPBundleFileOutputActionKey] length] diff --git a/Source/SPTextViewAdditions.m b/Source/SPTextViewAdditions.m index fdf2318c..ae07d289 100644 --- a/Source/SPTextViewAdditions.m +++ b/Source/SPTextViewAdditions.m @@ -525,10 +525,11 @@ NSString *inputAction = @""; NSString *inputFallBackAction = @""; NSError *err = nil; + NSString *bundleInputFilePath = [NSString stringWithFormat:@"%@_%@", SPBundleTaskInputFilePath, [NSString stringWithNewUUID]]; NSRange currentWordRange, currentSelectionRange, currentLineRange; - [[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskInputFilePath error:nil]; + [[NSFileManager defaultManager] removeItemAtPath:bundleInputFilePath error:nil]; if([cmdData objectForKey:SPBundleFileInputSourceKey]) inputAction = [[cmdData objectForKey:SPBundleFileInputSourceKey] lowercaseString]; @@ -559,7 +560,7 @@ NSMutableDictionary *env = [NSMutableDictionary dictionary]; [env setObject:[infoPath stringByDeletingLastPathComponent] forKey:@"SP_BUNDLE_PATH"]; - [env setObject:SPBundleTaskInputFilePath forKey:@"SP_BUNDLE_INPUT_FILE"]; + [env setObject:bundleInputFilePath forKey:@"SP_BUNDLE_INPUT_FILE"]; if(currentSelectionRange.length) [env setObject:[[self string] substringWithRange:currentSelectionRange] forKey:@"SP_SELECTED_TEXT"]; @@ -572,7 +573,7 @@ NSError *inputFileError = nil; NSString *input = [NSString stringWithString:[[self string] substringWithRange:replaceRange]]; - [input writeToFile:SPBundleTaskInputFilePath + [input writeToFile:bundleInputFilePath atomically:YES encoding:NSUTF8StringEncoding error:&inputFileError]; @@ -587,7 +588,7 @@ NSString *output = [cmd runBashCommandWithEnvironment:env atCurrentDirectoryPath:nil error:&err]; - [[NSFileManager defaultManager] removeItemAtPath:SPBundleTaskInputFilePath error:nil]; + [[NSFileManager defaultManager] removeItemAtPath:bundleInputFilePath error:nil]; if(err == nil && output && [cmdData objectForKey:SPBundleFileOutputActionKey]) { if([[cmdData objectForKey:SPBundleFileOutputActionKey] length] |