Skip to content

Commit 3c5cceb

Browse files
committed
Merge pull request PowerShell#105 from PowerShell/daviwil/repl-improvements
Improvements to REPL execution
2 parents 3a3d984 + 96f5e47 commit 3c5cceb

File tree

18 files changed

+504
-89
lines changed

18 files changed

+504
-89
lines changed

src/PowerShellEditorServices.Channel.WebSocket/Properties/AssemblyInfo.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using System.Reflection;
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System.Reflection;
27
using System.Runtime.CompilerServices;
38
using System.Runtime.InteropServices;
49

@@ -10,7 +15,7 @@
1015
[assembly: AssemblyConfiguration("")]
1116
[assembly: AssemblyCompany("")]
1217
[assembly: AssemblyProduct("PowerShellEditorServices.Channel.WebSocket")]
13-
[assembly: AssemblyCopyright("Copyright © 2015")]
18+
[assembly: AssemblyCopyright("Copyright © 2015")]
1419
[assembly: AssemblyTrademark("")]
1520
[assembly: AssemblyCulture("")]
1621

@@ -34,3 +39,4 @@
3439
// [assembly: AssemblyVersion("1.0.*")]
3540
[assembly: AssemblyVersion("1.0.0.0")]
3641
[assembly: AssemblyFileVersion("1.0.0.0")]
42+

src/PowerShellEditorServices.Channel.WebSocket/WebsocketClientChannel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using System;
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
27
using System.IO;
38
using System.Linq;
49
using System.Net.WebSockets;
@@ -150,3 +155,4 @@ public override async Task FlushAsync(CancellationToken cancellationToken)
150155
}
151156
}
152157
}
158+

src/PowerShellEditorServices.Channel.WebSocket/WebsocketServerChannel.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
using System;
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
27
using System.IO;
38
using System.Linq;
49
using System.Net.WebSockets;
@@ -170,3 +175,4 @@ public async Task DispatchMessage()
170175
}
171176
}
172177
}
178+

src/PowerShellEditorServices.Host/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,15 @@ static void Main(string[] args)
9494
}
9595
#endif
9696

97-
Logger.Write(LogLevel.Normal, "PowerShell Editor Services Host starting...");
97+
FileVersionInfo fileVersionInfo =
98+
FileVersionInfo.GetVersionInfo(
99+
System.Reflection.Assembly.GetExecutingAssembly().Location);
100+
101+
Logger.Write(
102+
LogLevel.Normal,
103+
string.Format(
104+
"PowerShell Editor Services Host v{0} starting...",
105+
fileVersionInfo.FileVersion));
98106

99107
// Start the server
100108
server.Start();

src/PowerShellEditorServices.Protocol/DebugAdapter/EvaluateRequest.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,37 @@ public static readonly
1616

1717
public class EvaluateRequestArguments
1818
{
19+
/// <summary>
20+
/// The expression to evaluate.
21+
/// </summary>
1922
public string Expression { get; set; }
2023

21-
// /** Evaluate the expression in the context of this stack frame. If not specified, the top most frame is used. */
24+
/// <summary>
25+
/// The context in which the evaluate request is run. Possible
26+
/// values are 'watch' if evaluate is run in a watch or 'repl'
27+
/// if run from the REPL console.
28+
/// </summary>
29+
public string Context { get; set; }
30+
31+
/// <summary>
32+
/// Evaluate the expression in the context of this stack frame.
33+
/// If not specified, the top most frame is used.
34+
/// </summary>
2235
public int FrameId { get; set; }
2336
}
2437

2538
public class EvaluateResponseBody
2639
{
40+
/// <summary>
41+
/// The evaluation result.
42+
/// </summary>
2743
public string Result { get; set; }
2844

29-
// /** If variablesReference is > 0, the evaluate result is structured and its children can be retrieved by passing variablesReference to the VariablesRequest */
45+
/// <summary>
46+
/// If variablesReference is > 0, the evaluate result is
47+
/// structured and its children can be retrieved by passing
48+
/// variablesReference to the VariablesRequest
49+
/// </summary>
3050
public int VariablesReference { get; set; }
3151
}
3252
}

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,17 @@ protected async Task HandleEvaluateRequest(
312312
EvaluateRequestArguments evaluateParams,
313313
RequestContext<EvaluateResponseBody> requestContext)
314314
{
315+
bool isFromRepl =
316+
string.Equals(
317+
evaluateParams.Context,
318+
"repl",
319+
StringComparison.InvariantCultureIgnoreCase);
320+
315321
VariableDetails result =
316322
await editorSession.DebugService.EvaluateExpression(
317323
evaluateParams.Expression,
318-
evaluateParams.FrameId);
324+
evaluateParams.FrameId,
325+
isFromRepl);
319326

