Skip to content

Commit

Permalink
Add WrapExpressionInParentheses code fix (dotnet#10460)
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermp committed Nov 17, 2020
1 parent bacf7b8 commit 063eb98
Show file tree
Hide file tree
Showing 16 changed files with 108 additions and 0 deletions.
39 changes: 39 additions & 0 deletions CodeFix/WrapExpressionInParentheses.fs
@@ -0,0 +1,39 @@
// 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 = "AddParentheses"); Shared>]
type internal FSharpWrapExpressionInParenthesesFixProvider() =
inherit CodeFixProvider()

let fixableDiagnosticIds = set ["FS0597"]

override __.FixableDiagnosticIds = Seq.toImmutableArray fixableDiagnosticIds

override this.RegisterCodeFixesAsync context : Task =
async {
let title = SR.WrapExpressionInParentheses()

let getChangedText (sourceText: SourceText) =
sourceText.WithChanges(TextChange(TextSpan(context.Span.Start, 0), "("))
.WithChanges(TextChange(TextSpan(context.Span.End, 0), ")"))

context.RegisterCodeFix(
CodeAction.Create(
title,
(fun (cancellationToken: CancellationToken) ->
async {
let! cancellationToken = Async.CancellationToken
let! sourceText = context.Document.GetTextAsync(cancellationToken) |> Async.AwaitTask
return context.Document.WithText(getChangedText sourceText)
} |> RoslynHelpers.StartAsyncAsTask(cancellationToken)),
title), context.Diagnostics |> Seq.filter (fun x -> this.FixableDiagnosticIds.Contains x.Id) |> Seq.toImmutableArray)
} |> RoslynHelpers.StartAsyncUnitAsTask(context.CancellationToken)
1 change: 1 addition & 0 deletions FSharp.Editor.fsproj
Expand Up @@ -89,6 +89,7 @@
<Compile Include="Commands\FsiCommandService.fs" />
<Compile Include="Commands\XmlDocCommandService.fs" />
<Compile Include="CodeFix\CodeFixHelpers.fs" />
<Compile Include="CodeFix\WrapExpressionInParentheses.fs" />
<Compile Include="CodeFix\ChangePrefixNegationToInfixSubtraction.fs" />
<Compile Include="CodeFix\AddNewKeywordToDisposableConstructorInvocation.fs" />
<Compile Include="CodeFix\AddOpenCodeFixProvider.fs" />
Expand Down
3 changes: 3 additions & 0 deletions 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="WrapExpressionInParentheses" xml:space="preserve">
<value>Wrap expression in parentheses</value>
</data>
<data name="ChangePrefixNegationToInfixSubtraction" xml:space="preserve">
<value>Use subtraction instead of negation</value>
</data>
Expand Down
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.cs.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formátování</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.de.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formatierung</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.es.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formato</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.fr.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Mise en forme</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.it.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formattazione</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ja.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">書式設定</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ko.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">서식</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.pl.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formatowanie</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.pt-BR.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Formatação</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.ru.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Форматирование</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.tr.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">Biçimlendirme</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.zh-Hans.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">正在格式化</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>
5 changes: 5 additions & 0 deletions xlf/FSharp.Editor.zh-Hant.xlf
Expand Up @@ -177,6 +177,11 @@
<target state="translated">格式化</target>
<note />
</trans-unit>
<trans-unit id="WrapExpressionInParentheses">
<source>Wrap expression in parentheses</source>
<target state="new">Wrap expression in parentheses</target>
<note />
</trans-unit>
</body>
</file>
</xliff>

0 comments on commit 063eb98

Please sign in to comment.