Skip to content

Commit 177d09f

Browse files
committed
Add parameter for echoing ExecuteScriptString
This change adds a new parameter to PowerShellContext.ExecuteScriptString which causes the executed script string to be echoed to the host. This is useful for "run selection" operations where some selected text is executed in the REPL.
1 parent b4fb564 commit 177d09f

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,8 @@ protected async Task HandleEvaluateRequest(
678678
{
679679
var results =
680680
await this.editorSession.PowerShellContext.ExecuteScriptString(
681-
evaluateParams.Expression);
681+
evaluateParams.Expression,
682+
true);
682683

683684
// Return an empty result since the result value is irrelevant
684685
// for this request in the LanguageServer

src/PowerShellEditorServices/Debugging/DebugService.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,21 @@ public async Task<VariableDetails> EvaluateExpression(string expressionString, i
238238
{
239239
var results =
240240
await this.powerShellContext.ExecuteScriptString(
241-
expressionString);
241+
expressionString,
242+
false);
242243

243244
// Since this method should only be getting invoked in the debugger,
244245
// we can assume that Out-String will be getting used to format results
245-
// of command executions into string output.
246+
// of command executions into string output. However, if null is returned
247+
// then pass null through so that no output gets displayed.
248+
string outputString =
249+
results != null ?
250+
string.Join(Environment.NewLine, results) :
251+
null;
246252

247253
return new VariableDetails(
248-
expressionString,
249-
string.Join(Environment.NewLine, results));
254+
expressionString,
255+
outputString);
250256
}
251257

252258
/// <summary>

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,30 @@ public Task ExecuteCommand(PSCommand psCommand)
377377
/// </summary>
378378
/// <param name="scriptString">The script string to execute.</param>
379379
/// <returns>A Task that can be awaited for the script completion.</returns>
380-
public async Task<IEnumerable<object>> ExecuteScriptString(string scriptString)
380+
public Task<IEnumerable<object>> ExecuteScriptString(
381+
string scriptString)
381382
{
383+
return this.ExecuteScriptString(scriptString, false);
384+
}
385+
386+
/// <summary>
387+
/// Executes a script string in the session's runspace.
388+
/// </summary>
389+
/// <param name="scriptString">The script string to execute.</param>
390+
/// <param name="writeInputToHost">If true, causes the script string to be written to the host.</param>
391+
/// <returns>A Task that can be awaited for the script completion.</returns>
392+
public async Task<IEnumerable<object>> ExecuteScriptString(
393+
string scriptString,
394+
bool writeInputToHost)
395+
{
396+
if (writeInputToHost)
397+
{
398+
((IConsoleHost)this).WriteOutput(
399+
scriptString + Environment.NewLine,
400+
true,
401+
OutputType.Normal);
402+
}
403+
382404
PSCommand psCommand = new PSCommand();
383405
psCommand.AddScript(scriptString);
384406

@@ -547,6 +569,14 @@ private void OnSessionStateChanged(object sender, SessionStateChangedEventArgs e
547569

548570
#region Private Methods
549571

572+
private void WriteOutput(string outputString, bool includeNewLine)
573+
{
574+
((IConsoleHost)this).WriteOutput(
575+
outputString,
576+
includeNewLine,
577+
OutputType.Normal);
578+
}
579+
550580
private void WriteExceptionToHost(Exception e)
551581
{
552582
const string ExceptionFormat =
@@ -867,6 +897,9 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e)
867897

868898
if (taskIndex == 0)
869899
{
900+
// Write a new output line before continuing
901+
this.WriteOutput("", true);
902+
870903
e.ResumeAction = this.debuggerStoppedTask.Task.Result;
871904
Logger.Write(LogLevel.Verbose, "Received debugger resume action " + e.ResumeAction.ToString());
872905

0 commit comments

Comments
 (0)