Skip to content
Closed
91 changes: 69 additions & 22 deletions src/Microsoft.PowerShell.Linux.Host/main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -360,14 +360,8 @@ private void ExecuteHelper(string cmd, object input)
{
this.currentPowerShell.Runspace = this.myRunSpace;

if (incompleteLine)
{
this.currentPowerShell.AddScript(partialLine + cmd);
}
else
{
this.currentPowerShell.AddScript(cmd);
}
string fullCommand = incompleteLine ? (partialLine + cmd) : cmd;
this.currentPowerShell.AddScript(fullCommand);
incompleteLine = false;

// Add the default outputter to the end of the pipe and then call the
Expand All @@ -394,11 +388,7 @@ private void ExecuteHelper(string cmd, object input)
catch (IncompleteParseException)
{
incompleteLine = true;
cmd = cmd.Trim();
partialLine += cmd;

partialLine += System.Environment.NewLine;

partialLine = $"{partialLine}{cmd}{System.Environment.NewLine}";
}

finally
Expand Down Expand Up @@ -526,7 +516,6 @@ private void HandleControlC(object sender, ConsoleCancelEventArgs e)
this.currentPowerShell.Stop();
}
}

e.Cancel = true;
}
catch (Exception exception)
Expand All @@ -546,6 +535,8 @@ internal void Run()
Console.CancelKeyPress += new ConsoleCancelEventHandler(this.HandleControlC);
//Console.TreatControlCAsInput = false;

string initialCommand = String.Empty;

// Read commands and run them until the ShouldExit flag is set by
// the user calling "exit".
while (!this.ShouldExit && this.myHost.Runspace != null)
Expand All @@ -565,8 +556,25 @@ internal void Run()
}

this.myHost.UI.Write(ConsoleColor.White, Console.BackgroundColor, prompt);
string cmd = consoleReadLine.Read(this.myHost.Runspace, false);
this.Execute(cmd);

ConsoleReadLine.ReadResult result = consoleReadLine.Read(this.myHost.Runspace, false, initialCommand);

switch(result.state)
{
case ConsoleReadLine.ReadResult.State.Abort:
incompleteLine = false;
partialLine = string.Empty;
initialCommand = String.Empty;
break;
case ConsoleReadLine.ReadResult.State.Redraw:
initialCommand = result.command;
break;
case ConsoleReadLine.ReadResult.State.Complete:
default:
this.Execute(result.command);
initialCommand = String.Empty;
break;
}
}
}

Expand All @@ -582,12 +590,31 @@ private void HandleDebuggerStopEvent(object sender, DebuggerStopEventArgs args)

WriteDebuggerStopMessages(args);

string initialCommand = String.Empty;

// loop to process Debugger commands.
while (resumeAction == null)
{
Console.Write("[DBG] PS >> ");
string command = consoleReadLine.Read(this.myHost.Runspace, true);
Console.WriteLine();
string prompt = incompleteLine ? ">> " : "[DBG] PS >> ";
Console.Write(prompt);

ConsoleReadLine.ReadResult result = consoleReadLine.Read(this.myHost.Runspace, true, initialCommand);

switch(result.state)
{
case ConsoleReadLine.ReadResult.State.Abort:
incompleteLine = false;
partialLine = string.Empty;
initialCommand = String.Empty;
continue;
case ConsoleReadLine.ReadResult.State.Redraw:
initialCommand = result.command;
continue;
case ConsoleReadLine.ReadResult.State.Complete:
default:
initialCommand = String.Empty;
break;
}

// Stream output from command processing to console.
var output = new PSDataCollection<PSObject>();
Expand All @@ -606,10 +633,30 @@ private void HandleDebuggerStopEvent(object sender, DebuggerStopEventArgs args)
// command or script. The returned DebuggerCommandResults object will indicate
// whether the command was evaluated by the debugger and if the debugger should
// be released with a specific resume action.

PSCommand psCommand = new PSCommand();
psCommand.AddScript(command).AddCommand("Out-String").AddParameter("Stream", true);
DebuggerCommandResults results = debugger.ProcessCommand(psCommand, output);
if (results.ResumeAction != null)

string fullCommand = incompleteLine ? (partialLine + result.command) : result.command;
psCommand.AddScript(fullCommand).AddCommand("Out-String").AddParameter("Stream", true);
incompleteLine = false;

DebuggerCommandResults results = null;
try
{
results = debugger.ProcessCommand(psCommand, output);
}
catch (IncompleteParseException)
{
incompleteLine = true;
partialLine = $"{partialLine}{result.command}{System.Environment.NewLine}";
}

if (!incompleteLine)
{
partialLine = string.Empty;
}

if (!incompleteLine && results.ResumeAction != null)
{
resumeAction = results.ResumeAction;
}
Expand Down
Loading