From 5428d13f62baf4969e801bc32717369272971188 Mon Sep 17 00:00:00 2001 From: Phillip Carter Date: Wed, 18 Nov 2020 17:59:41 -0800 Subject: [PATCH] Add ConvertToAnonymousRecord quick fixeroony (#10493) --- CodeFix/ConvertToAnonymousRecord.fs | 62 +++++++++++++++++++++++++++++ FSharp.Editor.fsproj | 1 + FSharp.Editor.resx | 3 ++ xlf/FSharp.Editor.cs.xlf | 5 +++ xlf/FSharp.Editor.de.xlf | 5 +++ xlf/FSharp.Editor.es.xlf | 5 +++ xlf/FSharp.Editor.fr.xlf | 5 +++ xlf/FSharp.Editor.it.xlf | 5 +++ xlf/FSharp.Editor.ja.xlf | 5 +++ xlf/FSharp.Editor.ko.xlf | 5 +++ xlf/FSharp.Editor.pl.xlf | 5 +++ xlf/FSharp.Editor.pt-BR.xlf | 5 +++ xlf/FSharp.Editor.ru.xlf | 5 +++ xlf/FSharp.Editor.tr.xlf | 5 +++ xlf/FSharp.Editor.zh-Hans.xlf | 5 +++ xlf/FSharp.Editor.zh-Hant.xlf | 5 +++ 16 files changed, 131 insertions(+) create mode 100644 CodeFix/ConvertToAnonymousRecord.fs diff --git a/CodeFix/ConvertToAnonymousRecord.fs b/CodeFix/ConvertToAnonymousRecord.fs new file mode 100644 index 00000000000..645ffee5ab8 --- /dev/null +++ b/CodeFix/ConvertToAnonymousRecord.fs @@ -0,0 +1,62 @@ +// 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 +open System.Threading.Tasks + +open Microsoft.CodeAnalysis.Text +open Microsoft.CodeAnalysis.CodeFixes +open Microsoft.CodeAnalysis.CodeActions + +[] +type internal FSharpConvertToAnonymousRecordCodeFixProvider + [] + ( + checkerProvider: FSharpCheckerProvider, + projectInfoManager: FSharpProjectOptionsManager + ) = + inherit CodeFixProvider() + + static let userOpName = "ConvertToAnonymousRecord" + + let fixableDiagnosticIds = set ["FS0039"] + + override _.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds + + override _.RegisterCodeFixesAsync context : Task = + asyncMaybe { + let document = context.Document + let! parsingOptions, _ = projectInfoManager.TryGetOptionsForEditingDocumentOrProject(document, context.CancellationToken, userOpName) + let! sourceText = context.Document.GetTextAsync(context.CancellationToken) + let! parseResults = checkerProvider.Checker.ParseFile(document.FilePath, sourceText.ToFSharpSourceText(), parsingOptions, userOpName) |> liftAsync + + let errorRange = RoslynHelpers.TextSpanToFSharpRange(document.FilePath, context.Span, sourceText) + let! recordRange = parseResults.TryRangeOfRecordExpressionContainingPos errorRange.Start + let! recordSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, recordRange) + + let getChangedText () = + sourceText.WithChanges(TextChange(TextSpan(recordSpan.Start + 1, 0), "|")) + .WithChanges(TextChange(TextSpan(recordSpan.End, 0), "|")) + + let diagnostics = + context.Diagnostics + |> Seq.filter (fun x -> fixableDiagnosticIds |> Set.contains x.Id) + |> Seq.toImmutableArray + + let title = SR.ConvertToAnonymousRecord() + + let codeFix = + CodeAction.Create( + title, + (fun (cancellationToken: CancellationToken) -> + async { + return context.Document.WithText(getChangedText()) + } |> RoslynHelpers.StartAsyncAsTask(cancellationToken)), + title) + + context.RegisterCodeFix(codeFix, diagnostics) + } + |> Async.Ignore + |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken) diff --git a/FSharp.Editor.fsproj b/FSharp.Editor.fsproj index d1ad750f1f8..bb94fee1f86 100644 --- a/FSharp.Editor.fsproj +++ b/FSharp.Editor.fsproj @@ -89,6 +89,7 @@ + diff --git a/FSharp.Editor.resx b/FSharp.Editor.resx index cc79f2dd7f9..1b048c017fd 100644 --- a/FSharp.Editor.resx +++ b/FSharp.Editor.resx @@ -243,6 +243,9 @@ Use subtraction instead of negation + + Convert to Anonymous Record + Use '<-' to mutate value diff --git a/xlf/FSharp.Editor.cs.xlf b/xlf/FSharp.Editor.cs.xlf index 8f4b72116da..696632d8431 100644 --- a/xlf/FSharp.Editor.cs.xlf +++ b/xlf/FSharp.Editor.cs.xlf @@ -12,6 +12,11 @@ Přidejte klíčové slovo new. + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.de.xlf b/xlf/FSharp.Editor.de.xlf index 398d1dc6222..6211ac66433 100644 --- a/xlf/FSharp.Editor.de.xlf +++ b/xlf/FSharp.Editor.de.xlf @@ -12,6 +12,11 @@ Schlüsselwort "new" hinzufügen + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.es.xlf b/xlf/FSharp.Editor.es.xlf index 20d5f653715..3b52e44f6ef 100644 --- a/xlf/FSharp.Editor.es.xlf +++ b/xlf/FSharp.Editor.es.xlf @@ -12,6 +12,11 @@ Agregar "nueva" palabra clave + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.fr.xlf b/xlf/FSharp.Editor.fr.xlf index 2c95c0d1292..f2a49f771d1 100644 --- a/xlf/FSharp.Editor.fr.xlf +++ b/xlf/FSharp.Editor.fr.xlf @@ -12,6 +12,11 @@ Ajouter le mot clé 'new' + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.it.xlf b/xlf/FSharp.Editor.it.xlf index 1bb527a3361..5716858b0d0 100644 --- a/xlf/FSharp.Editor.it.xlf +++ b/xlf/FSharp.Editor.it.xlf @@ -12,6 +12,11 @@ Aggiungi la parola chiave 'new' + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.ja.xlf b/xlf/FSharp.Editor.ja.xlf index 0a18d050f7f..d9d1f37f2e0 100644 --- a/xlf/FSharp.Editor.ja.xlf +++ b/xlf/FSharp.Editor.ja.xlf @@ -12,6 +12,11 @@ 'new' キーワードを追加する + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.ko.xlf b/xlf/FSharp.Editor.ko.xlf index d95bde0d93e..3ff829d0c18 100644 --- a/xlf/FSharp.Editor.ko.xlf +++ b/xlf/FSharp.Editor.ko.xlf @@ -12,6 +12,11 @@ 'new' 키워드 추가 + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.pl.xlf b/xlf/FSharp.Editor.pl.xlf index 579d328911a..88891e9f75d 100644 --- a/xlf/FSharp.Editor.pl.xlf +++ b/xlf/FSharp.Editor.pl.xlf @@ -12,6 +12,11 @@ Dodaj słowo kluczowe „new” + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.pt-BR.xlf b/xlf/FSharp.Editor.pt-BR.xlf index ce039317f06..989e14866ae 100644 --- a/xlf/FSharp.Editor.pt-BR.xlf +++ b/xlf/FSharp.Editor.pt-BR.xlf @@ -12,6 +12,11 @@ Adicionar a palavra-chave 'new' + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.ru.xlf b/xlf/FSharp.Editor.ru.xlf index 72b8228c262..a5bab017c76 100644 --- a/xlf/FSharp.Editor.ru.xlf +++ b/xlf/FSharp.Editor.ru.xlf @@ -12,6 +12,11 @@ Добавить ключевое слово "new" + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.tr.xlf b/xlf/FSharp.Editor.tr.xlf index a8bd45e7577..04be92de984 100644 --- a/xlf/FSharp.Editor.tr.xlf +++ b/xlf/FSharp.Editor.tr.xlf @@ -12,6 +12,11 @@ 'new' anahtar sözcüğünü ekleme + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.zh-Hans.xlf b/xlf/FSharp.Editor.zh-Hans.xlf index 0ebd31d4d09..c094963ad32 100644 --- a/xlf/FSharp.Editor.zh-Hans.xlf +++ b/xlf/FSharp.Editor.zh-Hans.xlf @@ -12,6 +12,11 @@ 添加“新”关键字 + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check diff --git a/xlf/FSharp.Editor.zh-Hant.xlf b/xlf/FSharp.Editor.zh-Hant.xlf index da6ba8d19c3..1f1b7c1f65a 100644 --- a/xlf/FSharp.Editor.zh-Hant.xlf +++ b/xlf/FSharp.Editor.zh-Hant.xlf @@ -12,6 +12,11 @@ 新增 'new' 關鍵字 + + Convert to Anonymous Record + Convert to Anonymous Record + + Use '=' for equality check Use '=' for equality check