From 49e1b3a3a7bb87e68cb0417e6b61b61fdba774fe Mon Sep 17 00:00:00 2001 From: Phillip Carter Date: Wed, 18 Nov 2020 11:49:38 -0800 Subject: [PATCH] Add ChangeToUpcast code fix (#10463) --- CodeFix/ChangeToUpcast.fs | 55 +++++++++++++++++++++++++++++++++++ FSharp.Editor.fsproj | 1 + FSharp.Editor.resx | 6 ++++ xlf/FSharp.Editor.cs.xlf | 10 +++++++ xlf/FSharp.Editor.de.xlf | 10 +++++++ xlf/FSharp.Editor.es.xlf | 10 +++++++ xlf/FSharp.Editor.fr.xlf | 10 +++++++ xlf/FSharp.Editor.it.xlf | 10 +++++++ xlf/FSharp.Editor.ja.xlf | 10 +++++++ xlf/FSharp.Editor.ko.xlf | 10 +++++++ xlf/FSharp.Editor.pl.xlf | 10 +++++++ xlf/FSharp.Editor.pt-BR.xlf | 10 +++++++ xlf/FSharp.Editor.ru.xlf | 10 +++++++ xlf/FSharp.Editor.tr.xlf | 10 +++++++ xlf/FSharp.Editor.zh-Hans.xlf | 10 +++++++ xlf/FSharp.Editor.zh-Hant.xlf | 10 +++++++ 16 files changed, 192 insertions(+) create mode 100644 CodeFix/ChangeToUpcast.fs diff --git a/CodeFix/ChangeToUpcast.fs b/CodeFix/ChangeToUpcast.fs new file mode 100644 index 00000000000..cc28cdc0a52 --- /dev/null +++ b/CodeFix/ChangeToUpcast.fs @@ -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 + +[] +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) diff --git a/FSharp.Editor.fsproj b/FSharp.Editor.fsproj index 2ce6fb25e9a..4d8542a4495 100644 --- a/FSharp.Editor.fsproj +++ b/FSharp.Editor.fsproj @@ -184,6 +184,7 @@ + diff --git a/FSharp.Editor.resx b/FSharp.Editor.resx index 62497078788..fbfd462a09e 100644 --- a/FSharp.Editor.resx +++ b/FSharp.Editor.resx @@ -219,6 +219,12 @@ F# Dispostable Values (top-level) + + Use 'upcast' + + + Use ':>' operator + Add missing '=' to type definition diff --git a/xlf/FSharp.Editor.cs.xlf b/xlf/FSharp.Editor.cs.xlf index 14b804cc247..cfc92dc856e 100644 --- a/xlf/FSharp.Editor.cs.xlf +++ b/xlf/FSharp.Editor.cs.xlf @@ -187,6 +187,16 @@ Formátování + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.de.xlf b/xlf/FSharp.Editor.de.xlf index 4aa11aa5540..18b949208c7 100644 --- a/xlf/FSharp.Editor.de.xlf +++ b/xlf/FSharp.Editor.de.xlf @@ -187,6 +187,16 @@ Formatierung + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.es.xlf b/xlf/FSharp.Editor.es.xlf index d2adca7d01a..78f2b99a234 100644 --- a/xlf/FSharp.Editor.es.xlf +++ b/xlf/FSharp.Editor.es.xlf @@ -187,6 +187,16 @@ Formato + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.fr.xlf b/xlf/FSharp.Editor.fr.xlf index ae54a946ed6..9c18b60164b 100644 --- a/xlf/FSharp.Editor.fr.xlf +++ b/xlf/FSharp.Editor.fr.xlf @@ -187,6 +187,16 @@ Mise en forme + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.it.xlf b/xlf/FSharp.Editor.it.xlf index 9df3852f0cd..560bb871c5c 100644 --- a/xlf/FSharp.Editor.it.xlf +++ b/xlf/FSharp.Editor.it.xlf @@ -187,6 +187,16 @@ Formattazione + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.ja.xlf b/xlf/FSharp.Editor.ja.xlf index d141030be81..0a6e6dc43c5 100644 --- a/xlf/FSharp.Editor.ja.xlf +++ b/xlf/FSharp.Editor.ja.xlf @@ -187,6 +187,16 @@ 書式設定 + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.ko.xlf b/xlf/FSharp.Editor.ko.xlf index b2e8082b7fc..1951db13fab 100644 --- a/xlf/FSharp.Editor.ko.xlf +++ b/xlf/FSharp.Editor.ko.xlf @@ -187,6 +187,16 @@ 서식 + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.pl.xlf b/xlf/FSharp.Editor.pl.xlf index 4acc383f4bb..7b903f93fa3 100644 --- a/xlf/FSharp.Editor.pl.xlf +++ b/xlf/FSharp.Editor.pl.xlf @@ -187,6 +187,16 @@ Formatowanie + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.pt-BR.xlf b/xlf/FSharp.Editor.pt-BR.xlf index 8256c04823f..30a5191c9d9 100644 --- a/xlf/FSharp.Editor.pt-BR.xlf +++ b/xlf/FSharp.Editor.pt-BR.xlf @@ -187,6 +187,16 @@ Formatação + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.ru.xlf b/xlf/FSharp.Editor.ru.xlf index 4df93cd671a..29fb73ca1bd 100644 --- a/xlf/FSharp.Editor.ru.xlf +++ b/xlf/FSharp.Editor.ru.xlf @@ -187,6 +187,16 @@ Форматирование + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.tr.xlf b/xlf/FSharp.Editor.tr.xlf index c42645b2e76..10b88fe3681 100644 --- a/xlf/FSharp.Editor.tr.xlf +++ b/xlf/FSharp.Editor.tr.xlf @@ -187,6 +187,16 @@ Biçimlendirme + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.zh-Hans.xlf b/xlf/FSharp.Editor.zh-Hans.xlf index ec5e69bca10..c17436ace25 100644 --- a/xlf/FSharp.Editor.zh-Hans.xlf +++ b/xlf/FSharp.Editor.zh-Hans.xlf @@ -187,6 +187,16 @@ 正在格式化 + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression diff --git a/xlf/FSharp.Editor.zh-Hant.xlf b/xlf/FSharp.Editor.zh-Hant.xlf index acccd1d14d4..29a418e0050 100644 --- a/xlf/FSharp.Editor.zh-Hant.xlf +++ b/xlf/FSharp.Editor.zh-Hant.xlf @@ -187,6 +187,16 @@ 格式化 + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + Use 'not' to negate expression Use 'not' to negate expression