From 16b8956e8e903b180f5d1afc956b87882ee851df Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sun, 9 Mar 2008 13:13:14 -0400 Subject: [PATCH] Adding a method to scroll to a given line in the BSSourceView * Source/BSSourceView.m: ([BSSourceView scrollToLine:]): New method that scrolls to a specific line in the NSTextView * Source/BSSourceView.h: ditto * Source/DebuggerWindowController.m: ([DebuggerWindowController updateSourceViewer]): Removing the scrolling logic that used to be here --- Source/BSSourceView.h | 2 ++ Source/BSSourceView.m | 18 ++++++++++++++++++ Source/DebuggerWindowController.m | 19 +------------------ 3 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Source/BSSourceView.h b/Source/BSSourceView.h index 3a44765..03a306c 100644 --- a/Source/BSSourceView.h +++ b/Source/BSSourceView.h @@ -29,4 +29,6 @@ @property(readwrite, assign) BSSourceViewTextView *textView; @property(readwrite, assign) NSScrollView *scrollView; +- (void)scrollToLine:(int)line; + @end diff --git a/Source/BSSourceView.m b/Source/BSSourceView.m index be5a15a..30214fe 100644 --- a/Source/BSSourceView.m +++ b/Source/BSSourceView.m @@ -44,6 +44,24 @@ return YES; } +/** + * Tells the text view to scroll to a certain line + */ +- (void)scrollToLine:(int)line +{ + // go through the document until we find the NSRange for the line we want + int rangeIndex = 0; + for (int i = 0; i < line; i++) + { + rangeIndex = NSMaxRange([[textView string] lineRangeForRange:NSMakeRange(rangeIndex, 0)]); + } + + // now get the true start/end markers for it + unsigned lineStart, lineEnd; + [[textView string] getLineStart:&lineStart end:NULL contentsEnd:&lineEnd forRange:NSMakeRange(rangeIndex - 1, 0)]; + [textView scrollRangeToVisible:[[textView string] lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]]; +} + /** * Setup all the subviews for the source metaview */ diff --git a/Source/DebuggerWindowController.m b/Source/DebuggerWindowController.m index f3d76dd..cca867e 100644 --- a/Source/DebuggerWindowController.m +++ b/Source/DebuggerWindowController.m @@ -219,24 +219,7 @@ NSString *text = [NSString stringWithContentsOfFile:filename]; [[sourceViewer textView] setString:text]; - // go through the document until we find the NSRange for the line we want - int destination = [[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue]; - int rangeIndex = 0; - for (int line = 0; line < destination; line++) - { - rangeIndex = NSMaxRange([text lineRangeForRange:NSMakeRange(rangeIndex, 0)]); - } - - // now get the true start/end markers for it - unsigned lineStart, lineEnd; - [text getLineStart:&lineStart end:NULL contentsEnd:&lineEnd forRange:NSMakeRange(rangeIndex - 1, 0)]; - NSRange lineRange = NSMakeRange(lineStart, lineEnd - lineStart); - - // colorize it so the user knows which line we're on in the stack - [[[sourceViewer textView] textStorage] setAttributes:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:[NSColor redColor], [NSColor yellowColor], nil] - forKeys:[NSArray arrayWithObjects:NSForegroundColorAttributeName, NSBackgroundColorAttributeName, nil]] - range:lineRange]; - [[sourceViewer textView] scrollRangeToVisible:[text lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]]; + [sourceViewer scrollToLine:[[[stack objectAtIndex:selection] valueForKey:@"lineno"] intValue]]; // make sure the font stays Monaco //[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]]; -- 2.22.5