Skip to content

Commit

Permalink
Add AddMissingEqualsToTypeDefinition code fixer (dotnet#10470)
Browse files Browse the repository at this point in the history
* Add AddMissingEqualsToTypeDefinition code fixer

* Restrict to FS3360
  • Loading branch information
cartermp authored and nosami committed Feb 22, 2021
1 parent 30592af commit 1392e16
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 0 deletions.
@@ -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

[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "AddMissingEqualsToTypeDefinition"); Shared>]
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)
1 change: 1 addition & 0 deletions vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj
Expand Up @@ -184,6 +184,7 @@
<Compile Include="Commands\FsiCommandService.fs" />
<Compile Include="Commands\XmlDocCommandService.fs" />
<Compile Include="CodeFix\CodeFixHelpers.fs" />
<Compile Include="CodeFix\AddMissingEqualsToTypeDefinition.fs" />
<Compile Include="CodeFix\ConvertToSingleEqualsEqualityExpression.fs" />
<Compile Include="CodeFix\ChangeRefCellDerefToNotExpression.fs" />
<Compile Include="CodeFix\WrapExpressionInParentheses.fs" />
Expand Down
3 changes: 3 additions & 0 deletions vsintegration/src/FSharp.Editor/FSharp.Editor.resx
Expand Up @@ -219,6 +219,9 @@
<data name="FSharpDisposableTopLevelValuesClassificationType" xml:space="preserve">
<value>F# Dispostable Values (top-level)</value>
</data>
<data name="AddMissingEqualsToTypeDefinition" xml:space="preserve">
<value>Add missing '=' to type definition</value>
</data>
<data name="ConvertToSingleEqualsEqualityExpression" xml:space="preserve">
<value>Use '=' for equality check</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="cs" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">Přidejte klíčové slovo new.</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="de" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">Schlüsselwort "new" hinzufügen</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="es" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">Agregar "nueva" palabra clave</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="fr" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">Ajouter le mot clé 'new'</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="it" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">Aggiungi la parola chiave 'new'</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ja" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">'new' キーワードを追加する</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ko" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">'new' 키워드 추가</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pl" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">Dodaj słowo kluczowe „new”</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="pt-BR" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">Adicionar a palavra-chave 'new'</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="ru" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">Добавить ключевое слово "new"</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="tr" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">'new' anahtar sözcüğünü ekleme</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="zh-Hans" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">添加“新”关键字</target>
Expand Down
5 changes: 5 additions & 0 deletions vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf
Expand Up @@ -2,6 +2,11 @@
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="zh-Hant" original="../FSharp.Editor.resx">
<body>
<trans-unit id="AddMissingEqualsToTypeDefinition">
<source>Add missing '=' to type definition</source>
<target state="new">Add missing '=' to type definition</target>
<note />
</trans-unit>
<trans-unit id="AddNewKeyword">
<source>Add 'new' keyword</source>
<target state="translated">新增 'new' 關鍵字</target>
Expand Down

0 comments on commit 1392e16

Please sign in to comment.