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

Commit

Permalink
Add WrapExpressionInParentheses code fix (dotnet#10460)
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermp authored 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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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.