|
| 1 | +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +namespace Microsoft.VisualStudio.FSharp.Editor |
| 4 | + |
| 5 | +open System |
| 6 | +open System.Composition |
| 7 | +open System.Threading.Tasks |
| 8 | + |
| 9 | +open Microsoft.CodeAnalysis.Text |
| 10 | +open Microsoft.CodeAnalysis.CodeFixes |
| 11 | + |
| 12 | +open FSharp.Compiler |
| 13 | +open FSharp.Compiler.CodeAnalysis |
| 14 | +open FSharp.Compiler.Symbols |
| 15 | +open FSharp.Compiler.Syntax |
| 16 | +open FSharp.Compiler.EditorServices |
| 17 | + |
| 18 | +[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "RemoveSuperflousCapture"); Shared>] |
| 19 | +type internal RemoveSuperflousCaptureForUnionCaseWithNoDataProvider |
| 20 | + [<ImportingConstructor>] |
| 21 | + ( |
| 22 | + ) = |
| 23 | + |
| 24 | + inherit CodeFixProvider() |
| 25 | + |
| 26 | + override _.FixableDiagnosticIds = Seq.toImmutableArray ["FS0725";"FS3548"] |
| 27 | + |
| 28 | + override this.RegisterCodeFixesAsync context : Task = |
| 29 | + asyncMaybe { |
| 30 | + do! Option.guard context.Document.Project.IsFSharpCodeFixesUnusedDeclarationsEnabled |
| 31 | + |
| 32 | + let document = context.Document |
| 33 | + let! sourceText = document.GetTextAsync(context.CancellationToken) |
| 34 | + let! _, checkResults = document.GetFSharpParseAndCheckResultsAsync(nameof(RemoveSuperflousCaptureForUnionCaseWithNoDataProvider)) |> liftAsync |
| 35 | + let m = RoslynHelpers.TextSpanToFSharpRange(document.FilePath, context.Span, sourceText) |
| 36 | + let classifications = checkResults.GetSemanticClassification(Some m) |
| 37 | + let unionCaseItem = |
| 38 | + classifications |
| 39 | + |> Array.tryFind (fun c -> c.Type = SemanticClassificationType.UnionCase) |
| 40 | + |
| 41 | + match unionCaseItem with |
| 42 | + | None -> () |
| 43 | + | Some unionCaseItem -> |
| 44 | + // 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". |
| 45 | + let typeInfoLength = unionCaseItem.Range.EndColumn - m.StartColumn |
| 46 | + let reminderSpan = new TextSpan(context.Span.Start + typeInfoLength, context.Span.Length - typeInfoLength) |
| 47 | + this.RegisterFix(context, SR.RemoveUnusedBinding(), TextChange(reminderSpan, "")) |
| 48 | + } |
| 49 | + |> Async.Ignore |
| 50 | + |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken) |
0 commit comments