-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2b723b0
commit 5580e80
Showing
5 changed files
with
355 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
151 changes: 151 additions & 0 deletions
151
src/PowerShellEditorServices.Engine/Services/Symbols/ParameterSetSignatures.cs
This file contains 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,151 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System.Collections.Generic; | ||
using System.Management.Automation; | ||
using Microsoft.PowerShell.EditorServices.Symbols; | ||
|
||
namespace Microsoft.PowerShell.EditorServices | ||
{ | ||
/// <summary> | ||
/// A class for containing the commandName, the command's | ||
/// possible signatures, and the script extent of the command | ||
/// </summary> | ||
public class ParameterSetSignatures | ||
{ | ||
#region Properties | ||
/// <summary> | ||
/// Gets the name of the command | ||
/// </summary> | ||
public string CommandName { get; internal set; } | ||
|
||
/// <summary> | ||
/// Gets the collection of signatures for the command | ||
/// </summary> | ||
public ParameterSetSignature[] Signatures { get; internal set; } | ||
|
||
/// <summary> | ||
/// Gets the script extent of the command | ||
/// </summary> | ||
public ScriptRegion ScriptRegion { get; internal set; } | ||
#endregion | ||
|
||
/// <summary> | ||
/// Constructs an instance of a ParameterSetSignatures object | ||
/// </summary> | ||
/// <param name="commandInfoSet">Collection of parameter set info</param> | ||
/// <param name="foundSymbol"> The SymbolReference of the command</param> | ||
public ParameterSetSignatures(IEnumerable<CommandParameterSetInfo> commandInfoSet, SymbolReference foundSymbol) | ||
{ | ||
List<ParameterSetSignature> paramSetSignatures = new List<ParameterSetSignature>(); | ||
foreach (CommandParameterSetInfo setInfo in commandInfoSet) | ||
{ | ||
paramSetSignatures.Add(new ParameterSetSignature(setInfo)); | ||
} | ||
Signatures = paramSetSignatures.ToArray(); | ||
CommandName = foundSymbol.ScriptRegion.Text; | ||
ScriptRegion = foundSymbol.ScriptRegion; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// A class for containing the signature text and the collection of parameters for a signature | ||
/// </summary> | ||
public class ParameterSetSignature | ||
{ | ||
private static HashSet<string> commonParameterNames = | ||
new HashSet<string> | ||
{ | ||
"Verbose", | ||
"Debug", | ||
"ErrorAction", | ||
"WarningAction", | ||
"InformationAction", | ||
"ErrorVariable", | ||
"WarningVariable", | ||
"InformationVariable", | ||
"OutVariable", | ||
"OutBuffer", | ||
"PipelineVariable", | ||
}; | ||
|
||
#region Properties | ||
/// <summary> | ||
/// Gets the signature text | ||
/// </summary> | ||
public string SignatureText { get; internal set; } | ||
|
||
/// <summary> | ||
/// Gets the collection of parameters for the signature | ||
/// </summary> | ||
public IEnumerable<ParameterInfo> Parameters { get; internal set; } | ||
#endregion | ||
|
||
/// <summary> | ||
/// Constructs an instance of a ParameterSetSignature | ||
/// </summary> | ||
/// <param name="commandParamInfoSet">Collection of parameter info</param> | ||
public ParameterSetSignature(CommandParameterSetInfo commandParamInfoSet) | ||
{ | ||
List<ParameterInfo> parameterInfo = new List<ParameterInfo>(); | ||
foreach (CommandParameterInfo commandParameterInfo in commandParamInfoSet.Parameters) | ||
{ | ||
if (!commonParameterNames.Contains(commandParameterInfo.Name)) | ||
{ | ||
parameterInfo.Add(new ParameterInfo(commandParameterInfo)); | ||
} | ||
} | ||
|
||
SignatureText = commandParamInfoSet.ToString(); | ||
Parameters = parameterInfo.ToArray(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// A class for containing the parameter info of a parameter | ||
/// </summary> | ||
public class ParameterInfo | ||
{ | ||
#region Properties | ||
/// <summary> | ||
/// Gets the name of the parameter | ||
/// </summary> | ||
public string Name { get; internal set; } | ||
|
||
/// <summary> | ||
/// Gets the type of the parameter | ||
/// </summary> | ||
public string ParameterType { get; internal set; } | ||
|
||
/// <summary> | ||
/// Gets the position of the parameter | ||
/// </summary> | ||
public int Position { get; internal set; } | ||
|
||
/// <summary> | ||
/// Gets a boolean for whetheer or not the parameter is required | ||
/// </summary> | ||
public bool IsMandatory { get; internal set; } | ||
|
||
/// <summary> | ||
/// Gets the help message of the parameter | ||
/// </summary> | ||
public string HelpMessage { get; internal set; } | ||
#endregion | ||
|
||
/// <summary> | ||
/// Constructs an instance of a ParameterInfo object | ||
/// </summary> | ||
/// <param name="parameterInfo">Parameter info of the parameter</param> | ||
public ParameterInfo(CommandParameterInfo parameterInfo) | ||
{ | ||
this.Name = "-" + parameterInfo.Name; | ||
this.ParameterType = parameterInfo.ParameterType.FullName; | ||
this.Position = parameterInfo.Position; | ||
this.IsMandatory = parameterInfo.IsMandatory; | ||
this.HelpMessage = parameterInfo.HelpMessage; | ||
} | ||
} | ||
} |
This file contains 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
116 changes: 116 additions & 0 deletions
116
src/PowerShellEditorServices.Engine/Services/TextDocument/Handlers/SignatureHelpHandler.cs
This file contains 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,116 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.PowerShell.EditorServices; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Server; | ||
|
||
namespace PowerShellEditorServices.Engine.Services.Handlers | ||
{ | ||
public class SignatureHelpHandler : ISignatureHelpHandler | ||
{ | ||
private static readonly SignatureInformation[] s_emptySignatureResult = new SignatureInformation[0]; | ||
|
||
private readonly DocumentSelector _documentSelector = new DocumentSelector( | ||
new DocumentFilter() | ||
{ | ||
Pattern = "**/*.ps*1" | ||
} | ||
); | ||
|
||
private readonly ILogger _logger; | ||
private readonly SymbolsService _symbolsService; | ||
private readonly WorkspaceService _workspaceService; | ||
private readonly PowerShellContextService _powerShellContextService; | ||
|
||
private SignatureHelpCapability _capability; | ||
|
||
public SignatureHelpHandler( | ||
ILoggerFactory factory, | ||
SymbolsService symbolsService, | ||
WorkspaceService workspaceService, | ||
PowerShellContextService powerShellContextService) | ||
{ | ||
_logger = factory.CreateLogger<HoverHandler>(); | ||
_symbolsService = symbolsService; | ||
_workspaceService = workspaceService; | ||
_powerShellContextService = powerShellContextService; | ||
} | ||
|
||
public SignatureHelpRegistrationOptions GetRegistrationOptions() | ||
{ | ||
return new SignatureHelpRegistrationOptions | ||
{ | ||
DocumentSelector = _documentSelector, | ||
// A sane default of " ". We may be able to include others like "-". | ||
TriggerCharacters = new Container<string>(" ") | ||
}; | ||
} | ||
|
||
public async Task<SignatureHelp> Handle(SignatureHelpParams request, CancellationToken cancellationToken) | ||
{ | ||
ScriptFile scriptFile = | ||
_workspaceService.GetFile( | ||
request.TextDocument.Uri.ToString()); | ||
|
||
ParameterSetSignatures parameterSets = | ||
await _symbolsService.FindParameterSetsInFileAsync( | ||
scriptFile, | ||
(int) request.Position.Line + 1, | ||
(int) request.Position.Character + 1, | ||
_powerShellContextService); | ||
|
||
SignatureInformation[] signatures = s_emptySignatureResult; | ||
|
||
if (parameterSets != null) | ||
{ | ||
signatures = new SignatureInformation[parameterSets.Signatures.Length]; | ||
for (int i = 0; i < signatures.Length; i++) | ||
{ | ||
var parameters = new ParameterInformation[parameterSets.Signatures[i].Parameters.Count()]; | ||
int j = 0; | ||
foreach (ParameterInfo param in parameterSets.Signatures[i].Parameters) | ||
{ | ||
parameters[j] = CreateParameterInfo(param); | ||
j++; | ||
} | ||
|
||
signatures[i] = new SignatureInformation | ||
{ | ||
Label = parameterSets.CommandName + " " + parameterSets.Signatures[i].SignatureText, | ||
Documentation = null, | ||
Parameters = parameters, | ||
}; | ||
} | ||
} | ||
|
||
return new SignatureHelp | ||
{ | ||
Signatures = signatures, | ||
ActiveParameter = null, | ||
ActiveSignature = 0 | ||
}; | ||
} | ||
|
||
public void SetCapability(SignatureHelpCapability capability) | ||
{ | ||
_capability = capability; | ||
} | ||
|
||
private static ParameterInformation CreateParameterInfo(ParameterInfo parameterInfo) | ||
{ | ||
return new ParameterInformation | ||
{ | ||
Label = parameterInfo.Name, | ||
Documentation = string.Empty | ||
}; | ||
} | ||
} | ||
} |
This file contains 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