From dd502314c95826c9bd83f986b60eea27eb0748a8 Mon Sep 17 00:00:00 2001 From: Phillip Carter Date: Mon, 16 Nov 2020 20:00:54 -0800 Subject: [PATCH] Add WrapExpressionInParentheses code fix (#10460) --- .../CodeFix/WrapExpressionInParentheses.fs | 39 +++++++++++++++++++ .../src/FSharp.Editor/FSharp.Editor.fsproj | 1 + .../src/FSharp.Editor/FSharp.Editor.resx | 3 ++ .../FSharp.Editor/xlf/FSharp.Editor.cs.xlf | 5 +++ .../FSharp.Editor/xlf/FSharp.Editor.de.xlf | 5 +++ .../FSharp.Editor/xlf/FSharp.Editor.es.xlf | 5 +++ .../FSharp.Editor/xlf/FSharp.Editor.fr.xlf | 5 +++ .../FSharp.Editor/xlf/FSharp.Editor.it.xlf | 5 +++ .../FSharp.Editor/xlf/FSharp.Editor.ja.xlf | 5 +++ .../FSharp.Editor/xlf/FSharp.Editor.ko.xlf | 5 +++ .../FSharp.Editor/xlf/FSharp.Editor.pl.xlf | 5 +++ .../FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf | 5 +++ .../FSharp.Editor/xlf/FSharp.Editor.ru.xlf | 5 +++ .../FSharp.Editor/xlf/FSharp.Editor.tr.xlf | 5 +++ .../xlf/FSharp.Editor.zh-Hans.xlf | 5 +++ .../xlf/FSharp.Editor.zh-Hant.xlf | 5 +++ 16 files changed, 108 insertions(+) create mode 100644 vsintegration/src/FSharp.Editor/CodeFix/WrapExpressionInParentheses.fs diff --git a/vsintegration/src/FSharp.Editor/CodeFix/WrapExpressionInParentheses.fs b/vsintegration/src/FSharp.Editor/CodeFix/WrapExpressionInParentheses.fs new file mode 100644 index 00000000000..b6e41206a9c --- /dev/null +++ b/vsintegration/src/FSharp.Editor/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 + +[] +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) diff --git a/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj b/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj index 1cbf82861ab..b7a653b0b44 100644 --- a/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj +++ b/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj @@ -180,6 +180,7 @@ + diff --git a/vsintegration/src/FSharp.Editor/FSharp.Editor.resx b/vsintegration/src/FSharp.Editor/FSharp.Editor.resx index 13709945ce2..802675e062e 100644 --- a/vsintegration/src/FSharp.Editor/FSharp.Editor.resx +++ b/vsintegration/src/FSharp.Editor/FSharp.Editor.resx @@ -219,6 +219,9 @@ F# Dispostable Values (top-level) + + Wrap expression in parentheses + Use subtraction instead of negation diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf index 3f88b1c720e..d5486f6aac4 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf @@ -177,6 +177,11 @@ Formátování + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf index 3688348de79..b66b9951e47 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf @@ -177,6 +177,11 @@ Formatierung + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf index 25dd2a86c06..ebe2cb49dbd 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf @@ -177,6 +177,11 @@ Formato + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf index b86e842de07..8a24580b08e 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf @@ -177,6 +177,11 @@ Mise en forme + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf index e39eae13e4f..b6cadf4482c 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf @@ -177,6 +177,11 @@ Formattazione + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf index fa9e2640c6a..9029587aabc 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf @@ -177,6 +177,11 @@ 書式設定 + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf index 8d004350bfe..6c18283919e 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf @@ -177,6 +177,11 @@ 서식 + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf index 263f33a5269..94e101a86e6 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf @@ -177,6 +177,11 @@ Formatowanie + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf index fb4600b9227..dedf532aedc 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf @@ -177,6 +177,11 @@ Formatação + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf index 9982dfa1fe6..1207c51d09f 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf @@ -177,6 +177,11 @@ Форматирование + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf index edbc805cf1f..19c41424348 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf @@ -177,6 +177,11 @@ Biçimlendirme + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf index c52791002ea..14605a7064c 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf @@ -177,6 +177,11 @@ 正在格式化 + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf index 69df9f10ee7..3e89e5c086c 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf @@ -177,6 +177,11 @@ 格式化 + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file