Skip to content

Commit

Permalink
WIP: Evaluate variables on hover
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andyleejordan committed Jan 5, 2024
1 parent 4c39342 commit aaac8e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Collections.Generic;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -22,9 +23,9 @@ internal class EvaluateHandler : IEvaluateHandler
public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request, CancellationToken cancellationToken)
{
// This API is mostly used for F8 execution so it requires the foreground.
await _executionService.ExecutePSCommandAsync(
IReadOnlyList<PSObject> results = await _executionService.ExecutePSCommandAsync<PSObject>(
new PSCommand().AddScript(request.Expression),
CancellationToken.None,
cancellationToken,
new PowerShellExecutionOptions
{
RequiresForeground = true,
Expand All @@ -34,10 +35,9 @@ await _executionService.ExecutePSCommandAsync(
ThrowOnError = false,
}).ConfigureAwait(false);

// TODO: Should we return a more informative result?
return new EvaluateResponseBody
{
Result = "",
Result = string.Join(System.Environment.NewLine, results),
VariablesReference = 0
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Services.PowerShell;
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
using Microsoft.PowerShell.EditorServices.Services.Symbols;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Microsoft.PowerShell.EditorServices.Utility;
Expand All @@ -18,15 +22,18 @@ namespace Microsoft.PowerShell.EditorServices.Handlers
internal class PsesHoverHandler : HoverHandlerBase
{
private readonly ILogger _logger;
private readonly IInternalPowerShellExecutionService _executionService;
private readonly SymbolsService _symbolsService;
private readonly WorkspaceService _workspaceService;

public PsesHoverHandler(
ILoggerFactory factory,
IInternalPowerShellExecutionService executionService,
SymbolsService symbolsService,
WorkspaceService workspaceService)
{
_logger = factory.CreateLogger<PsesHoverHandler>();
_executionService = executionService;
_symbolsService = symbolsService;
_workspaceService = workspaceService;
}
Expand Down Expand Up @@ -63,6 +70,21 @@ await _symbolsService.FindSymbolDetailsAtLocationAsync(
new MarkedString("PowerShell", symbolDetails.SymbolReference.Name)
};

// If we're looking at a variable, try to get its value.
if (symbolDetails.SymbolReference.Type == SymbolType.Variable)
{
PSCommand command = new PSCommand().AddScript($"[System.Diagnostics.DebuggerHidden()]param() {symbolDetails.SymbolReference.Name}");
IReadOnlyList<PSObject> results = await _executionService.ExecutePSCommandAsync<PSObject>(
command,
cancellationToken,
new PowerShellExecutionOptions { ThrowOnError = false }).ConfigureAwait(false);

if (results != null)
{
symbolInfo.Add(new MarkedString("PowerShell", string.Join(Environment.NewLine, results)));
}
}

if (!string.IsNullOrEmpty(symbolDetails.Documentation))
{
symbolInfo.Add(new MarkedString("markdown", symbolDetails.Documentation));
Expand Down

0 comments on commit aaac8e1

Please sign in to comment.