From 26551572f464c6130930209b98b7112d83756546 Mon Sep 17 00:00:00 2001
From: Robert Sesek <rsesek@bluestatic.org>
Date: Thu, 22 Jan 2009 14:04:17 -0500
Subject: [PATCH] Highlighting of the currently debugged file will now get the
 source from Xdebug rather than reading local files

* Source/BSSourceView.m+h:
(setString:asFile:): New method that will highlight strings rather than files
(scrollToLine:): Change the sanity check to be based off of text in the textStorage, rather than if a file has been set
* Source/DebuggerController.m:
(updateSourceViewer): Use the new setString:asFile: instead of just setFile:
---
 Source/BSSourceView.h       |  1 +
 Source/BSSourceView.m       | 41 ++++++++++++++++++++++++++++++++-----
 Source/DebuggerController.m | 15 ++++++--------
 3 files changed, 43 insertions(+), 14 deletions(-)

diff --git a/Source/BSSourceView.h b/Source/BSSourceView.h
index abf3f18..a3342b7 100644
--- a/Source/BSSourceView.h
+++ b/Source/BSSourceView.h
@@ -38,6 +38,7 @@
 @property(readwrite, assign) id delegate;
 
 - (void)setFile:(NSString *)f;
+- (void)setString:(NSString *)source asFile:(NSString *)path;
 - (void)scrollToLine:(int)line;
 
 @end
diff --git a/Source/BSSourceView.m b/Source/BSSourceView.m
index e2290f7..2a99bb9 100644
--- a/Source/BSSourceView.m
+++ b/Source/BSSourceView.m
@@ -33,10 +33,12 @@
 	if (self = [super initWithFrame:frame])
 	{
 		[self setupViews];
-		[[NSNotificationCenter defaultCenter] addObserver:self
-												 selector:@selector(errorHighlightingFile:)
-													 name:NSFileHandleReadToEndOfFileCompletionNotification
-												   object:nil];
+		[[NSNotificationCenter defaultCenter]
+			addObserver:self
+			selector:@selector(errorHighlightingFile:)
+			name:NSFileHandleReadToEndOfFileCompletionNotification
+			object:nil
+		];
 	}
 	return self;
 }
@@ -99,6 +101,35 @@
 	}
 }
 
+/**
+ * Sets the contents of the SourceView via a string rather than loading from a path
+ */
+- (void)setString:(NSString *)source asFile:(NSString *)path
+{
+	// create the temp file
+	NSError *error = nil;
+	NSString *tmpPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"MacGDBpHighlighter"];
+	[source writeToFile:tmpPath atomically:NO encoding:NSASCIIStringEncoding error:&error];
+	if (error)
+	{
+		[textView setString:source];
+		return;
+	}
+	
+	// highlight the temporary file
+	[self setFile:tmpPath];
+	
+	// delete the temp file
+	[[NSFileManager defaultManager] removeItemAtPath:tmpPath error:NULL];
+	
+	// plop in our fake path so nobody knows the difference
+	if (path != file)
+	{
+		[file release];
+		file = [path copy];
+	}
+}
+
 /**
  * If an error occurs in reading the highlighted PHP source, this will merely set the string
  */
@@ -122,7 +153,7 @@
  */
 - (void)scrollToLine:(int)line
 {
-	if (![[NSFileManager defaultManager] fileExistsAtPath:file])
+	if ([[textView textStorage] length] == 0)
 		return;
 	
 	// go through the document until we find the NSRange for the line we want
diff --git a/Source/DebuggerController.m b/Source/DebuggerController.m
index e4972a7..36c98f2 100644
--- a/Source/DebuggerController.m
+++ b/Source/DebuggerController.m
@@ -218,29 +218,26 @@
 {
 	id selection = [stackArrayController selection];
 	if ([selection valueForKey:@"filename"] == NSNoSelectionMarker)
-	{
 		return;
-	}
 	
-	// get the filename and then set the text
+	// get the filename
 	NSString *filename = [selection valueForKey:@"filename"];
 	filename = [[NSURL URLWithString:filename] path];
 	if ([filename isEqualToString:@""])
-	{
 		return;
-	}
 	
+	// replace the source if necessary
 	if (![sourceViewer.file isEqualToString:filename])
-		[sourceViewer setFile:filename];
+	{
+		NSString *source = [selection valueForKey:@"source"];
+		[sourceViewer setString:source asFile:filename];
+	}
 	
 	int line = [[selection valueForKey:@"lineNumber"] intValue];
 	[sourceViewer setMarkedLine:line];
 	[sourceViewer scrollToLine:line];
 	
 	[[sourceViewer textView] display];
-	
-	// make sure the font stays Monaco
-	//[sourceViewer setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
 }
 
 /**
-- 
2.43.5