Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Commit ac1b856

Browse files
dsymeKevinRansom
authored andcommitted
implement check and codefix for mismatched parameters (dotnet#3527)
Issue opened: dotnet#3539
1 parent 1310b03 commit ac1b856

16 files changed

+144
-64
lines changed

CodeFix/AddNewKeywordToDisposableConstructorInvocation.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
namespace rec Microsoft.VisualStudio.FSharp.Editor
3+
namespace Microsoft.VisualStudio.FSharp.Editor
44

55
open System.Composition
66
open System.Collections.Immutable

CodeFix/AddOpenCodeFixProvider.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
namespace rec Microsoft.VisualStudio.FSharp.Editor
3+
namespace Microsoft.VisualStudio.FSharp.Editor
44

55
open System
66
open System.Composition

CodeFix/FixIndexerAccess.fs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
namespace rec Microsoft.VisualStudio.FSharp.Editor
3+
namespace Microsoft.VisualStudio.FSharp.Editor
44

55
open System
66
open System.Composition
77
open System.Collections.Immutable
8-
open System.Threading
98
open System.Threading.Tasks
109

1110
open Microsoft.CodeAnalysis
1211
open Microsoft.CodeAnalysis.Text
1312
open Microsoft.CodeAnalysis.CodeFixes
14-
open Microsoft.CodeAnalysis.CodeActions
13+
open SymbolHelpers
1514

1615
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "FixIndexerAccess"); Shared>]
1716
type internal FSharpFixIndexerAccessCodeFixProvider() =
1817
inherit CodeFixProvider()
1918
let fixableDiagnosticIds = set ["FS3217"]
2019

21-
let createCodeFix (title: string, context: CodeFixContext, textChange: TextChange) =
22-
CodeAction.Create(
23-
title,
24-
(fun (cancellationToken: CancellationToken) ->
25-
async {
26-
let! cancellationToken = Async.CancellationToken
27-
let! sourceText = context.Document.GetTextAsync(cancellationToken) |> Async.AwaitTask
28-
return context.Document.WithText(sourceText.WithChanges(textChange))
29-
} |> RoslynHelpers.StartAsyncAsTask(cancellationToken)),
30-
title)
31-
3220
override __.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds
3321

3422
override __.RegisterCodeFixesAsync context : Task =
@@ -60,9 +48,10 @@ type internal FSharpFixIndexerAccessCodeFixProvider() =
6048
| _ -> context.Span,sourceText.GetSubText(context.Span).ToString()
6149

6250
let codefix =
63-
createCodeFix(
51+
createTextChangeCodeFix(
6452
FSComp.SR.addIndexerDot(),
6553
context,
66-
TextChange(span, replacement.TrimEnd() + "."))
54+
(fun () -> asyncMaybe.Return [| TextChange(span, replacement.TrimEnd() + ".") |]))
55+
6756
context.RegisterCodeFix(codefix, diagnostics))
6857
} |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)

CodeFix/ImplementInterfaceCodeFixProvider.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
namespace rec Microsoft.VisualStudio.FSharp.Editor
3+
namespace Microsoft.VisualStudio.FSharp.Editor
44

55
open System
66
open System.Composition

CodeFix/MissingReferenceCodeFixProvider.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
namespace rec Microsoft.VisualStudio.FSharp.Editor
3+
namespace Microsoft.VisualStudio.FSharp.Editor
44

55
open System
66
open System.Composition

CodeFix/ProposeUppercaseLabel.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
namespace rec Microsoft.VisualStudio.FSharp.Editor
3+
namespace Microsoft.VisualStudio.FSharp.Editor
44

55
open System.Composition
66
open System.Threading.Tasks

CodeFix/RemoveUnusedOpens.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
namespace rec Microsoft.VisualStudio.FSharp.Editor
3+
namespace Microsoft.VisualStudio.FSharp.Editor
44

