This repository was archived by the owner on Apr 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathQuickFixCodeActionSource.cs
More file actions
77 lines (64 loc) · 3.36 KB
/
Copy pathQuickFixCodeActionSource.cs
File metadata and controls
77 lines (64 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright(c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the License); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABILITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Python.Analysis;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Core;
using Microsoft.Python.Core.Collections;
using Microsoft.Python.LanguageServer.CodeActions;
using Microsoft.Python.LanguageServer.Protocol;
using Microsoft.Python.Parsing.Ast;
namespace Microsoft.Python.LanguageServer.Sources {
internal sealed partial class QuickFixCodeActionSource {
private static readonly ImmutableArray<IQuickFixCodeActionProvider> _codeActionProviders =
ImmutableArray<IQuickFixCodeActionProvider>.Create(MissingImportCodeActionProvider.Instance);
private readonly IServiceContainer _services;
public QuickFixCodeActionSource(IServiceContainer services) {
_services = services;
}
public async Task<CodeAction[]> GetCodeActionsAsync(IDocumentAnalysis analysis, CodeActionSettings settings, Diagnostic[] diagnostics, CancellationToken cancellationToken) {
var results = new List<CodeAction>();
foreach (var diagnostic in GetMatchingDiagnostics(analysis, diagnostics, cancellationToken)) {
foreach (var codeActionProvider in _codeActionProviders) {
cancellationToken.ThrowIfCancellationRequested();
if (codeActionProvider.FixableDiagnostics.Any(code => code == diagnostic.ErrorCode)) {
results.AddRange(await codeActionProvider.GetCodeActionsAsync(analysis, settings, diagnostic, cancellationToken));
}
}
}
return results.ToArray();
}
private IEnumerable<DiagnosticsEntry> GetMatchingDiagnostics(IDocumentAnalysis analysis, Diagnostic[] diagnostics, CancellationToken cancellationToken) {
var diagnosticService = _services.GetService<IDiagnosticsService>();
// we assume diagnostic service has the latest results
if (diagnosticService == null || !diagnosticService.Diagnostics.TryGetValue(analysis.Document.Uri, out var latestDiagnostics)) {
yield break;
}
foreach (var diagnostic in latestDiagnostics) {
cancellationToken.ThrowIfCancellationRequested();
if (diagnostics.Any(d => AreEqual(d, diagnostic))) {
yield return diagnostic;
}
}
bool AreEqual(Diagnostic diagnostic1, DiagnosticsEntry diagnostic2) {
return diagnostic1.code == diagnostic2.ErrorCode &&
diagnostic1.range.ToSourceSpan() == diagnostic2.SourceSpan;
}
}
}
}