Skip to content

Commit b3c1859

Browse files
Kapil Borledaviwil
authored andcommitted
Handle comment help completion request
1 parent 411fc37 commit b3c1859

File tree

3 files changed

+76
-17
lines changed

3 files changed

+76
-17
lines changed

src/PowerShellEditorServices.Protocol/LanguageServer/CommentHelpRequest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer
1010
class CommentHelpRequest
1111
{
1212
public static readonly RequestType<CommentHelpRequestParams, CommentHelpRequestResult, object, object> Type
13-
= RequestType<CommentHelpRequestParams, CommentHelpRequestResult, object, object>.Create("powershell/getCommentHelp");
13+
= RequestType<CommentHelpRequestParams, CommentHelpRequestResult, object, object>.Create("powerShell/getCommentHelp");
1414
}
1515

1616
public class CommentHelpRequestResult
1717
{
18-
public string[] content;
18+
public string[] Content { get; set; }
1919
}
2020

2121
public class CommentHelpRequestParams

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Threading;
2222
using System.Threading.Tasks;
2323
using DebugAdapterMessages = Microsoft.PowerShell.EditorServices.Protocol.DebugAdapter;
24+
using System.Collections;
2425

2526
namespace Microsoft.PowerShell.EditorServices.Protocol.Server
2627
{
@@ -149,7 +150,7 @@ protected override void Initialize()
149150
this.SetRequestHandler(ScriptRegionRequest.Type, this.HandleGetFormatScriptRegionRequest);
150151

151152
this.SetRequestHandler(GetPSHostProcessesRequest.Type, this.HandleGetPSHostProcessesRequest);
152-
153+
this.SetRequestHandler(CommentHelpRequest.Type, this.HandleCommentHelpRequest);
153154
// Initialize the extension service
154155
// TODO: This should be made awaited once Initialize is async!
155156
this.editorSession.ExtensionService.Initialize(
@@ -243,9 +244,9 @@ private async Task HandleSetPSSARulesRequest(
243244
var ruleInfos = dynParams.ruleInfos;
244245
foreach (dynamic ruleInfo in ruleInfos)
245246
{
246-
if ((Boolean) ruleInfo.isEnabled)
247+
if ((Boolean)ruleInfo.isEnabled)
247248
{
248-
activeRules.Add((string) ruleInfo.name);
249+
activeRules.Add((string)ruleInfo.name);
249250
}
250251
}
251252
editorSession.AnalysisService.ActiveRules = activeRules.ToArray();
@@ -309,7 +310,8 @@ private async Task HandleScriptFileMarkersRequest(
309310
var markers = await editorSession.AnalysisService.GetSemanticMarkersAsync(
310311
editorSession.Workspace.GetFile(requestParams.fileUri),
311312
editorSession.AnalysisService.GetPSSASettingsHashtable(requestParams.settings));
312-
await requestContext.SendResult(new ScriptFileMarkerRequestResultParams {
313+
await requestContext.SendResult(new ScriptFileMarkerRequestResultParams
314+
{
313315
markers = markers
314316
});
315317
}
@@ -571,7 +573,7 @@ protected async Task HandleDidChangeConfigurationNotification(
571573
{
572574
bool oldLoadProfiles = this.currentSettings.EnableProfileLoading;
573575
bool oldScriptAnalysisEnabled =
574-
this.currentSettings.ScriptAnalysis.Enable.HasValue ? this.currentSettings.ScriptAnalysis.Enable.Value : false ;
576+
this.currentSettings.ScriptAnalysis.Enable.HasValue ? this.currentSettings.ScriptAnalysis.Enable.Value : false;
575577
string oldScriptAnalysisSettingsPath =
576578
this.currentSettings.ScriptAnalysis.SettingsPath;
577579

@@ -1074,6 +1076,50 @@ protected async Task HandleGetPSHostProcessesRequest(
10741076
await requestContext.SendResult(psHostProcesses.ToArray());
10751077
}
10761078

1079+
protected async Task HandleCommentHelpRequest(
1080+
CommentHelpRequestParams requestParams,
1081+
RequestContext<CommentHelpRequestResult> requestContext)
1082+
{
1083+
var scriptFile = EditorSession.Workspace.GetFile(requestParams.DocumentUri);
1084+
var expectedFunctionLine = requestParams.TriggerPosition.Line + 2;
1085+
var functionDefinitionAst = EditorSession.LanguageService.GetFunctionDefinitionAtLine(
1086+
scriptFile,
1087+
expectedFunctionLine);
1088+
var result = new CommentHelpRequestResult()
1089+
{
1090+
Content = new string[] { "" }
1091+
};
1092+
1093+
if (functionDefinitionAst != null)
1094+
{
1095+
var settings = new Dictionary<string, Hashtable>();
1096+
var ruleSettings = new Hashtable();
1097+
ruleSettings.Add("ExportedOnly", false);
1098+
ruleSettings.Add("Enable", true);
1099+
settings.Add("PSProvideCommentHelp", ruleSettings);
1100+
var pssaSettings = EditorSession.AnalysisService.GetPSSASettingsHashtable(settings);
1101+
1102+
// todo create a semantic marker api that take only string
1103+
var analysisResults = await EditorSession.AnalysisService.GetSemanticMarkersAsync(
1104+
scriptFile,
1105+
pssaSettings);
1106+
1107+
var analysisResult = analysisResults?.FirstOrDefault(x =>
1108+
{
1109+
return x.Correction != null
1110+
&& x.Correction.Edits[0].StartLineNumber == expectedFunctionLine;
1111+
});
1112+
1113+
if (analysisResult != null)
1114+
{
1115+
// find the analysis result whose correction starts on
1116+
result.Content = analysisResult.Correction.Edits[0].Text.Split('\n').Select(x => x.Trim('\r')).ToArray();
1117+
}
1118+
}
1119+
1120+
await requestContext.SendResult(result);
1121+
}
1122+
10771123
private bool IsQueryMatch(string query, string symbolName)
10781124
{
10791125
return symbolName.IndexOf(query, StringComparison.OrdinalIgnoreCase) >= 0;
@@ -1136,12 +1182,12 @@ protected Task HandleEvaluateRequest(
11361182
// Return an empty result since the result value is irrelevant
11371183
// for this request in the LanguageServer
11381184
return
1139-
requestContext.SendResult(
1140-
new DebugAdapterMessages.EvaluateResponseBody
1141-
{
1142-
Result = "",
1143-
VariablesReference = 0
1144-
});
1185+
requestContext.SendResult(
1186+
new DebugAdapterMessages.EvaluateResponseBody
1187+
{
1188+
Result = "",
1189+
VariablesReference = 0
1190+
});
11451191
});
11461192

11471193
return Task.FromResult(true);

src/PowerShellEditorServices/Language/LanguageService.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ await AstOperations.GetCompletions(
115115

116116
return completionResults;
117117
}
118-
catch(ArgumentException e)
118+
catch (ArgumentException e)
119119
{
120120
// Bad completion results could return an invalid
121121
// replacement range, catch that here
@@ -231,15 +231,17 @@ public FindOccurrencesResult FindSymbolsInFile(ScriptFile scriptFile)
231231
AstOperations
232232
.FindSymbolsInDocument(scriptFile.ScriptAst, this.powerShellContext.LocalPowerShellVersion.Version)
233233
.Select(
234-
reference => {
234+
reference =>
235+
{
235236
reference.SourceLine =
236237
scriptFile.GetLine(reference.ScriptRegion.StartLineNumber);
237238
reference.FilePath = scriptFile.FilePath;
238239
return reference;
239240
});
240241

241242
return
242-
new FindOccurrencesResult {
243+
new FindOccurrencesResult
244+
{
243245
FoundOccurrences = symbolReferencesinFile
244246
};
245247
}
@@ -267,7 +269,7 @@ public async Task<FindReferencesResult> FindReferencesOfSymbol(
267269

268270
// We want to look for references first in referenced files, hence we use ordered dictionary
269271
var fileMap = new OrderedDictionary(StringComparer.OrdinalIgnoreCase);
270-
foreach(ScriptFile file in referencedFiles)
272+
foreach (ScriptFile file in referencedFiles)
271273
{
272274
fileMap.Add(file.FilePath, file);
273275
}
@@ -520,6 +522,17 @@ public ScriptRegion FindSmallestStatementAstRegion(
520522
return ScriptRegion.Create(ast.Extent);
521523
}
522524

525+
public FunctionDefinitionAst GetFunctionDefinitionAtLine(
526+
ScriptFile scriptFile,
527+
int lineNumber)
528+
{
529+
var functionDefinitionAst = scriptFile.ScriptAst.Find(
530+
ast => ast is FunctionDefinitionAst && ast.Extent.StartLineNumber == lineNumber,
531+
true);
532+
533+
return functionDefinitionAst as FunctionDefinitionAst;
534+
}
535+
523536
#endregion
524537

525538
#region Private Fields

0 commit comments

Comments
 (0)