Skip to content
This repository has been archived by the owner on Dec 23, 2024. It is now read-only.

Commit

Permalink
Add MakeOuterBindingRecursive code fix (dotnet#10666)
Browse files Browse the repository at this point in the history
* Add MakeOuterBindingRecursive code fix

* Allow other bindings to come before a nested should-be-recursive binding

* more fixy

* formatting and comment

* Add to service layer

* Area and add name to message
  • Loading branch information
cartermp authored Dec 16, 2020
1 parent fa3ff62 commit 8c095a1
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 0 deletions.
56 changes: 56 additions & 0 deletions CodeFix/MakeOuterBindingRecursive.fs
Original file line number Diff line number Diff line change
@@ -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 Microsoft.CodeAnalysis.Text
open Microsoft.CodeAnalysis.CodeFixes

[<ExportCodeFixProvider(FSharpConstants.FSharpLanguageName, Name = "MakeOuterBindingRecursive"); Shared>]
type internal FSharpMakeOuterBindingRecursiveCodeFixProvider
[<ImportingConstructor>]
(
checkerProvider: FSharpCheckerProvider,
projectInfoManager: FSharpProjectOptionsManager
) =
inherit CodeFixProvider()

static let userOpName = "MakeOuterBindingRecursive"
let fixableDiagnosticIds = set ["FS0039"]

override _.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds

override _.RegisterCodeFixesAsync context =
asyncMaybe {
let! sourceText = context.Document.GetTextAsync(context.CancellationToken)
let! parsingOptions, _ = projectInfoManager.TryGetOptionsForEditingDocumentOrProject(context.Document, context.CancellationToken, userOpName)
let! parseResults = checkerProvider.Checker.ParseFile(context.Document.FilePath, sourceText.ToFSharpSourceText(), parsingOptions, userOpName) |> liftAsync

let diagnosticRange = RoslynHelpers.TextSpanToFSharpRange(context.Document.FilePath, context.Span, sourceText)
do! Option.guard (parseResults.IsPosContainedInApplication diagnosticRange.Start)

let! outerBindingRange = parseResults.TryRangeOfNameOfNearestOuterBindingContainingPos diagnosticRange.Start
let! outerBindingNameSpan = RoslynHelpers.TryFSharpRangeToTextSpan(sourceText, outerBindingRange)

// One last check to verify the names are the same
do! Option.guard (sourceText.GetSubText(outerBindingNameSpan).ContentEquals(sourceText.GetSubText(context.Span)))

let diagnostics =
context.Diagnostics
|> Seq.filter (fun x -> fixableDiagnosticIds |> Set.contains x.Id)
|> Seq.toImmutableArray

let title = String.Format(SR.MakeOuterBindingRecursive(), sourceText.GetSubText(outerBindingNameSpan).ToString())

let codeFix =
CodeFixHelpers.createTextChangeCodeFix(
title,
context,
(fun () -> asyncMaybe.Return [| TextChange(TextSpan(outerBindingNameSpan.Start, 0), "rec ") |]))

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 @@ -90,6 +90,7 @@
<Compile Include="Commands\XmlDocCommandService.fs" />
<Compile Include="CodeFix\CodeFixHelpers.fs" />
<Compile Include="CodeFix\ConvertCSharpLambdaToFSharpLambda.fs" />
<Compile Include="CodeFix\MakeOuterBindingRecursive.fs" />
<Compile Include="CodeFix\RemoveReturnOrYield.fs" />
<Compile Include="CodeFix\ConvertToAnonymousRecord.fs" />
<Compile Include="CodeFix\UseMutationWhenValueIsMutable.fs" />
Expand Down
3 changes: 3 additions & 0 deletions FSharp.Editor.resx
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,7 @@
<data name="UseFSharpLambda" xml:space="preserve">
<value>Use F# lambda syntax</value>
</data>
<data name="MakeOuterBindingRecursive" xml:space="preserve">
<value>Make '{0}' recursive</value>
</data>
</root>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.cs.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
<target state="translated">Nastavte deklaraci jako mutable.</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">Před {0} vložte podtržítko.</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 @@ -62,6 +62,11 @@
<target state="translated">Deklaration "änderbar" machen</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">"{0}" einen Unterstrich voranstellen</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 @@ -62,6 +62,11 @@
<target state="translated">Convertir la declaración en "mutable"</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">Colocar un carácter de subrayado delante de "{0}"</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 @@ -62,6 +62,11 @@
<target state="translated">Rendre la déclaration 'mutable'</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">Faire précéder '{0}' d'un trait de soulignement</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 @@ -62,6 +62,11 @@
<target state="translated">Impostare la dichiarazione come 'mutable'</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">Anteponi a '{0}' un carattere di sottolineatura</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 @@ -62,6 +62,11 @@
<target state="translated">'mutable' を宣言する</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">アンダースコアが含まれているプレフィックス '{0}'</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 @@ -62,6 +62,11 @@
<target state="translated">선언을 '변경 가능'으로 지정</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">밑줄이 있는 '{0}' 접두사</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 @@ -62,6 +62,11 @@
<target state="translated">Nadaj deklaracji właściwość „mutable”</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">Prefiks „{0}” ze znakiem podkreślenia</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 @@ -62,6 +62,11 @@
<target state="translated">Fazer com que a declaração seja 'mutable'</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">Prefixo '{0}' sem sublinhado</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 @@ -62,6 +62,11 @@
<target state="translated">Сделайте объявление "изменяемым"</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">Добавить символ подчеркивания как префикс "{0}"</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 @@ -62,6 +62,11 @@
<target state="translated">Bildirimi 'mutable' yapın</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">'{0}' öğesinin önüne alt çizgi ekleme</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 @@ -62,6 +62,11 @@
<target state="translated">将声明设为“可变”</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">带下划线的前缀“{0}”</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 @@ -62,6 +62,11 @@
<target state="translated">將宣告設定為「可變動」</target>
<note />
</trans-unit>
<trans-unit id="MakeOuterBindingRecursive">
<source>Make '{0}' recursive</source>
<target state="new">Make '{0}' recursive</target>
<note />
</trans-unit>
<trans-unit id="PrefixValueNameWithUnderscore">
<source>Prefix '{0}' with underscore</source>
<target state="translated">有底線的前置詞 '{0}'</target>
Expand Down

0 comments on commit 8c095a1

Please sign in to comment.