From 7fd31824c8ca8c22798adab90f7292627f3e25fd Mon Sep 17 00:00:00 2001 From: David Wilson Date: Wed, 18 Jan 2017 14:47:33 -0800 Subject: [PATCH] Handle non-string return values from the user's prompt function This change adds a catch for the InvalidCastException when gathering the user's prompt string in the case where their prompt function returns a value other than a string. In this case a default prompt is returned. This change also improves the way we write prompts for the debugger. Instead of writing out the prompt each time the debugger stops, we write out the details about the breakpoint that was hit, if any. We also try to preserve any session context at the beginning of the prompt string like the remote computer name or process ID. Fixes #339. --- .../Session/PowerShellContext.cs | 76 ++++++++++++++++--- 1 file changed, 66 insertions(+), 10 deletions(-) diff --git a/src/PowerShellEditorServices/Session/PowerShellContext.cs b/src/PowerShellEditorServices/Session/PowerShellContext.cs index 86e912f2f..ce9f9abff 100644 --- a/src/PowerShellEditorServices/Session/PowerShellContext.cs +++ b/src/PowerShellEditorServices/Session/PowerShellContext.cs @@ -1212,7 +1212,7 @@ private void SetExecutionPolicy(ExecutionPolicy desiredExecutionPolicy) } } - private void WritePromptToHost(Func invokeAction) + private string GetPromptString(Func invokeAction) { string promptString = null; @@ -1228,19 +1228,29 @@ private void WritePromptToHost(Func invokeAction) LogLevel.Verbose, "Runtime exception occurred while executing prompt command:\r\n\r\n" + e.ToString()); } + catch (InvalidCastException e) + { + Logger.WriteException( + LogLevel.Verbose, + "Prompt function returned a result which is not a string.", + e); + } finally { - promptString = promptString ?? "PS >"; + promptString = promptString ?? "PS>"; } - this.WriteOutput( - Environment.NewLine, - false); - // Trim the '>' off the end of the prompt string to reduce // user confusion about where they can type. // TODO: Eventually put this behind a setting, #133 - promptString = promptString.TrimEnd(' ', '>', '\r', '\n'); + return promptString.TrimEnd(' ', '>', '\r', '\n'); + } + + private void WritePromptStringToHost(string promptString) + { + this.WriteOutput( + Environment.NewLine, + false); // Write the prompt string this.WriteOutput( @@ -1248,6 +1258,12 @@ private void WritePromptToHost(Func invokeAction) true); } + private void WritePromptToHost(Func invokeAction) + { + this.WritePromptStringToHost( + this.GetPromptString(invokeAction)); + } + private void WritePromptWithRunspace(Runspace runspace) { this.WritePromptToHost( @@ -1275,9 +1291,9 @@ private void WritePromptWithRunspace(Runspace runspace) }); } - private void WritePromptInDebugger() + private string GetPromptInDebugger() { - this.WritePromptToHost( + return this.GetPromptString( command => { return @@ -1286,6 +1302,46 @@ private void WritePromptInDebugger() }); } + private void WritePromptInDebugger(DebuggerStopEventArgs eventArgs = null) + { + string promptPrefix = string.Empty; + string promptString = this.GetPromptInDebugger(); + + if (eventArgs != null) + { + // Is there a prompt prefix worth keeping? + const string promptSeparator = "]: "; + if (promptString != null && promptString[0] == ('[')) + { + int separatorIndex = promptString.LastIndexOf(promptSeparator); + if (separatorIndex > 0) + { + promptPrefix = + promptString.Substring( + 0, + separatorIndex + promptSeparator.Length); + } + } + + // This was called from OnDebuggerStop, write a different prompt + if (eventArgs.Breakpoints.Count > 0) + { + // The breakpoint classes have nice ToString output so use that + promptString = $"Hit {eventArgs.Breakpoints[0].ToString()}"; + } + else + { + // TODO: What do we display when we don't know why we stopped? + promptString = null; + } + } + + if (promptString != null) + { + this.WritePromptStringToHost(promptPrefix + promptString); + } + } + private void WritePromptWithNestedPipeline() { using (var pipeline = this.CurrentRunspace.Runspace.CreateNestedPipeline()) @@ -1418,7 +1474,7 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e) } // Write out the debugger prompt - this.WritePromptInDebugger(); + this.WritePromptInDebugger(e); // Raise the event for the debugger service this.DebuggerStop?.Invoke(sender, e);