diff --git a/src/VisualStudio/ExternalAccess/FSharp/Diagnostics/IFSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs b/src/VisualStudio/ExternalAccess/FSharp/Diagnostics/IFSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs new file mode 100644 index 0000000000000..27d05e4ac5f73 --- /dev/null +++ b/src/VisualStudio/ExternalAccess/FSharp/Diagnostics/IFSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs @@ -0,0 +1,14 @@ +// 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. + +using System.Collections.Immutable; +using System.Threading; +using System.Threading.Tasks; + +namespace Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics; + +internal interface IFSharpUnnecessaryParenthesesDiagnosticAnalyzer +{ + Task> AnalyzeSyntaxAsync(DiagnosticDescriptor descriptor, Document document, CancellationToken cancellationToken); +} diff --git a/src/VisualStudio/ExternalAccess/FSharp/Internal/Diagnostics/FSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs b/src/VisualStudio/ExternalAccess/FSharp/Internal/Diagnostics/FSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs new file mode 100644 index 0000000000000..c2693e1242405 --- /dev/null +++ b/src/VisualStudio/ExternalAccess/FSharp/Internal/Diagnostics/FSharpUnnecessaryParenthesesDiagnosticAnalyzer.cs @@ -0,0 +1,54 @@ +// 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. + +using System; +using System.Collections.Immutable; +using System.Composition; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.CodeAnalysis.Diagnostics; +using Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics; +using Microsoft.CodeAnalysis.Host; +using Microsoft.CodeAnalysis.Host.Mef; + +namespace Microsoft.CodeAnalysis.ExternalAccess.FSharp.Internal.Diagnostics; + +[Shared] +[ExportLanguageService(typeof(FSharpUnnecessaryParenthesesDiagnosticAnalyzerService), LanguageNames.FSharp)] +[method: ImportingConstructor] +[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)] +internal sealed class FSharpUnnecessaryParenthesesDiagnosticAnalyzerService(IFSharpUnnecessaryParenthesesDiagnosticAnalyzer analyzer) : ILanguageService +{ + public Task> AnalyzeSyntaxAsync(DiagnosticDescriptor descriptor, Document document, CancellationToken cancellationToken) + => analyzer.AnalyzeSyntaxAsync(descriptor, document, cancellationToken); +} + +[DiagnosticAnalyzer(LanguageNames.FSharp)] +internal sealed class FSharpUnnecessaryParenthesesDiagnosticAnalyzer : DocumentDiagnosticAnalyzer, IBuiltInAnalyzer +{ + private readonly DiagnosticDescriptor _descriptor = + new( + IDEDiagnosticIds.RemoveUnnecessaryParenthesesDiagnosticId, + new LocalizableResourceString(nameof(AnalyzersResources.Remove_unnecessary_parentheses), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)), + new LocalizableResourceString(nameof(AnalyzersResources.Parentheses_can_be_removed), AnalyzersResources.ResourceManager, typeof(AnalyzersResources)), + DiagnosticCategory.Style, + DiagnosticSeverity.Hidden, + isEnabledByDefault: true, + customTags: FSharpDiagnosticCustomTags.Unnecessary); + + public FSharpUnnecessaryParenthesesDiagnosticAnalyzer() => SupportedDiagnostics = [_descriptor]; + + public override ImmutableArray SupportedDiagnostics { get; } + + public override Task> AnalyzeSemanticsAsync(Document document, CancellationToken cancellationToken) + => Task.FromResult>([]); + + public override Task> AnalyzeSyntaxAsync(Document document, CancellationToken cancellationToken) + => document.Project.Services.GetService()?.AnalyzeSyntaxAsync(_descriptor, document, cancellationToken) + ?? Task.FromResult>([]); + + public bool IsHighPriority => false; + + public DiagnosticAnalyzerCategory GetAnalyzerCategory() => DiagnosticAnalyzerCategory.SyntaxTreeWithoutSemanticsAnalysis; +} diff --git a/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt b/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt index f5bd0244c11a9..8d215106500e2 100644 --- a/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt +++ b/src/VisualStudio/ExternalAccess/FSharp/InternalAPI.Unshipped.txt @@ -27,6 +27,8 @@ Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.FSharpIDEDiagnosticIds Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpDiagnosticAnalyzerService Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpDocumentDiagnosticAnalyzer Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpSimplifyNameDiagnosticAnalyzer +Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpUnnecessaryParenthesesDiagnosticAnalyzer +Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpUnnecessaryParenthesesDiagnosticAnalyzer.AnalyzeSyntaxAsync(Microsoft.CodeAnalysis.DiagnosticDescriptor! descriptor, Microsoft.CodeAnalysis.Document! document, System.Threading.CancellationToken cancellationToken) -> System.Threading.Tasks.Task>! Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpUnusedDeclarationsDiagnosticAnalyzer Microsoft.CodeAnalysis.ExternalAccess.FSharp.Diagnostics.IFSharpUnusedOpensDiagnosticAnalyzer Microsoft.CodeAnalysis.ExternalAccess.FSharp.DocumentHighlighting.FSharpDocumentHighlights