Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Evaluate variables on hover #2127

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ public async Task<EvaluateResponseBody> Handle(EvaluateRequestArguments request,
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 @@ public override async Task<Hover> Handle(HoverParams request, CancellationToken
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