From eb06c5904fbffb0c952161caa7a3e575b89dde00 Mon Sep 17 00:00:00 2001 From: cartermp Date: Fri, 13 Nov 2020 21:44:30 -0800 Subject: [PATCH] Add WrapExpressionInParentheses code fix --- .../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 8bc59c35a19..6b1e0cf445d 100644 --- a/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj +++ b/vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj @@ -89,6 +89,7 @@ + diff --git a/vsintegration/src/FSharp.Editor/FSharp.Editor.resx b/vsintegration/src/FSharp.Editor/FSharp.Editor.resx index 8440136cf33..5e77aaf0b20 100644 --- a/vsintegration/src/FSharp.Editor/FSharp.Editor.resx +++ b/vsintegration/src/FSharp.Editor/FSharp.Editor.resx @@ -219,4 +219,7 @@ F# Dispostable Values (top-level) + + Wrap expression in parentheses + \ No newline at end of file diff --git a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf index acac2c13bf4..0f30f3bec30 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.cs.xlf @@ -172,6 +172,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 639a34858b9..4fced35d25d 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.de.xlf @@ -172,6 +172,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 5f1297eab36..23463795326 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.es.xlf @@ -172,6 +172,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 3ae9d25817a..472f3041f77 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.fr.xlf @@ -172,6 +172,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 8457ca172b1..2fab4e7362b 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.it.xlf @@ -172,6 +172,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 614fdea4a51..eddec702b72 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ja.xlf @@ -172,6 +172,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 832aaa80b5c..4eac65e5d62 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ko.xlf @@ -172,6 +172,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 785230a56bb..50a10428654 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pl.xlf @@ -172,6 +172,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 98636e84fd4..37e1fc6ba6a 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.pt-BR.xlf @@ -172,6 +172,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 04891b9acea..da3c06f2da0 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.ru.xlf @@ -172,6 +172,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 0cbe170c6d7..e5517570196 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.tr.xlf @@ -172,6 +172,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 dd827dbfd65..770b6ccf721 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hans.xlf @@ -172,6 +172,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 a59aa79970f..0b3fb5fa248 100644 --- a/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf +++ b/vsintegration/src/FSharp.Editor/xlf/FSharp.Editor.zh-Hant.xlf @@ -172,6 +172,11 @@ 格式化 + + Wrap expression in parentheses + Wrap expression in parentheses + + \ No newline at end of file