Skip to content

Commit

Permalink
Add ConvertToAnonymousRecord quick fixeroony (dotnet#10493)
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermp authored and nosami committed Feb 22, 2021
1 parent 94c0d54 commit 844d50e
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 0 deletions.
62 changes: 62 additions & 0 deletions CodeFix/ConvertToAnonymousRecord.fs
Original file line number Diff line number Diff line change
@@ -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

[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "ConvertToAnonymousRecord"); Shared>]
type internal FSharpConvertToAnonymousRecordCodeFixProvider
[<ImportingConstructor>]
(
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)
1 change: 1 addition & 0 deletions FSharp.Editor.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
<Compile Include="Commands\FsiCommandService.fs" />
<Compile Include="Commands\XmlDocCommandService.fs" />
<Compile Include="CodeFix\CodeFixHelpers.fs" />
<Compile Include="CodeFix\ConvertToAnonymousRecord.fs" />
<Compile Include="CodeFix\UseMutationWhenValueIsMutable.fs" />
<Compile Include="CodeFix\MakeDeclarationMutable.fs" />
<Compile Include="CodeFix\ChangeToUpcast.fs" />
Expand Down
3 changes: 3 additions & 0 deletions FSharp.Editor.resx
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@
<data name="ChangePrefixNegationToInfixSubtraction" xml:space="preserve">
<value>Use subtraction instead of negation</value>
</data>
<data name="ConvertToAnonymousRecord" xml:space="preserve">
<value>Convert to Anonymous Record</value>
</data>
<data name="UseMutationWhenValueIsMutable" xml:space="preserve">
<value>Use '&lt;-' to mutate value</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Přidejte klíčové slovo new.</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.de.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Schlüsselwort "new" hinzufügen</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.es.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Agregar "nueva" palabra clave</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.fr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Ajouter le mot clé 'new'</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.it.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Aggiungi la parola chiave 'new'</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ja.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">'new' キーワードを追加する</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ko.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">'new' 키워드 추가</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.pl.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Dodaj słowo kluczowe „new”</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.pt-BR.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Adicionar a palavra-chave 'new'</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ru.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">Добавить ключевое слово "new"</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.tr.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">'new' anahtar sözcüğünü ekleme</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.zh-Hans.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">添加“新”关键字</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.zh-Hant.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<target state="translated">新增 'new' 關鍵字</target>
<note />
</trans-unit>
<trans-unit id="ConvertToAnonymousRecord">
<source>Convert to Anonymous Record</source>
<target state="new">Convert to Anonymous Record</target>
<note />
</trans-unit>
<trans-unit id="ConvertToSingleEqualsEqualityExpression">
<source>Use '=' for equality check</source>
<target state="new">Use '=' for equality check</target>
Expand Down

0 comments on commit 844d50e

Please sign in to comment.