forked from nosami/visualfsharp
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ChangeToUpcast code fix (dotnet#10463)
- Loading branch information
Showing
16 changed files
with
192 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.VisualStudio.FSharp.Editor | ||
|
||
open System.Composition | ||
open System.Threading.Tasks | ||
|
||
open Microsoft.CodeAnalysis.Text | ||
open Microsoft.CodeAnalysis.CodeFixes | ||
|
||
[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "ChangeToUpcast"); Shared>] | ||
type internal FSharpChangeToUpcastCodeFixProvider() = | ||
inherit CodeFixProvider() | ||
|
||
let fixableDiagnosticIds = set ["FS3198"] | ||
|
||
override __.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds | ||
|
||
override this.RegisterCodeFixesAsync context : Task = | ||
asyncMaybe { | ||
let! sourceText = context.Document.GetTextAsync(context.CancellationToken) | ||
let text = sourceText.GetSubText(context.Span).ToString() | ||
|
||
// Only works if it's one or the other | ||
let isDowncastOperator = text.Contains(":?>") | ||
let isDowncastKeyword = text.Contains("downcast") | ||
do! Option.guard ((isDowncastOperator || isDowncastKeyword) && not (isDowncastOperator && isDowncastKeyword)) | ||
|
||
let replacement = | ||
if isDowncastOperator then | ||
text.Replace(":?>", ":>") | ||
else | ||
text.Replace("downcast", "upcast") | ||
|
||
let title = | ||
if isDowncastOperator then | ||
SR.UseUpcastOperator() | ||
else | ||
SR.UseUpcastKeyword() | ||
|
||
let diagnostics = | ||
context.Diagnostics | ||
|> Seq.filter (fun x -> fixableDiagnosticIds |> Set.contains x.Id) | ||
|> Seq.toImmutableArray | ||
|
||
let codeFix = | ||
CodeFixHelpers.createTextChangeCodeFix( | ||
title, | ||
context, | ||
(fun () -> asyncMaybe.Return [| TextChange(context.Span, replacement) |])) | ||
|
||
context.RegisterCodeFix(codeFix, diagnostics) | ||
} | ||
|> Async.Ignore | ||
|> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters