Skip to content

Commit aaac8e1

Browse files
committed
WIP: Evaluate variables on hover
If their value is available (either because its been set in the session manually, or the script has been executed or is being debugged) we can return it on the hover. We should ask if we actually want to do this, and if so, how we want to format it.
1 parent 4c39342 commit aaac8e1

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/PowerShellEditorServices/Services/PowerShell/Handlers/EvaluateHandler.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System.Collections.Generic;
45
using System.Management.Automation;
56
using System.Threading;
67
using System.Threading.Tasks;
@@ -22,9 +23,9 @@ internal class EvaluateHandler : IEvaluateHandler
2223
public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request, CancellationToken cancellationToken)
2324
{
2425
// This API is mostly used for F8 execution so it requires the foreground.
25-
await _executionService.ExecutePSCommandAsync(
26+
IReadOnlyList<PSObject> results = await _executionService.ExecutePSCommandAsync<PSObject>(
2627
new PSCommand().AddScript(request.Expression),
27-
CancellationToken.None,
28+
cancellationToken,
2829
new PowerShellExecutionOptions
2930
{
3031
RequiresForeground = true,
@@ -34,10 +35,9 @@ await _executionService.ExecutePSCommandAsync(
3435
ThrowOnError = false,
3536
}).ConfigureAwait(false);
3637

37-
// TODO: Should we return a more informative result?
3838
return new EvaluateResponseBody
3939
{
40-
Result = "",
40+
Result = string.Join(System.Environment.NewLine, results),
4141
VariablesReference = 0
4242
};
4343
}

src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs

+22
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using System;
45
using System.Collections.Generic;
6+
using System.Management.Automation;
57
using System.Threading;
68
using System.Threading.Tasks;
79
using Microsoft.Extensions.Logging;
810
using Microsoft.PowerShell.EditorServices.Services;
11+
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
12+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
913
using Microsoft.PowerShell.EditorServices.Services.Symbols;
1014
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
1115
using Microsoft.PowerShell.EditorServices.Utility;
@@ -18,15 +22,18 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
1822
internal class PsesHoverHandler : HoverHandlerBase
1923
{
2024
private readonly ILogger _logger;
25+
private readonly IInternalPowerShellExecutionService _executionService;
2126
private readonly SymbolsService _symbolsService;
2227
private readonly WorkspaceService _workspaceService;
2328

2429
public PsesHoverHandler(
2530
ILoggerFactory factory,
31+
IInternalPowerShellExecutionService executionService,
2632
SymbolsService symbolsService,
2733
WorkspaceService workspaceService)
2834
{
2935
_logger = factory.CreateLogger<PsesHoverHandler>();
36+
_executionService = executionService;
3037
_symbolsService = symbolsService;
3138
_workspaceService = workspaceService;
3239
}
@@ -63,6 +70,21 @@ await _symbolsService.FindSymbolDetailsAtLocationAsync(
6370
new MarkedString("PowerShell", symbolDetails.SymbolReference.Name)
6471
};
6572

73+
// If we're looking at a variable, try to get its value.
74+
if (symbolDetails.SymbolReference.Type == SymbolType.Variable)
75+
{
76+
PSCommand command = new PSCommand().AddScript($"[System.Diagnostics.DebuggerHidden()]param() {symbolDetails.SymbolReference.Name}");
77+
IReadOnlyList<PSObject> results = await _executionService.ExecutePSCommandAsync<PSObject>(
78+
command,
79+
cancellationToken,
80+
new PowerShellExecutionOptions { ThrowOnError = false }).ConfigureAwait(false);
81+
82+
if (results != null)
83+
{
84+
symbolInfo.Add(new MarkedString("PowerShell", string.Join(Environment.NewLine, results)));
85+
}
86+
}
87+
6688
if (!string.IsNullOrEmpty(symbolDetails.Documentation))
6789
{
6890
symbolInfo.Add(new MarkedString("markdown", symbolDetails.Documentation));

0 commit comments

Comments
 (0)