Skip to content

Commit f35fa1a

Browse files
authored
New codefix provider to remove superflous binding for a Union case that has 0 fields. (#14267)
1 parent 96cf79f commit f35fa1a

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

vsintegration/src/FSharp.Editor/CodeFix/CodeFixHelpers.fs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,18 @@ module internal CodeFixHelpers =
2222
| Some textChanges -> return context.Document.WithText(sourceText.WithChanges(textChanges))
2323
} |> RoslynHelpers.StartAsyncAsTask(cancellationToken)),
2424
title)
25+
26+
[<AutoOpen>]
27+
module internal CodeFixExtensions =
28+
type CodeFixProvider with
29+
member this.GetPrunedDiagnostics(context: CodeFixContext) =
30+
context.Diagnostics.RemoveAll(fun x -> this.FixableDiagnosticIds.Contains(x.Id) |> not)
31+
32+
member this.RegisterFix(context: CodeFixContext, fixName, fixChange) =
33+
let replaceCodeFix =
34+
CodeFixHelpers.createTextChangeCodeFix(
35+
fixName,
36+
context,
37+
(fun () -> asyncMaybe.Return [| fixChange |]))
38+
context.RegisterCodeFix(replaceCodeFix, this.GetPrunedDiagnostics(context))
39+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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)

vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
<Compile Include="CodeFix\AddOpenCodeFixProvider.fs" />
115115
<Compile Include="CodeFix\ProposeUppercaseLabel.fs" />
116116
<Compile Include="CodeFix\ReplaceWithSuggestion.fs" />
117+
<Compile Include="CodeFix\RemoveSuperflousCaptureForUnionCaseWithNoData.fs" />
117118
<Compile Include="CodeFix\RemoveUnusedBinding.fs" />
118119
<Compile Include="CodeFix\RenameUnusedValue.fs" />
119120
<Compile Include="CodeFix\ImplementInterfaceCodeFixProvider.fs" />

0 commit comments

Comments
 (0)