未验证 提交 f35fa1a1 编写于 作者: T Tomas Grosup 提交者: GitHub

New codefix provider to remove superflous binding for a Union case that has 0 fields. (#14267)

上级 96cf79f2
......@@ -22,3 +22,18 @@ module internal CodeFixHelpers =
| Some textChanges -> return context.Document.WithText(sourceText.WithChanges(textChanges))
} |> RoslynHelpers.StartAsyncAsTask(cancellationToken)),
title)
[<AutoOpen>]
module internal CodeFixExtensions =
type CodeFixProvider with
member this.GetPrunedDiagnostics(context: CodeFixContext) =
context.Diagnostics.RemoveAll(fun x -> this.FixableDiagnosticIds.Contains(x.Id) |> not)
member this.RegisterFix(context: CodeFixContext, fixName, fixChange) =
let replaceCodeFix =
CodeFixHelpers.createTextChangeCodeFix(
fixName,
context,
(fun () -> asyncMaybe.Return [| fixChange |]))
context.RegisterCodeFix(replaceCodeFix, this.GetPrunedDiagnostics(context))
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
namespace Microsoft.VisualStudio.FSharp.Editor
open System
open System.Composition
open System.Threading.Tasks
open Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes
open FSharp.Compiler
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Symbols
open FSharp.Compiler.Syntax
open FSharp.Compiler.EditorServices
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "RemoveSuperflousCapture"); Shared>]
type internal RemoveSuperflousCaptureForUnionCaseWithNoDataProvider
[<ImportingConstructor>]
(
) =
inherit CodeFixProvider()
override _.FixableDiagnosticIds = Seq.toImmutableArray ["FS0725";"FS3548"]
override this.RegisterCodeFixesAsync context : Task =
asyncMaybe {
do! Option.guard context.Document.Project.IsFSharpCodeFixesUnusedDeclarationsEnabled
let document = context.Document
let! sourceText = document.GetTextAsync(context.CancellationToken)
let! _, checkResults = document.GetFSharpParseAndCheckResultsAsync(nameof(RemoveSuperflousCaptureForUnionCaseWithNoDataProvider)) |> liftAsync
let m = RoslynHelpers.TextSpanToFSharpRange(document.FilePath, context.Span, sourceText)
let classifications = checkResults.GetSemanticClassification(Some m)
let unionCaseItem =
classifications
|> Array.tryFind (fun c -> c.Type = SemanticClassificationType.UnionCase)
match unionCaseItem with
| None -> ()
| Some unionCaseItem ->
// The error/warning captures entire pattern match, like "Ns.Type.DuName bindingName". We want to keep type info when suggesting a replacement, and only remove "bindingName".
let typeInfoLength = unionCaseItem.Range.EndColumn - m.StartColumn
let reminderSpan = new TextSpan(context.Span.Start + typeInfoLength, context.Span.Length - typeInfoLength)
this.RegisterFix(context, SR.RemoveUnusedBinding(), TextChange(reminderSpan, ""))
}
|> Async.Ignore
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)
......@@ -114,6 +114,7 @@
<Compile Include="CodeFix\AddOpenCodeFixProvider.fs" />
<Compile Include="CodeFix\ProposeUppercaseLabel.fs" />
<Compile Include="CodeFix\ReplaceWithSuggestion.fs" />
<Compile Include="CodeFix\RemoveSuperflousCaptureForUnionCaseWithNoData.fs" />
<Compile Include="CodeFix\RemoveUnusedBinding.fs" />
<Compile Include="CodeFix\RenameUnusedValue.fs" />
<Compile Include="CodeFix\ImplementInterfaceCodeFixProvider.fs" />
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册