diff --git a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs index a0d2f77fc..728891e65 100644 --- a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs +++ b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs @@ -208,14 +208,12 @@ public async Task GetCommentHelpText(string functionText, string helpLoc ScriptFileMarker[] analysisResults = await AnalysisEngine.AnalyzeScriptAsync(functionText, commentHelpSettings).ConfigureAwait(false); - if (analysisResults.Length == 0 - || analysisResults[0]?.Correction?.Edits == null - || analysisResults[0].Correction.Edits.Length == 0) + if (analysisResults.Length == 0 || !analysisResults[0].Corrections.Any()) { return null; } - return analysisResults[0].Correction.Edits[0].Text; + return analysisResults[0].Corrections.First().Edit.Text; } /// @@ -223,7 +221,7 @@ public async Task GetCommentHelpText(string functionText, string helpLoc /// /// The URI string of the file to get code actions for. /// A threadsafe readonly dictionary of the code actions of the particular file. - public async Task> GetMostRecentCodeActionsForFileAsync(DocumentUri uri) + public async Task>> GetMostRecentCodeActionsForFileAsync(DocumentUri uri) { if (!_workspaceService.TryGetFile(uri, out ScriptFile file) || !_mostRecentCorrectionsByFile.TryGetValue(file, out CorrectionTableEntry corrections)) @@ -404,10 +402,10 @@ private void PublishScriptDiagnostics(ScriptFile scriptFile, IReadOnlyList(); + Corrections = new ConcurrentDictionary>(); DiagnosticPublish = Task.CompletedTask; } - public ConcurrentDictionary Corrections { get; } + public ConcurrentDictionary> Corrections { get; } public Task DiagnosticPublish { get; set; } } diff --git a/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs b/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs index 50de27462..2fe88da96 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/Handlers/CodeActionHandler.cs @@ -52,7 +52,7 @@ public override async Task Handle(CodeActionParams return Array.Empty(); } - IReadOnlyDictionary corrections = await _analysisService.GetMostRecentCodeActionsForFileAsync( + IReadOnlyDictionary> corrections = await _analysisService.GetMostRecentCodeActionsForFileAsync( request.TextDocument.Uri) .ConfigureAwait(false); @@ -75,13 +75,13 @@ public override async Task Handle(CodeActionParams } string diagnosticId = AnalysisService.GetUniqueIdFromDiagnostic(diagnostic); - if (corrections.TryGetValue(diagnosticId, out MarkerCorrection correction)) + if (corrections.TryGetValue(diagnosticId, out IEnumerable markerCorrections)) { - foreach (ScriptRegion edit in correction.Edits) + foreach (MarkerCorrection markerCorrection in markerCorrections) { codeActions.Add(new CodeAction { - Title = correction.Name, + Title = markerCorrection.Name, Kind = CodeActionKind.QuickFix, Edit = new WorkspaceEdit { @@ -93,7 +93,7 @@ public override async Task Handle(CodeActionParams { Uri = request.TextDocument.Uri }, - Edits = new TextEditContainer(ScriptRegion.ToTextEdit(edit)) + Edits = new TextEditContainer(ScriptRegion.ToTextEdit(markerCorrection.Edit)) })) } }); diff --git a/src/PowerShellEditorServices/Services/TextDocument/ScriptFileMarker.cs b/src/PowerShellEditorServices/Services/TextDocument/ScriptFileMarker.cs index 8d7c24ca3..6dccca37a 100644 --- a/src/PowerShellEditorServices/Services/TextDocument/ScriptFileMarker.cs +++ b/src/PowerShellEditorServices/Services/TextDocument/ScriptFileMarker.cs @@ -21,9 +21,9 @@ public sealed class MarkerCorrection public string Name { get; set; } /// - /// Gets or sets the list of ScriptRegions that define the edits to be made by the correction. + /// Gets or sets the ScriptRegion that define the edit to be made by the correction. /// - public ScriptRegion[] Edits { get; set; } + public ScriptRegion Edit { get; set; } } /// @@ -79,9 +79,9 @@ public class ScriptFileMarker public ScriptRegion ScriptRegion { get; set; } /// - /// Gets or sets an optional code correction that can be applied based on this marker. + /// Gets or sets a optional code corrections that can be applied based on its marker. /// - public MarkerCorrection Correction { get; set; } + public IEnumerable Corrections { get; set; } /// /// Gets or sets the name of the marker's source like "PowerShell" @@ -110,7 +110,6 @@ internal static ScriptFileMarker FromParseError( internal static ScriptFileMarker FromDiagnosticRecord(PSObject psObject) { Validate.IsNotNull(nameof(psObject), psObject); - MarkerCorrection correction = null; // make sure psobject is of type DiagnosticRecord if (!psObject.TypeNames.Contains( @@ -124,32 +123,25 @@ internal static ScriptFileMarker FromDiagnosticRecord(PSObject psObject) // the diagnostic record's properties directly i.e. . // without having to go through PSObject's Members property. dynamic diagnosticRecord = psObject; - + List markerCorrections = new(); if (diagnosticRecord.SuggestedCorrections != null) { - List editRegions = new(); - string correctionMessage = null; foreach (dynamic suggestedCorrection in diagnosticRecord.SuggestedCorrections) { - editRegions.Add( - new ScriptRegion( - diagnosticRecord.ScriptPath, - suggestedCorrection.Text, - startLineNumber: suggestedCorrection.StartLineNumber, - startColumnNumber: suggestedCorrection.StartColumnNumber, - endLineNumber: suggestedCorrection.EndLineNumber, - endColumnNumber: suggestedCorrection.EndColumnNumber, - startOffset: -1, - endOffset: -1)); - - correctionMessage = suggestedCorrection.Description; + markerCorrections.Add(new MarkerCorrection + { + Name = suggestedCorrection.Description ?? diagnosticRecord.Message, + Edit = new ScriptRegion( + diagnosticRecord.ScriptPath, + suggestedCorrection.Text, + startLineNumber: suggestedCorrection.StartLineNumber, + startColumnNumber: suggestedCorrection.StartColumnNumber, + startOffset: -1, + endLineNumber: suggestedCorrection.EndLineNumber, + endColumnNumber: suggestedCorrection.EndColumnNumber, + endOffset: -1), + }); } - - correction = new MarkerCorrection - { - Name = correctionMessage ?? diagnosticRecord.Message, - Edits = editRegions.ToArray() - }; } string severity = diagnosticRecord.Severity.ToString(); @@ -166,7 +158,7 @@ internal static ScriptFileMarker FromDiagnosticRecord(PSObject psObject) RuleName = diagnosticRecord.RuleName as string ?? string.Empty, Level = level, ScriptRegion = ScriptRegion.Create(diagnosticRecord.Extent as IScriptExtent), - Correction = correction, + Corrections = markerCorrections, Source = "PSScriptAnalyzer" }; }