Skip to content

Commit 4631f97

Browse files
add evaluate handler
1 parent c434fa2 commit 4631f97

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

src/PowerShellEditorServices.Engine/LanguageServer/OmnisharpLanguageServer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public async Task StartAsync()
116116
.WithHandler<DefinitionHandler>()
117117
.WithHandler<TemplateHandlers>()
118118
.WithHandler<GetCommentHelpHandler>()
119+
.WithHandler<EvaluateHandler>()
119120
.OnInitialize(
120121
async (languageServer, request) =>
121122
{
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Threading;
3+
using System.Threading.Tasks;
4+
using Microsoft.Extensions.Logging;
5+
using Microsoft.PowerShell.EditorServices;
6+
7+
namespace PowerShellEditorServices.Engine.Services.Handlers
8+
{
9+
public class EvaluateHandler : IEvaluateHandler
10+
{
11+
private readonly ILogger _logger;
12+
private readonly PowerShellContextService _powerShellContextService;
13+
14+
public EvaluateHandler(ILoggerFactory factory, PowerShellContextService powerShellContextService)
15+
{
16+
_logger = factory.CreateLogger<EvaluateHandler>();
17+
_powerShellContextService = powerShellContextService;
18+
}
19+
20+
public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request, CancellationToken cancellationToken)
21+
{
22+
await _powerShellContextService.ExecuteScriptStringAsync(
23+
request.Expression,
24+
writeInputToHost: true,
25+
writeOutputToHost: true,
26+
addToHistory: true);
27+
28+
return new EvaluateResponseBody
29+
{
30+
Result = "",
31+
VariablesReference = 0
32+
};
33+
}
34+
}
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using OmniSharp.Extensions.Embedded.MediatR;
2+
using OmniSharp.Extensions.JsonRpc;
3+
4+
namespace PowerShellEditorServices.Engine.Services.Handlers
5+
{
6+
[Serial, Method("evaluate")]
7+
public interface IEvaluateHandler : IJsonRpcRequestHandler<EvaluateRequestArguments, EvaluateResponseBody> { }
8+
9+
public class EvaluateRequestArguments : IRequest<EvaluateResponseBody>
10+
{
11+
/// <summary>
12+
/// The expression to evaluate.
13+
/// </summary>
14+
public string Expression { get; set; }
15+
16+
/// <summary>
17+
/// The context in which the evaluate request is run. Possible
18+
/// values are 'watch' if evaluate is run in a watch or 'repl'
19+
/// if run from the REPL console.
20+
/// </summary>
21+
public string Context { get; set; }
22+
23+
/// <summary>
24+
/// Evaluate the expression in the context of this stack frame.
25+
/// If not specified, the top most frame is used.
26+
/// </summary>
27+
public int FrameId { get; set; }
28+
}
29+
30+
public class EvaluateResponseBody
31+
{
32+
/// <summary>
33+
/// The evaluation result.
34+
/// </summary>
35+
public string Result { get; set; }
36+
37+
/// <summary>
38+
/// If variablesReference is > 0, the evaluate result is
39+
/// structured and its children can be retrieved by passing
40+
/// variablesReference to the VariablesRequest
41+
/// </summary>
42+
public int VariablesReference { get; set; }
43+
}
44+
}

test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,5 +781,21 @@ await LanguageClient.SendRequest<CommentHelpRequestResult>(
781781
Assert.NotEmpty(commentHelpRequestResult.Content);
782782
Assert.Contains("myParam", commentHelpRequestResult.Content[7]);
783783
}
784+
785+
[Fact]
786+
public async Task CanSendEvaluateRequest()
787+
{
788+
EvaluateResponseBody evaluateResponseBody =
789+
await LanguageClient.SendRequest<EvaluateResponseBody>(
790+
"evaluate",
791+
new EvaluateRequestArguments
792+
{
793+
Expression = "Get-ChildItem"
794+
});
795+
796+
// These always gets returned so this test really just makes sure we get _any_ response.
797+
Assert.Equal("", evaluateResponseBody.Result);
798+
Assert.Equal(0, evaluateResponseBody.VariablesReference);
799+
}
784800
}
785801
}

0 commit comments

Comments
 (0)