320327
string valueString = null;
321328
int variableId = 0;

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -676,27 +676,19 @@ protected async Task HandleEvaluateRequest(
676676
DebugAdapterMessages.EvaluateRequestArguments evaluateParams,
677677
RequestContext<DebugAdapterMessages.EvaluateResponseBody> requestContext)
678678
{
679-
VariableDetails result =
680-
await editorSession.DebugService.EvaluateExpression(
679+
var results =
680+
await this.editorSession.PowerShellContext.ExecuteScriptString(
681681
evaluateParams.Expression,
682-
evaluateParams.FrameId);
683-
684-
string valueString = null;
685-
int variableId = 0;
686-
687-
if (result != null)
688-
{
689-
valueString = result.ValueString;
690-
variableId =
691-
result.IsExpandable ?
692-
result.Id : 0;
693-
}
682+
true,
683+
true);
694684

685+
// Return an empty result since the result value is irrelevant
686+
// for this request in the LanguageServer
695687
await requestContext.SendResult(
696688
new DebugAdapterMessages.EvaluateResponseBody
697689
{
698-
Result = valueString,
699-
VariablesReference = variableId
690+
Result = "",
691+
VariablesReference = 0
700692
});
701693
}
702694

src/PowerShellEditorServices/Debugging/DebugService.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,20 +233,38 @@ public VariableDetailsBase GetVariableFromExpression(string variableExpression,
233233
/// </summary>
234234
/// <param name="expressionString">The expression string to execute.</param>
235235
/// <param name="stackFrameId">The ID of the stack frame in which the expression should be executed.</param>
236+
/// <param name="writeResultAsOutput">
237+
/// If true, writes the expression result as host output rather than returning the results.
238+
/// In this case, the return value of this function will be null.</param>
236239
/// <returns>A VariableDetails object containing the result.</returns>
237-
public async Task<VariableDetails> EvaluateExpression(string expressionString, int stackFrameId)
240+
public async Task<VariableDetails> EvaluateExpression(
241+
string expressionString,
242+
int stackFrameId,
243+
bool writeResultAsOutput)
238244
{
239245
var results =
240246
await this.powerShellContext.ExecuteScriptString(
241-
expressionString);
247+
expressionString,
248+
false,
249+
writeResultAsOutput);
242250

243251
// Since this method should only be getting invoked in the debugger,
244252
// we can assume that Out-String will be getting used to format results
245-
// of command executions into string output.
246-
247-
return new VariableDetails(
248-
expressionString,
249-
string.Join(Environment.NewLine, results));
253+
// of command executions into string output. However, if null is returned
254+
// then return null so that no output gets displayed.
255+
string outputString =
256+
results != null ?
257+
string.Join(Environment.NewLine, results) :
258+
null;
259+
260+
// If we've written the result as output, don't return a
261+
// VariableDetails instance.
262+
return
263+
writeResultAsOutput ?
264+
null :
265+
new VariableDetails(
266+
expressionString,
267+
outputString);
250268
}
251269

252270
/// <summary>

src/PowerShellEditorServices/Language/AstOperations.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,18 +180,24 @@ static public SymbolReference FindDefinitionOfSymbol(
180180
/// <returns>A collection of SymbolReference objects</returns>
181181
static public IEnumerable<SymbolReference> FindSymbolsInDocument(Ast scriptAst, Version powerShellVersion)
182182
{
183+
IEnumerable<SymbolReference> symbolReferences = null;
184+
183185
if (powerShellVersion >= new Version(5,0))
184186
{
187+
#if PowerShellv5
185188
FindSymbolsVisitor2 findSymbolsVisitor = new FindSymbolsVisitor2();
186189
scriptAst.Visit(findSymbolsVisitor);
187-
return findSymbolsVisitor.SymbolReferences;
190+
symbolReferences = findSymbolsVisitor.SymbolReferences;
191+
#endif
188192
}
189193
else
190194
{
191195
FindSymbolsVisitor findSymbolsVisitor = new FindSymbolsVisitor();
192196
scriptAst.Visit(findSymbolsVisitor);
193-
return findSymbolsVisitor.SymbolReferences;
197+
symbolReferences = findSymbolsVisitor.SymbolReferences;
194198
}
199+
200+
return symbolReferences;
195201
}
196202

197203
/// <summary>

src/PowerShellEditorServices/Language/FindSymbolsVisitor2.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
using System.Collections.Generic;
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System.Collections.Generic;
27
using System.Management.Automation.Language;
38

49
namespace Microsoft.PowerShell.EditorServices
510
{
11+
#if PowerShellv5
612
/// <summary>
713
/// The visitor used to find all the symbols (function and class defs) in the AST.
814
/// </summary>
@@ -67,4 +73,6 @@ public override AstVisitAction VisitConfigurationDefinition(ConfigurationDefinit
6773
return AstVisitAction.Continue;
6874
}
6975
}
76+
#endif
7077
}
78+

0 commit comments

Comments
 (0)