-
Notifications
You must be signed in to change notification settings - Fork 254
Back language model tools and the Command Explorer with new LSP requests #2298
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
Open
andyleejordan
wants to merge
3
commits into
main
Choose a base branch
from
andyleejordan/lm-tools-command-explorer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
src/PowerShellEditorServices/Services/PowerShell/Handlers/GetModuleHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Management.Automation; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using MediatR; | ||
| using OmniSharp.Extensions.JsonRpc; | ||
| using Microsoft.PowerShell.EditorServices.Services.PowerShell; | ||
| using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution; | ||
|
|
||
| namespace Microsoft.PowerShell.EditorServices.Handlers | ||
| { | ||
| [Serial, Method("powerShell/getModule", Direction.ClientToServer)] | ||
| internal interface IGetModuleHandler : IJsonRpcRequestHandler<GetModuleParams, PSModuleMessage> { } | ||
|
andyleejordan marked this conversation as resolved.
|
||
|
|
||
| internal class GetModuleParams : IRequest<PSModuleMessage> | ||
| { | ||
| /// <summary> | ||
| /// The name of the module to retrieve metadata for. | ||
| /// </summary> | ||
| public string Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// An optional specific version of the module. When omitted, the newest | ||
| /// available version is returned. | ||
| /// </summary> | ||
| public string Version { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Describes the metadata for a single PowerShell module, used to populate | ||
| /// the Command Explorer's module tooltips. | ||
| /// </summary> | ||
| internal class PSModuleMessage | ||
| { | ||
| public string Name { get; set; } | ||
| public string Version { get; set; } | ||
| public string Description { get; set; } | ||
| public string Path { get; set; } | ||
| public string Author { get; set; } | ||
| public string CompanyName { get; set; } | ||
| public string ProjectUri { get; set; } | ||
| public string PowerShellVersion { get; set; } | ||
| } | ||
|
|
||
| internal class GetModuleHandler : IGetModuleHandler | ||
| { | ||
| private readonly IInternalPowerShellExecutionService _executionService; | ||
|
|
||
| public GetModuleHandler(IInternalPowerShellExecutionService executionService) => _executionService = executionService; | ||
|
|
||
| public async Task<PSModuleMessage> Handle(GetModuleParams request, CancellationToken cancellationToken) | ||
| { | ||
| if (string.IsNullOrEmpty(request.Name)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| // Resolve a module's metadata from the available modules, pinning to a | ||
| // specific version when requested and otherwise taking the newest. | ||
| const string GetModuleScript = @" | ||
| [System.Diagnostics.DebuggerHidden()] | ||
| [CmdletBinding()] | ||
| param ( | ||
| [String]$Name, | ||
| [String]$Version | ||
| ) | ||
| $modules = Microsoft.PowerShell.Core\Get-Module -ListAvailable -Name $Name -ErrorAction Ignore | ||
| if ($Version) { | ||
| $modules = $modules | Microsoft.PowerShell.Core\Where-Object { $_.Version.ToString() -eq $Version } | ||
| } | ||
| $module = $modules | Microsoft.PowerShell.Utility\Sort-Object Version -Descending | Microsoft.PowerShell.Utility\Select-Object -First 1 | ||
| if ($null -eq $module) { | ||
| return | ||
| } | ||
| [PSCustomObject]@{ | ||
| Name = $module.Name | ||
| Version = $module.Version.ToString() | ||
| Description = $module.Description | ||
| Path = $module.Path | ||
| Author = $module.Author | ||
| CompanyName = $module.CompanyName | ||
| ProjectUri = if ($module.ProjectUri) { $module.ProjectUri.ToString() } else { '' } | ||
| PowerShellVersion = if ($module.PowerShellVersion) { $module.PowerShellVersion.ToString() } else { '' } | ||
| } | ||
| "; | ||
|
Comment on lines
+64
to
+88
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @SeeminglyScience main thing I'd like your eye on. |
||
|
|
||
| PSCommand getModuleCommand = new PSCommand() | ||
| .AddScript(GetModuleScript, useLocalScope: true) | ||
| .AddParameter("Name", request.Name) | ||
| .AddParameter("Version", request.Version); | ||
|
|
||
| IReadOnlyList<PSObject> results = await _executionService.ExecutePSCommandAsync<PSObject>( | ||
| getModuleCommand, | ||
| cancellationToken, | ||
| new PowerShellExecutionOptions | ||
| { | ||
| ThrowOnError = false | ||
| }).ConfigureAwait(false); | ||
|
|
||
| PSObject result = results is { Count: > 0 } ? results[0] : null; | ||
| if (result is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return new PSModuleMessage | ||
| { | ||
| Name = GetPropertyString(result, "Name"), | ||
| Version = GetPropertyString(result, "Version"), | ||
| Description = GetPropertyString(result, "Description"), | ||
| Path = GetPropertyString(result, "Path"), | ||
| Author = GetPropertyString(result, "Author"), | ||
| CompanyName = GetPropertyString(result, "CompanyName"), | ||
| ProjectUri = GetPropertyString(result, "ProjectUri"), | ||
| PowerShellVersion = GetPropertyString(result, "PowerShellVersion") | ||
| }; | ||
| } | ||
|
|
||
| private static string GetPropertyString(PSObject psObject, string propertyName) | ||
| => psObject.Properties[propertyName]?.Value as string ?? string.Empty; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.