55
open System.Composition
66
open System.Threading
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.Collections.Immutable
7+
open System.Composition
8+
open System.Threading.Tasks
9+
10+
open Microsoft.CodeAnalysis
11+
open Microsoft.CodeAnalysis.Text
12+
open Microsoft.CodeAnalysis.CodeFixes
13+
14+
open Microsoft.FSharp.Compiler
15+
open Microsoft.FSharp.Compiler.SourceCodeServices
16+
open Microsoft.VisualStudio.FSharp.Editor.SymbolHelpers
17+
open Microsoft.FSharp.Compiler.SourceCodeServices.Keywords
18+
19+
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "FSharpRenameParamToMatchSignature"); Shared>]
20+
type internal FSharpRenameParamToMatchSignature
21+
[<ImportingConstructor>]
22+
(
23+
checkerProvider: FSharpCheckerProvider,
24+
projectInfoManager: FSharpProjectOptionsManager
25+
) =
26+
27+
inherit CodeFixProvider()
28+
static let userOpName = "RenameParamToMatchSignature"
29+
let fixableDiagnosticIds = ["FS3218"]
30+
31+
32+
override __.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds
33+
34+
override __.RegisterCodeFixesAsync context : Task =
35+
asyncMaybe {
36+
match context.Diagnostics |> Seq.filter (fun x -> fixableDiagnosticIds |> List.contains x.Id) |> Seq.toList with
37+
| [diagnostic] ->
38+
let message = diagnostic.GetMessage()
39+
let parts = System.Text.RegularExpressions.Regex.Match(message, ".+'(.+)'.+'(.+)'.+")
40+
if parts.Success then
41+
42+
let diagnostics = ImmutableArray.Create diagnostic
43+
let suggestion = parts.Groups.[1].Value
44+
let replacement = QuoteIdentifierIfNeeded suggestion
45+
let computeChanges() =
46+
asyncMaybe {
47+
let document = context.Document
48+
let! cancellationToken = Async.CancellationToken |> liftAsync
49+
let! sourceText = document.GetTextAsync(cancellationToken)
50+
let! symbolUses = getSymbolUsesOfSymbolAtLocationInDocument (document, context.Span.Start, projectInfoManager, checkerProvider.Checker, userOpName)
51+
let changes =
52+
[| for symbolUse in symbolUses do
53+
match RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, symbolUse.RangeAlternate) with
54+
| None -> ()
55+
| Some span ->
56+
let textSpan = Tokenizer.fixupSpan(sourceText, span)
57+
yield TextChange(textSpan, replacement) |]
58+
return changes
59+
}
60+
let title = FSComp.SR.replaceWithSuggestion suggestion
61+
let codefix = createTextChangeCodeFix(title, context, computeChanges)
62+
context.RegisterCodeFix(codefix, diagnostics)
63+
| _ -> ()
64+
} |> Async.Ignore |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)

CodeFix/RenameUnusedValue.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
namespace rec Microsoft.VisualStudio.FSharp.Editor
3+
namespace Microsoft.VisualStudio.FSharp.Editor
44

55
open System
66
open System.Composition

CodeFix/ReplaceWithSuggestion.fs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,24 @@
11
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
22

3-
namespace rec Microsoft.VisualStudio.FSharp.Editor
3+
namespace Microsoft.VisualStudio.FSharp.Editor
44

55
open System
66
open System.Composition
77
open System.Collections.Immutable
8-
open System.Threading
98
open System.Threading.Tasks
109

1110
open Microsoft.CodeAnalysis
1211
open Microsoft.CodeAnalysis.Text
1312
open Microsoft.CodeAnalysis.CodeFixes
14-
open Microsoft.CodeAnalysis.CodeActions
13+
open SymbolHelpers
14+
open Microsoft.FSharp.Compiler.SourceCodeServices.Keywords
1515

1616
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "ReplaceWithSuggestion"); Shared>]
1717
type internal FSharpReplaceWithSuggestionCodeFixProvider() =
1818
inherit CodeFixProvider()
1919
let fixableDiagnosticIds = set ["FS0039"; "FS1129"; "FS0495"]
2020
let maybeString = FSComp.SR.undefinedNameSuggestionsIntro()
2121

22-
let createCodeFix (title: string, context: CodeFixContext, textChange: TextChange) =
23-
CodeAction.Create(
24-
title,
25-
(fun (cancellationToken: CancellationToken) ->
26-
async {
27-
let! cancellationToken = Async.CancellationToken
28-
let! sourceText = context.Document.GetTextAsync(cancellationToken) |> Async.AwaitTask
29-
return context.Document.WithText(sourceText.WithChanges(textChange))
30-
} |> RoslynHelpers.StartAsyncAsTask(cancellationToken)),
31-
title)
32-
3322
override __.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds
3423

3524
override __.RegisterCodeFixesAsync context : Task =
@@ -47,11 +36,11 @@ type internal FSharpReplaceWithSuggestionCodeFixProvider() =
4736
let diagnostics = ImmutableArray.Create diagnostic
4837

4938
for suggestion in suggestions do
50-
let replacement = if suggestion.Contains " " then "``" + suggestion + "``" else suggestion
39+
let replacement = QuoteIdentifierIfNeeded suggestion
5140
let codefix =
52-
createCodeFix(
41+
createTextChangeCodeFix(
5342
FSComp.SR.replaceWithSuggestion suggestion,
5443
context,
55-
TextChange(context.Span, replacement))
44+
(fun () -> asyncMaybe.Return [| TextChange(context.Span, replacement) |]))
5645
context.RegisterCodeFix(codefix, diagnostics))
5746
} |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)

0 commit comments

Comments
 (0)