From 0d49529340f7224c40c71dd241473c570a3a9785 Mon Sep 17 00:00:00 2001 From: Andy Jordan <2226434+andyleejordan@users.noreply.github.com> Date: Thu, 4 Jan 2024 17:01:38 -0800 Subject: [PATCH] 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. --- .../PowerShell/Handlers/EvaluateHandler.cs | 8 +++---- .../TextDocument/Handlers/HoverHandler.cs | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/PowerShellEditorServices/Services/PowerShell/Handlers/EvaluateHandler.cs b/src/PowerShellEditorServices/Services/PowerShell/Handlers/EvaluateHandler.cs index 55a60be28..1bbfb457e 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Handlers/EvaluateHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Handlers/EvaluateHandler.cs @@ -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; @@ -22,9 +23,9 @@ internal class EvaluateHandler : IEvaluateHandler public async Task Handle(EvaluateRequestArguments request, CancellationToken cancellationToken) { // This API is mostly used for F8 execution so it requires the foreground. - await _executionService.ExecutePSCommandAsync( + IReadOnlyList results = await _executionService.ExecutePSCommandAsync( new PSCommand().AddScript(request.Expression), - CancellationToken.None, + cancellationToken, new PowerShellExecutionOptions { RequiresForeground = true, @@ -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 }; } diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs index 9da05490a..457030bcb 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/HoverHandler.cs @@ -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; @@ -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(); + _executionService = executionService; _symbolsService = symbolsService; _workspaceService = workspaceService; } @@ -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 results = await _executionService.ExecutePSCommandAsync( + 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));