-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathFSharpDocumentDiagnosticAnalyzer.cs
104 lines (86 loc) · 4.09 KB
/
FSharpDocumentDiagnosticAnalyzer.cs
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#nullable disable
using System;
using System.Collections.Immutable;
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
using Microsoft.CodeAnalysis.Options;
namespace Microsoft.CodeAnalysis.ExternalAccess.FSharp.Internal.Diagnostics
{
[Shared]
[ExportLanguageService(typeof(FSharpDocumentDiagnosticAnalyzerService), LanguageNames.FSharp)]
internal class FSharpDocumentDiagnosticAnalyzerService : ILanguageService
{
private readonly IFSharpDocumentDiagnosticAnalyzer _analyzer;
[ImportingConstructor]
[Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
public FSharpDocumentDiagnosticAnalyzerService(IFSharpDocumentDiagnosticAnalyzer analyzer)
{
_analyzer = analyzer;
}
public Task<ImmutableArray<Diagnostic>> AnalyzeSemanticsAsync(Document document, CancellationToken cancellationToken)
{
return _analyzer.AnalyzeSemanticsAsync(document, cancellationToken);
}
public Task<ImmutableArray<Diagnostic>> AnalyzeSyntaxAsync(Document document, CancellationToken cancellationToken)
{
return _analyzer.AnalyzeSyntaxAsync(document, cancellationToken);
}
}
[DiagnosticAnalyzer(LanguageNames.FSharp)]
internal class FSharpDocumentDiagnosticAnalyzer : DocumentDiagnosticAnalyzer, IBuiltInAnalyzer
{
private readonly ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics;
public FSharpDocumentDiagnosticAnalyzer()
{
_supportedDiagnostics = CreateSupportedDiagnostics();
}
public static ImmutableArray<DiagnosticDescriptor> CreateSupportedDiagnostics()
{
// We are constructing our own descriptors at run-time. Compiler service is already doing error formatting and localization.
var dummyDescriptors = ImmutableArray.CreateBuilder<DiagnosticDescriptor>();
for (var i = 0; i <= 10000; i++)
{
dummyDescriptors.Add(new DiagnosticDescriptor(String.Format("FS{0:D4}", i), String.Empty, String.Empty, String.Empty, DiagnosticSeverity.Error, true, null, null));
}
return dummyDescriptors.ToImmutable();
}
public CodeActionRequestPriority RequestPriority => CodeActionRequestPriority.Normal;
public override int Priority => 10; // Default = 50
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => _supportedDiagnostics;
public override Task<ImmutableArray<Diagnostic>> AnalyzeSemanticsAsync(Document document, CancellationToken cancellationToken)
{
var analyzer = document.Project.LanguageServices.GetService<FSharpDocumentDiagnosticAnalyzerService>();
if (analyzer == null)
{
return Task.FromResult(ImmutableArray<Diagnostic>.Empty);
}
return analyzer.AnalyzeSemanticsAsync(document, cancellationToken);
}
public override Task<ImmutableArray<Diagnostic>> AnalyzeSyntaxAsync(Document document, CancellationToken cancellationToken)
{
var analyzer = document.Project.LanguageServices.GetService<FSharpDocumentDiagnosticAnalyzerService>();
if (analyzer == null)
{
return Task.FromResult(ImmutableArray<Diagnostic>.Empty);
}
return analyzer.AnalyzeSyntaxAsync(document, cancellationToken);
}
public DiagnosticAnalyzerCategory GetAnalyzerCategory()
{
return DiagnosticAnalyzerCategory.SemanticDocumentAnalysis;
}
public bool OpenFileOnly(OptionSet options)
{
return true;
}
}
}