From 834912be4a87e3c176015b2cdf5096722f234701 Mon Sep 17 00:00:00 2001 From: Phillip Carter Date: Tue, 17 Nov 2020 18:42:52 -0800 Subject: [PATCH] Add AddMissingEqualsToTypeDefinition code fixer (#10470) * Add AddMissingEqualsToTypeDefinition code fixer * Restrict to FS3360 --- CodeFix/AddMissingEqualsToTypeDefinition.fs | 56 +++++++++++++++++++++ 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, 125 insertions(+) create mode 100644 CodeFix/AddMissingEqualsToTypeDefinition.fs diff --git a/CodeFix/AddMissingEqualsToTypeDefinition.fs b/CodeFix/AddMissingEqualsToTypeDefinition.fs new file mode 100644 index 00000000000..22877845814 --- /dev/null +++ b/CodeFix/AddMissingEqualsToTypeDefinition.fs @@ -0,0 +1,56 @@ +// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. + +namespace Microsoft.VisualStudio.FSharp.Editor + +open System +open System.Composition +open System.Threading.Tasks + +open Microsoft.VisualStudio.FSharp.Editor.Logging + +open Microsoft.CodeAnalysis.Text +open Microsoft.CodeAnalysis.CodeFixes + +[] +type internal FSharpAddMissingEqualsToTypeDefinitionCodeFixProvider() = + inherit CodeFixProvider() + + let fixableDiagnosticIds = set ["FS3360"] + + override _.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds + + override _.RegisterCodeFixesAsync context : Task = + asyncMaybe { + let diagnostics = + context.Diagnostics + |> Seq.filter (fun x -> fixableDiagnosticIds |> Set.contains x.Id) + |> Seq.toImmutableArray + + let! sourceText = context.Document.GetTextAsync(context.CancellationToken) + + let mutable pos = context.Span.Start - 1 + + // This won't ever actually happen, but eh why not + do! Option.guard (pos > 0) + + let mutable ch = sourceText.GetSubText(TextSpan(pos, 1)).ToString().[0] + + while pos > 0 && Char.IsWhiteSpace(ch) do + pos <- pos - 1 + let text = sourceText.GetSubText(TextSpan(pos, 1)) + ch <- text.ToString().[0] + + let title = SR.AddMissingEqualsToTypeDefinition() + + let codeFix = + CodeFixHelpers.createTextChangeCodeFix( + title, + context, + // 'pos + 1' is here because 'pos' is now the position of the first non-whitespace character. + // Using just 'pos' will creat uncompilable code. + (fun () -> asyncMaybe.Return [| TextChange(TextSpan(pos + 1, 0), " =") |])) + + context.RegisterCodeFix(codeFix, diagnostics) + } + |> Async.Ignore + |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken) \ No newline at end of file diff --git a/FSharp.Editor.fsproj b/FSharp.Editor.fsproj index c14493dc392..761e97ccd5c 100644 --- a/FSharp.Editor.fsproj +++ b/FSharp.Editor.fsproj @@ -89,6 +89,7 @@ + diff --git a/FSharp.Editor.resx b/FSharp.Editor.resx index 720e90003b3..62497078788 100644 --- a/FSharp.Editor.resx +++ b/FSharp.Editor.resx @@ -219,6 +219,9 @@ F# Dispostable Values (top-level) + + Add missing '=' to type definition + Use '=' for equality check diff --git a/xlf/FSharp.Editor.cs.xlf b/xlf/FSharp.Editor.cs.xlf index 39301e1124a..14b804cc247 100644 --- a/xlf/FSharp.Editor.cs.xlf +++ b/xlf/FSharp.Editor.cs.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword Přidejte klíčové slovo new. diff --git a/xlf/FSharp.Editor.de.xlf b/xlf/FSharp.Editor.de.xlf index c6b772545b4..4aa11aa5540 100644 --- a/xlf/FSharp.Editor.de.xlf +++ b/xlf/FSharp.Editor.de.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword Schlüsselwort "new" hinzufügen diff --git a/xlf/FSharp.Editor.es.xlf b/xlf/FSharp.Editor.es.xlf index 156379e248a..d2adca7d01a 100644 --- a/xlf/FSharp.Editor.es.xlf +++ b/xlf/FSharp.Editor.es.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword Agregar "nueva" palabra clave diff --git a/xlf/FSharp.Editor.fr.xlf b/xlf/FSharp.Editor.fr.xlf index 0d06914bd12..ae54a946ed6 100644 --- a/xlf/FSharp.Editor.fr.xlf +++ b/xlf/FSharp.Editor.fr.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword Ajouter le mot clé 'new' diff --git a/xlf/FSharp.Editor.it.xlf b/xlf/FSharp.Editor.it.xlf index d9b4ae18f3b..9df3852f0cd 100644 --- a/xlf/FSharp.Editor.it.xlf +++ b/xlf/FSharp.Editor.it.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword Aggiungi la parola chiave 'new' diff --git a/xlf/FSharp.Editor.ja.xlf b/xlf/FSharp.Editor.ja.xlf index 98b6321c3e7..d141030be81 100644 --- a/xlf/FSharp.Editor.ja.xlf +++ b/xlf/FSharp.Editor.ja.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword 'new' キーワードを追加する diff --git a/xlf/FSharp.Editor.ko.xlf b/xlf/FSharp.Editor.ko.xlf index 846f4f1f59f..b2e8082b7fc 100644 --- a/xlf/FSharp.Editor.ko.xlf +++ b/xlf/FSharp.Editor.ko.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword 'new' 키워드 추가 diff --git a/xlf/FSharp.Editor.pl.xlf b/xlf/FSharp.Editor.pl.xlf index 04349ea8fee..4acc383f4bb 100644 --- a/xlf/FSharp.Editor.pl.xlf +++ b/xlf/FSharp.Editor.pl.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword Dodaj słowo kluczowe „new” diff --git a/xlf/FSharp.Editor.pt-BR.xlf b/xlf/FSharp.Editor.pt-BR.xlf index cc9abde1f4c..8256c04823f 100644 --- a/xlf/FSharp.Editor.pt-BR.xlf +++ b/xlf/FSharp.Editor.pt-BR.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword Adicionar a palavra-chave 'new' diff --git a/xlf/FSharp.Editor.ru.xlf b/xlf/FSharp.Editor.ru.xlf index 4aea41af1e0..4df93cd671a 100644 --- a/xlf/FSharp.Editor.ru.xlf +++ b/xlf/FSharp.Editor.ru.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword Добавить ключевое слово "new" diff --git a/xlf/FSharp.Editor.tr.xlf b/xlf/FSharp.Editor.tr.xlf index a34f26a5e29..c42645b2e76 100644 --- a/xlf/FSharp.Editor.tr.xlf +++ b/xlf/FSharp.Editor.tr.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword 'new' anahtar sözcüğünü ekleme diff --git a/xlf/FSharp.Editor.zh-Hans.xlf b/xlf/FSharp.Editor.zh-Hans.xlf index a9259adff01..ec5e69bca10 100644 --- a/xlf/FSharp.Editor.zh-Hans.xlf +++ b/xlf/FSharp.Editor.zh-Hans.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword 添加“新”关键字 diff --git a/xlf/FSharp.Editor.zh-Hant.xlf b/xlf/FSharp.Editor.zh-Hant.xlf index 4d66d55af16..acccd1d14d4 100644 --- a/xlf/FSharp.Editor.zh-Hant.xlf +++ b/xlf/FSharp.Editor.zh-Hant.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword 新增 'new' 關鍵字