From f4474eb34764abb14a37f33b034eeda1499425dc Mon Sep 17 00:00:00 2001 From: cartermp Date: Sat, 14 Nov 2020 14:18:02 -0800 Subject: [PATCH] Add ChangeToUpcast code fix --- .../FSharp.Editor/CodeFix/ChangeToUpcast.fs | 55 +++++++++++++++++++ .../src/FSharp.Editor/FSharp.Editor.fsproj | 1 + .../src/FSharp.Editor/FSharp.Editor.resx | 6 ++ .../FSharp.Editor/xlf/FSharp.Editor.cs.xlf | 10 ++++ .../FSharp.Editor/xlf/FSharp.Editor.de.xlf | 10 ++++ .../FSharp.Editor/xlf/FSharp.Editor.es.xlf | 10 ++++ .../FSharp.Editor/xlf/FSharp.Editor.fr.xlf | 10 ++++ .../FSharp.Editor/xlf/FSharp.Editor.it.xlf | 10 ++++ .../FSharp.Editor/xlf/FSharp.Editor.ja.xlf | 10 ++++ .../FSharp.Editor/xlf/FSharp.Editor.ko.xlf | 10 ++++ .../FSharp.Editor/xlf/FSharp.Editor.pl.xlf | 10 ++++ .../FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf | 10 ++++ .../FSharp.Editor/xlf/FSharp.Editor.ru.xlf | 10 ++++ .../FSharp.Editor/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 vsintegration/src/FSharp.Editor/CodeFix/ChangeToUpcast.fs diff --git a/vsintegration/src/FSharp.Editor/CodeFix/ChangeToUpcast.fs b/vsintegration/src/FSharp.Editor/CodeFix/ChangeToUpcast.fs new file mode 100644 index 00000000000..cc28cdc0a52 --- /dev/null +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj b/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj index 8bc59c35a19..0714857b32e 100644 --- a/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj +++ b/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj @@ -89,6 +89,7 @@ + diff --git a/vsintegration/src/FSharp.Editor/FSharp.Editor.resx b/vsintegration/src/FSharp.Editor/FSharp.Editor.resx index 8440136cf33..552bd947c93 100644 --- a/vsintegration/src/FSharp.Editor/FSharp.Editor.resx +++ b/vsintegration/src/FSharp.Editor/FSharp.Editor.resx @@ -219,4 +219,10 @@ F# Dispostable Values (top-level) + + Use 'upcast' + + + Use ':>' operator + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf index acac2c13bf4..21f2694fce6 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf @@ -172,6 +172,16 @@ Formátování + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf index 639a34858b9..20493f4247f 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf @@ -172,6 +172,16 @@ Formatierung + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf index 5f1297eab36..0d739c5a848 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf @@ -172,6 +172,16 @@ Formato + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf index 3ae9d25817a..46cda34549b 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf @@ -172,6 +172,16 @@ Mise en forme + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf index 8457ca172b1..049dcb38317 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf @@ -172,6 +172,16 @@ Formattazione + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf index 614fdea4a51..310e6fbcda7 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf @@ -172,6 +172,16 @@ 書式設定 + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf index 832aaa80b5c..7d6aec732ca 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf @@ -172,6 +172,16 @@ 서식 + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf index 785230a56bb..29f96792964 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf @@ -172,6 +172,16 @@ Formatowanie + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf index 98636e84fd4..6b8a21f743a 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf @@ -172,6 +172,16 @@ Formatação + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf index 04891b9acea..753f76e37fb 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf @@ -172,6 +172,16 @@ Форматирование + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf index 0cbe170c6d7..fb96585818d 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf @@ -172,6 +172,16 @@ Biçimlendirme + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf index dd827dbfd65..507fc592337 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf @@ -172,6 +172,16 @@ 正在格式化 + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf index a59aa79970f..b6ef8eeba89 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf @@ -172,6 +172,16 @@ 格式化 + + Use 'upcast' + Use 'upcast' + + + + Use ':>' operator + Use ':>' operator + + \ No newline at end of file