From aaeaa776b0f9124ebbc6d8daa6aee3e8cfd44e6c Mon Sep 17 00:00:00 2001 From: cartermp Date: Sun, 15 Nov 2020 19:24:18 -0800 Subject: [PATCH 1/2] Add AddMissingEqualsToTypeDefinition code fixer --- .../AddMissingEqualsToTypeDefinition.fs | 62 +++++++++++++++++++ .../src/FSharp.Editor/FSharp.Editor.fsproj | 1 + .../src/FSharp.Editor/FSharp.Editor.resx | 3 + .../FSharp.Editor/xlf/FSharp.Editor.cs.xlf | 5 ++ .../FSharp.Editor/xlf/FSharp.Editor.de.xlf | 5 ++ .../FSharp.Editor/xlf/FSharp.Editor.es.xlf | 5 ++ .../FSharp.Editor/xlf/FSharp.Editor.fr.xlf | 5 ++ .../FSharp.Editor/xlf/FSharp.Editor.it.xlf | 5 ++ .../FSharp.Editor/xlf/FSharp.Editor.ja.xlf | 5 ++ .../FSharp.Editor/xlf/FSharp.Editor.ko.xlf | 5 ++ .../FSharp.Editor/xlf/FSharp.Editor.pl.xlf | 5 ++ .../FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf | 5 ++ .../FSharp.Editor/xlf/FSharp.Editor.ru.xlf | 5 ++ .../FSharp.Editor/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 vsintegration/src/FSharp.Editor/CodeFix/AddMissingEqualsToTypeDefinition.fs diff --git a/vsintegration/src/FSharp.Editor/CodeFix/AddMissingEqualsToTypeDefinition.fs b/vsintegration/src/FSharp.Editor/CodeFix/AddMissingEqualsToTypeDefinition.fs new file mode 100644 index 00000000000..df40168e392 --- /dev/null +++ b/vsintegration/src/FSharp.Editor/CodeFix/AddMissingEqualsToTypeDefinition.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 +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 ["FS0010"; "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 + + for diagnostic in diagnostics do + let message = diagnostic.GetMessage() + // Ensure it is the "missing '=' in type definition" error. + // This is a funky heuristic but it's probably correct. + do! Option.guard (message.Contains("'='")) + + 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/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj b/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj index 8bc59c35a19..d7eaf3cb50a 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..76e8660b8eb 100644 --- a/vsintegration/src/FSharp.Editor/FSharp.Editor.resx +++ b/vsintegration/src/FSharp.Editor/FSharp.Editor.resx @@ -219,4 +219,7 @@ F# Dispostable Values (top-level) + + Add missing '=' to type definition + \ 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..db419c11cba 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf index 639a34858b9..6d640790800 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf index 5f1297eab36..6e36884b78d 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf index 3ae9d25817a..15811ca0e0b 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf index 8457ca172b1..33efa5fca4b 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf index 614fdea4a51..becf5c1d33a 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf index 832aaa80b5c..a929e2565fd 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf index 785230a56bb..5c3aed1ee1a 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf index 98636e84fd4..c7e51f75e3d 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf index 04891b9acea..806a1bac0dd 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf index 0cbe170c6d7..1fef6904534 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf index dd827dbfd65..b0968314d23 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf +++ b/vsintegration/src/FSharp.Editor/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/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf index a59aa79970f..1c123d17b3c 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf @@ -2,6 +2,11 @@ + + Add missing '=' to type definition + Add missing '=' to type definition + + Add 'new' keyword 新增 'new' 關鍵字 From d5784b7f2039013a63528d9f3f1022b14b5c5e68 Mon Sep 17 00:00:00 2001 From: cartermp Date: Tue, 17 Nov 2020 17:20:49 -0800 Subject: [PATCH 2/2] Restrict to FS3360 --- .../CodeFix/AddMissingEqualsToTypeDefinition.fs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/vsintegration/src/FSharp.Editor/CodeFix/AddMissingEqualsToTypeDefinition.fs b/vsintegration/src/FSharp.Editor/CodeFix/AddMissingEqualsToTypeDefinition.fs index df40168e392..22877845814 100644 --- a/vsintegration/src/FSharp.Editor/CodeFix/AddMissingEqualsToTypeDefinition.fs +++ b/vsintegration/src/FSharp.Editor/CodeFix/AddMissingEqualsToTypeDefinition.fs @@ -15,7 +15,7 @@ open Microsoft.CodeAnalysis.CodeFixes type internal FSharpAddMissingEqualsToTypeDefinitionCodeFixProvider() = inherit CodeFixProvider() - let fixableDiagnosticIds = set ["FS0010"; "FS3360"] + let fixableDiagnosticIds = set ["FS3360"] override _.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds @@ -26,12 +26,6 @@ type internal FSharpAddMissingEqualsToTypeDefinitionCodeFixProvider() = |> Seq.filter (fun x -> fixableDiagnosticIds |> Set.contains x.Id) |> Seq.toImmutableArray - for diagnostic in diagnostics do - let message = diagnostic.GetMessage() - // Ensure it is the "missing '=' in type definition" error. - // This is a funky heuristic but it's probably correct. - do! Option.guard (message.Contains("'='")) - let! sourceText = context.Document.GetTextAsync(context.CancellationToken) let mutable pos = context.Span.Start - 1